Remember Selected Lines w/ Serverside Processing

Remember Selected Lines w/ Serverside Processing

YaniYani Posts: 24Questions: 7Answers: 0

Here's a little snippet to remember selected lines when you have serverSide processing enabled.

Define somewhere:

remember_lines = [];

After the table initialization:

table.on('select', function (e, dt, action) {
    var row_indexes = dt[0];
    $.each(row_indexes, function (key, row_index) {
        var data = table.row(row_index).data();
        if (typeof data != 'undefined' && typeof data.DT_RowId != 'undefined') {
            if ($.inArray(data.DT_RowId, remember_lines) === -1) {
                remember_lines.push(data.DT_RowId);
            }
        }
    });
});

table.on('deselect', function (e, dt, action) {
    var row_indexes = dt[0];
    $.each(row_indexes, function (key, row_index) {
        var data = table.row(row_index).data();
        if (typeof data != 'undefined' && typeof data.DT_RowId != 'undefined') {
            var array_index = $.inArray(data.DT_RowId, remember_lines);
            if (array_index !== -1) {
                remember_lines.splice(array_index, 1);
            }
        }
    });
});

table.on('draw', function (e, dt, action) {
    table.rows().every(function (rowIdx, tableLoop, rowLoop) {
        var row = table.row(rowIdx);
        var data = row.data();
        if (typeof data != 'undefined' && typeof data.DT_RowId != 'undefined') {
            if ($.inArray(data.DT_RowId, remember_lines) !== -1) {
                row.select();
            }
        }
    });
});

Be sure to look at the table instance variables as they might be named different for you

This discussion has been closed.