jquery datatables: update table cell after button click

前端 未结 1 1117
栀梦
栀梦 2021-01-05 03:29

we have a table in our page, with a few rows and a custom toggle button at the end. the table is loaded via html in the page, not via json.

now, the togglebutton at

相关标签:
1条回答
  • 2021-01-05 04:26

    Here is a possible solution:

    In addition to your code you should update the datatables data as following

    var rowIndex = table.fnGetPosition( $(this).closest('tr')[0] );
    var aData = table.fnGetData( rowIndex  );
    aData[2] = txt; //third column
    

    Here the jsfiddle

    And even better solution would be to use fnUpdate to update the data and display in the same time

    Here the jsfiddle

    // update column following here... 
    var followingCell = $(this).parents('td').prev();
    var txt = followingCell.text() == "1" ? "0" : "1";
    
    var rowIndex = table.fnGetPosition( $(this).closest('tr')[0] );
    table.fnUpdate( txt, rowIndex , 2);
    

    Also instead of us

    var followingCell = $(this).parents('td').prev();
    var txt = followingCell.text() == "1" ? "0" : "1";
    

    use

    var aData = table.fnGetData( rowIndex  );
    aData[2] //use it to check if the value is 0 or 1
    
    0 讨论(0)
提交回复
热议问题