首页 文章

在datatables中选中带复选框的行时出错

提问于
浏览
0

我有以下问题:我遵循这些教程,以便能够通过复选框选择的每一行将数据保存在数组中 .

就像在提供的链接中一样,我在JSON中接收数据以加载我的dataTable,现在我看到需要,通过一个按钮将新数据带到我的dataTable中,并为链接中的代码添加了更多功能 . 提供,如下:

To delete the datatable:

function destroy_data_table(){
  var table = $('#example').DataTable({
  if (table != undefined){
  table .fnDestroy();
  }
}

In the click of the button I destroy my datatable and call the function that loads a new one:

$("#btnNewDatatable").click(function (){
  destroy_data_table();
  new_data();
});

And this is the function that reloads the datatable:

function new_data(){
  var table = $('#example').DataTable({
  "sAjaxSource": //Here I return in json what my server      sends,
  "fnServerData": function ( sSource, aoData, fnCallback, oSettings ){
    oSettings.jqXHR = $.ajax({
    "dataType": 'json',
    "type": "POST",
    "url": sSource,
    "data": aoData,
    "success": function(result){
      fnCallback(result);
    }
   });
  },
  responsive: true,
  'columnDefs': [{
    'targets': 0,
    'searchable':false,
    'orderable':false,
    'width':'1%',
    'className': 'dt-body-center',
    'render': function (data, type, full, meta){
      return '<input type="checkbox">';
    }
   }]
  });
 }

我的问题是,在加载新的数据库时,当选择一行时,这继续显示来自前一个数据表的数据,我做了一个console.log一个var rows_selected = [];哪个负责存储每个选定行的数据,我正在严重破坏表格或可能是我的错误?

1 回答

  • 0

    你有没有尝试过

    将此表变量保持为全局以便于使用 .

    //Initialize First time
    
        var table = $('#example').DataTable({
              ......
              }
    
        function destroy_data_table(){
        table.clear().draw();
        }
    

    而不是破坏 .

相关问题