Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery DataTables Swapping Rows

I have created a function to swap rows in a table, but it doesn't seem to be working correctly. The data for the rows doesn't seem to be matching up with the data that is in the array. Can anyone help point out the bug in my code, or how I may be misusing some of the DataTable functions?

function swapDataTableRows(selector, row1Index, row2Index)
{
    var datatable = selector.DataTable();
    var rows = datatable.rows().data();
    var row1Data = datatable.row(row1Index).data();
    var row2Data = datatable.row(row2Index).data();

    datatable.row(row1Index).data(row2Data);
    datatable.row(row2Index).data(row1Data);
}

swapDataTableRows(table, 2, 3) - It will swap rows 1 and 2. If I add 1 to the indexes then it goes out of bounds.

like image 835
Taztingo Avatar asked Jun 20 '26 03:06

Taztingo


1 Answers

I'm not sure to have understood your problem...

I try to create a simple demo to understand:

http://jsfiddle.net/q0zb1rkc/

... in your functions you never draw the table with new (edited) data:

I change this 2 rows of your script:

datatable.row(row1Index).data(row2Data);
datatable.row(row2Index).data(row1Data);

and i add .draw():

datatable.row(row1Index).data(row2Data);
datatable.row(row2Index).data(row1Data);

width .draw() also DOM change and the ROW1 and ROW2 exchange.

like image 132
Frogmouth Avatar answered Jun 21 '26 17:06

Frogmouth