I want to add dynamic fields in monogDB
saharajdeep opened this issue · 0 comments
saharajdeep commented
//This is the javascipt Code which adds fields dynamically in front end.
var room = 1;
var temp=[];
// add dynamic fields
function add_education_fields() {
temp.push(this.player);
var marjor = $('#Major').val();
var Schoolname = $('#Schoolname').val();
var Degree = $('#Degree').val();
var educationDate = $('#educationDate option:selected').val();
room++;
var parentDiv = document.getElementById('education_fields'); // point to the parent Div
var childDiv = document.createElement("div"); // create child div on the fly
childDiv.setAttribute("class", "form-group removeclass"+room); // add a dynamic class to the child div
childDiv.innerHTML = '' +
'<div class="row register-form">'+
'<div class="col-sm-3 nopadding">' +
'<div class="form-group">' +
' <input type="text" class="form-control" id="Schoolname" name="firstName" value="'+Schoolname+'" placeholder="First Name">' +
'</div>' +
'</div>' +
'<div class="col-sm-3 nopadding">' +
'<div class="form-group"> ' +
'<input type="text" class="form-control" id="Major" name="lastName" value="'+marjor+'" placeholder="Last Name">' +
'</div>' +
'</div>' +
'<div class="col-sm-3 nopadding">' +
'<div class="form-group">'+
'<label id="Degree" for="validationDefault03">Madhyamik/HS result/Pass certificate</label>'+
' <input type="file" id="Degree" value="'+Degree +'" name="pic" accept="image/*" multiple required>'+
'</div>'+
'</div>' +
'<div class="col-sm-3 nopadding">' +
'<div class="form-group">' +
'<div class="input-group"> ' +
'<div class="input-group-btn">' +
' <button class="btn btn-danger" type="button" onclick="remove_education_fields('+ room +');"> ' +
'<span class="glyphicon glyphicon-minus" aria-hidden="true">-</span> ' +
'</button>' +
'</div></div></div>' +
'</div>' +
'<div class="clear"></div>'+
'</div>'+
'</div>';
parentDiv.appendChild(childDiv); // append or inject dynamic fields to the parent div
temp.push(parentDiv);
var StoredDiv = $('.removeclass'+room).html();
manage_append(room,StoredDiv,'add'); // store the dynamic fields to local storage
$('#Major').val(''); // then reset all fields
$('#Schoolname').val('');
$('#Degree').val('');
$('#educationDate').val(0);
room.value();
}
// remove dynamic fields
function remove_education_fields(rid) {
manage_append(rid,0,'delete');
$('.removeclass'+rid).remove();
}
// add and remove dynamic fields from local storage
function manage_append(room,html,action){
if(action === 'add'){
localStorage.setItem(room,html);//
}else{
localStorage.removeItem(room);
}
}
// restore dynamic fields from local storage
(function () {
for ( var i = 0, len = localStorage.length; i < len; ++i ) {
$("#education_fields").append( localStorage.getItem( localStorage.key( i ) ) );
}
});
//This is my post request
router.post('/', playerFile.single('pic'),(req,res)=>{
if(req.body._id== '')
insertRecord(req,res);
else
updateRecord(req,res);
});
function insertRecord(req, res) {
console.log(req.body);
var player = new Player();
player.teamName = req.body.teamName;
player.year = req.body.year;
player.phoneNo = req.body.phoneNo;
player.firstName = req.body.firstName;
player.lastName = req.body.lastName;
player.pic = req.file.filename;
player.save((err, doc) => {
if (!err)
res.redirect('player/list');
// else {
// if (err.name == 'ValidationError') {
// handleValidationError(err, req.body);
// res.render("player/addOrEdit", {
// viewTitle: "Insert Employee",
// player: req.body
// });
// }
else
console.log('Error during record insertion : ' + err);
// }
});
}
function updateRecord(req, res) {
Player.findOneAndUpdate({ _id: req.body._id }, req.body, { new: true }, (err, doc) => {
if (!err) { res.redirect('player/list'); }
// else {
// if (err.name == 'ValidationError') {
// handleValidationError(err, req.body);
// res.render("employee/addOrEdit", {
// viewTitle: 'Update Employee',
// player: req.body
// });
// }
else
console.log('Error during record update : ' + err);
// }
});
}
I want to submit the fields 10 times and maximum 25 times.Anyting beyond that should give alert message.