DataTables row.add to specific index

前端 未结 2 1039
生来不讨喜
生来不讨喜 2020-11-29 08:05

I\'m replacing row items like this:

var $targetRow = $(entity.row),
    dataTable = $targetRow.closest(\'table.dataTable\').DataTable();

dataTable.row($targ         


        
2条回答
  •  盖世英雄少女心
    2020-11-29 08:16

    Another way is to insert the row, and then move the row in the DataTable row array to a position you specify before redrawing the Table:

    // Define the row to insert (using your method of choice)
    var rowInsert = $('#table-id').find('tr:last');
    // Get table reference - note: dataTable() not DataTable()
    var table = $('#table-id').dataTable();
    // Get api
    var dt = table.api();
    // Insert row (inserted as the last element in aiDisplayMaster array)
    dt.row.add(rowInsert);
    // Get the array holding the rows
    var aiDisplayMaster = table.fnSettings()['aiDisplayMaster'];
    // Remove the last element in the array
    var moveRow = aiDisplayMaster.pop();
    // EITHER add row to the beginning of the array (uncomment)
    //aiDisplayMaster.unshift(moveRow);
    // OR add row to a specific index (in this case to index 3)
    var index = 3;
    aiDisplayMaster.splice(index, 0, moveRow);
    // Redraw Table
    dt.draw(false);
    

提交回复
热议问题