Hi I am using jquery datatable in my MVC page so .In 1 page I have to send all the data from the datatable to the controller. So what I did is I made all the datas into 1 array and I passed it to the controller. using code
var AllSelectedData = [];
var selected = [];
table = $('#Distpopup').DataTable();
table.rows().every(function () {
var d = this.data();
AllSelectedData.push(d);
});
$("#dtlist").val(AllSelectedData);
It is working fine for model binded datatable but in 1 case I am having a datatable which is binded using ajax in that case it is not working.Any suggestions
You can access all the rows of the table in model binding and also in Ajax.
e.g. this is your datatable initialization.
var table = $('#Distpopup').DataTable();
After adding data to the table you want to get this data in an array. If you want to send data to the controller use JSON.stringify() to stringfy your data and then send it to the controller and you can use it in model binding with a form field or also you can pass this stringfy data using Ajax request.
var AllSelectedData = [];
table.rows().every(function () {
var d = this.data();
AllSelectedData.push(d);
});
$("#dtlist").val(JSON.stringify(AllSelectedData));
I also face this issue see this question that it still has no answer.
You can use datatable callbacks, http://legacy.datatables.net/usage/callbacks
One way to do it using initcomplete callback function which works when the table has been initialised.
$('#Distpopup').DataTable({
//Other settings
"fnInitComplete": function (oSettings, json) {
console.log(json.data);
},
});
Another way using createdrow function which works every row created
var AllSelectedData = [];
$('#Distpopup').DataTable({
//Other settings
"createdRow": function (row, data, index) {
AllSelectedData.push(data);
},
});