I am using jquery datatables . http://jsfiddle.net/s3j5pbj4/2/ I am populating around 3000 records in paginated table.Problem is that If am selecting few checkbox and dropdown
Please take a look at my solution at JSFiddle.
HTML
select operation
Javascript:
//ajax emulation
$.mockjax({
url: '/test/0',
responseTime: 200,
responseText: {
"aaData" : [
[{"id":1}, {"chk":"on"}, {"operation":["Modify", "Delete"]}],
[{"id":2}, {"chk":"on"}, {"operation":["Modify", "Delete"]}],
[{"id":3}, {"chk":"on"}, {"operation":["Modify", "Delete"]}],
[{"id":4}, {"chk":"on"}, {"operation":["Modify", "Delete"]}],
[{"id":5}, {"chk":"on"}, {"operation":["Modify", "Delete"]}],
[{"id":6}, {"chk":"on"}, {"operation":["Modify", "Delete"]}],
[{"id":7}, {"chk":"on"}, {"operation":["Modify", "Delete"]}]
]
}
});
// Global variable holding current state of the controls in the data table
var g_data = {};
var $table = $('#test');
$table.dataTable( {
"lengthMenu": [[2,25,50 ,100, -1], [2,25,50, 100, "All"]],
"pagingType": "full_numbers" ,
"paging": true,
"ordering" : true,
"scrollY":false,
"autoWidth": false,
"serverSide": false,
"processing": false,
"info": true ,
"deferRender": true,
"processing": true,
"columns": [
["data", 1 ],
["data", 2 ]
],
"ajax" : {
"url": "/test/0",
"dataSrc" : function(json){
var data = json.aaData;
for (var i = 0; i < data.length; i++){
var chk_name = 'chk_' + data[i][0].id;
// If checkbox was never checked
if(typeof g_data[chk_name] === 'undefined'){
// Retrieve checkbox state from received data
g_data[chk_name] = (data[i][1].chk === 'on') ? true : false;
}
}
return data;
}
},
"createdRow" : function( row, data, index ){
var chk_name = 'chk_' + data[0].id;
var chk_checked = (g_data[chk_name]) ? " checked" : "";
$('td:eq(0)', row)
.html('');
var select_name = 'select_' + data[0].id;
html =
'';
$('td:eq(1)', row).html(html);
},
});
$('#test tbody').on('click', 'input[type=checkbox]', function (e){
g_data[this.name] = this.checked;
});
$('#test tbody').on('change', 'select', function (e){
if(this.selectedIndex != -1){
var value = this.options[this.selectedIndex].value;
g_data[this.name] = value;
}
});
$('#btn-submit').on('click', function(){
$('#test-data-json').val(JSON.stringify(g_data));
console.log($('#test-data-json').val());
});
I have slightly updated your code since it was a mix of new and legacy options. However I haven't edited legacy server response using aaData property, so you don't have to change your server-side script.
Basically, the solution is to use a variable (g_data in my example) to store/retrieve state of dynamic form controls.
Upon form submission, the data is stored in hidden INPUT element as JSON string.
Optionally, if form validation is needed, inspect the state of controls stored in g_data variable.