Quantcast
Viewing all articles
Browse latest Browse all 82040

Get rows that have changed

I'm trying to create a batch update functionality for my table.

My table declaration looks like so:

    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 '<input type="checkbox" class="change" name="d' + full['Id'] + '" value="' + full['Id'] + '" ' + (data == 1 ? 'checked="checked"' : '') + ' />';
                }
            }]
    });

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:

Name | Id | Checked
---------------------
One | 1| Yes
Two| 2| Yes
Three | 5| No
Four | 10| Yes
And I change it to:

Name | Id | Checked
---------------------
One | 1| No
Two| 2| Yes
Three | 5| Yes
Four | 10| Yes

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:
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;
    }

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.

Viewing all articles
Browse latest Browse all 82040

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>