Get rows that have changed
Get rows that have changed
Misiu
Posts: 68Questions: 4Answers: 2
I'm trying to create a batch update functionality for my table.
My table declaration looks like so:
[code]
var settings = $('table#settings').dataTable({
"aoColumns": [{
"sType": "string",
"mData": "Name",
"sWidth": "100px"
}, {
"sType": "numeric",
"mData": "Id",
"sWidth": "100px"
}, {
"sType": "numeric",
"mData": "Allow",
"sClass": "center",
"mRender": function(data, type, full) {
return '';
}
}]
});
[/code]
Above code generates table with 3 columns, in last I have checkbox that allows user to change some settings.
Then when user clicks on a button I would like to make only one call to server to save all changes.
My idea was to create an array of all items that have changed, and because I store only 0/1 values in last column I figured out that I can get only 'Id' (second column value) for every item that has changed.
This way I will be able to send simple array to server.
For example if I'll have table with 4 rows:
[quote]
Name | Id | Checked
---------------------
One | 1| Yes
Two| 2| Yes
Three | 5| No
Four | 10| Yes
[/quote]
And I change it to:
[quote]
Name | Id | Checked
---------------------
One | 1| No
Two| 2| Yes
Three | 5| Yes
Four | 10| Yes
[/quote]
I only need to send array [1,5] to server. I hope this is quite simple :)
I started to create function that will give me those Id's from specific table:
[code]
function getChanges() {
var i;
var iLen;
var data = settings.fnSettings().aoData;
//console.log(settings.fnSettings());
var result = '';
for (i = 0, iLen = data.length; i < iLen; i++) {
var sLoopData = data[i]._aData['Id'];
result += ',' + sLoopData;
}
return result;
}
[/code]
This function returns all Id's for my table. I need to check if data[i]._aData['Allow'] is equal to that row checkbox state.
If current row _aData['Allow'] is different then I need to add that row aData['Id'] value to my array.
I would really appreciate some help on that.
My table declaration looks like so:
[code]
var settings = $('table#settings').dataTable({
"aoColumns": [{
"sType": "string",
"mData": "Name",
"sWidth": "100px"
}, {
"sType": "numeric",
"mData": "Id",
"sWidth": "100px"
}, {
"sType": "numeric",
"mData": "Allow",
"sClass": "center",
"mRender": function(data, type, full) {
return '';
}
}]
});
[/code]
Above code generates table with 3 columns, in last I have checkbox that allows user to change some settings.
Then when user clicks on a button I would like to make only one call to server to save all changes.
My idea was to create an array of all items that have changed, and because I store only 0/1 values in last column I figured out that I can get only 'Id' (second column value) for every item that has changed.
This way I will be able to send simple array to server.
For example if I'll have table with 4 rows:
[quote]
Name | Id | Checked
---------------------
One | 1| Yes
Two| 2| Yes
Three | 5| No
Four | 10| Yes
[/quote]
And I change it to:
[quote]
Name | Id | Checked
---------------------
One | 1| No
Two| 2| Yes
Three | 5| Yes
Four | 10| Yes
[/quote]
I only need to send array [1,5] to server. I hope this is quite simple :)
I started to create function that will give me those Id's from specific table:
[code]
function getChanges() {
var i;
var iLen;
var data = settings.fnSettings().aoData;
//console.log(settings.fnSettings());
var result = '';
for (i = 0, iLen = data.length; i < iLen; i++) {
var sLoopData = data[i]._aData['Id'];
result += ',' + sLoopData;
}
return result;
}
[/code]
This function returns all Id's for my table. I need to check if data[i]._aData['Allow'] is equal to that row checkbox state.
If current row _aData['Allow'] is different then I need to add that row aData['Id'] value to my array.
I would really appreciate some help on that.
This discussion has been closed.
Replies
[code]
function getChanges() {
var changed = new Array();
$('table#settings input:checkbox').filter(function () {
return ($(this).prop('checked') != $(this).prop('defaultChecked'));
}).each(function () {
changed.push($(this).attr('name').substr(1));
});
return changed;
}
[/code]
This function returns Id's for all changed rows.
Hope this will be helpful for anyone :)