Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在jquery数据表分页中选择记录_Jquery_Datatable_Datatables_Struts2 Jquery - Fatal编程技术网

如何在jquery数据表分页中选择记录

如何在jquery数据表分页中选择记录,jquery,datatable,datatables,struts2-jquery,Jquery,Datatable,Datatables,Struts2 Jquery,当我单击select allhyperlink选项或我想在特定页面中使用复选框将特定记录发送到服务器类时,我正在尝试发送jquery数据表中的所有记录,但问题是当我单击form submit按钮时,即Export PDF仅从当前页面获取记录,即使在jquery的其他页面中选择了记录数据表分页 为什么jquery数据表不同页面中的所选记录不发送到java类 我希望我正确地理解了您的意思,您希望在单击全选按钮时选择所有行,并将所选行的计数发送到服务器 这是一本书 所以我做了这个,您将了解如何使用da

当我单击select allhyperlink选项或我想在特定页面中使用复选框将特定记录发送到服务器类时,我正在尝试发送jquery数据表中的所有记录,但问题是当我单击form submit按钮时,即Export PDF仅从当前页面获取记录,即使在jquery的其他页面中选择了记录数据表分页

为什么jquery数据表不同页面中的所选记录不发送到java类


我希望我正确地理解了您的意思,您希望在单击全选按钮时选择所有行,并将所选行的计数发送到服务器

这是一本书

所以我做了这个,您将了解如何使用datatables api将计数发送到服务器:

$(document).ready(function() {
   var table =  $('#example').DataTable();

$("#selectall").click(function() {
    var rows = table.rows({ 'search': 'applied' }).nodes();

   debugger;
   if($('input:checked', rows).length == rows.length){
     $('input[type="checkbox"]', rows).prop('checked', false);
    }
    else{
     $('input[type="checkbox"]', rows).prop('checked', true);
   }


$('#dvcount').html($(rows).find("input:checked").length);

$("body").on("change","input",function() {

    var rows = table.rows({ 'search': 'applied' }).nodes();
    $('#dvcount').html($(rows).find("input:checked").length);

});

  } );

我试图用不同的方法来解决这个问题,但我认为上面的答案是最优雅的。我查看了基础数据并改变了这一点:

$(document).ready(function() {
  let runningTotal = 0;
  let table = $('#example').DataTable();
  $("#selectall").click(function() {
    if (runningTotal == table.rows().count()) {
      table.rows().every(function(rowIdx, tableLoop, rowLoop) {
        let clone = table.row(rowIdx).data().slice(0);
        clone[[clone.length - 1]] = '<input type="checkbox" class="record">'
        table.row(rowIdx).data(clone);
      });
    } else {
      table.rows().every(function(rowIdx, tableLoop, rowLoop) {
        let clone = table.row(rowIdx).data().slice(0);
        clone[[clone.length - 1]] = '<input type="checkbox" class="record" checked="checked">'
        table.row(rowIdx).data(clone);
      });
    }
    runningTotal = 0;
    table.rows().every(function(rowIdx, tableLoop, rowLoop) {
      var data = this.data();
      if ($(data[data.length - 1]).prop("checked")) {
        runningTotal++
      }
    });
    $('#dvcount').html(runningTotal);
  });
  $('#example tbody').on("click", ".record", function() {
    let clone = table.row($(this).closest('tr')).data().slice(0);
    let checkbox = clone[clone.length - 1];
    if ($(checkbox).prop("checked")) {
      clone[[clone.length - 1]] = '<input type="checkbox" class="record">'
    } else {
      clone[[clone.length - 1]] = '<input type="checkbox" class="record" checked="checked">';
    }
    table.row($(this).closest('tr')).data(clone);
    runningTotal = 0;
    table.rows().every(function(rowIdx, tableLoop, rowLoop) {
      var data = this.data();
      if ($(data[data.length - 1]).prop("checked")) {
        runningTotal++
      }
    });
    $('#dvcount').html(runningTotal);
  });
  $("#export").on("click", function() {
    let exportedRows = [];
    table.rows().every(function(rowIdx, tableLoop, rowLoop) {
      let data = table.row(rowIdx).data()
      if ($(data[data.length - 1]).prop("checked")) {
        exportedRows.push(data.slice(0, -1));
      }
    });
    console.log(exportedRows);
  });
});

这是一个JSFIDLE。

您能指定程序所需的行为吗?请编辑第一段,因为不可能理解你的意思。它的显示计数正确,但为什么我不能将所有数据发送到服务器,仍然发送当前页面?你应该针对该问题提出一个新问题@Jancypradepa我们可以使用struts标记,比如clone[[clone.length-1]]=而不是html标记吗?我认为一个更好的解决方案,我以前使用过的一个解决方案是在选中每行数据时获取它,并将其添加到数组变量中,然后将其发送到服务器。。。我猜@offir peer和我都提供了答案,他的答案似乎比我的要优雅得多,但我的答案确实有办法在导出按钮上的单击事件中检索数据。
$(document).ready(function() {
  let runningTotal = 0;
  let table = $('#example').DataTable();
  $("#selectall").click(function() {
    if (runningTotal == table.rows().count()) {
      table.rows().every(function(rowIdx, tableLoop, rowLoop) {
        let clone = table.row(rowIdx).data().slice(0);
        clone[[clone.length - 1]] = '<input type="checkbox" class="record">'
        table.row(rowIdx).data(clone);
      });
    } else {
      table.rows().every(function(rowIdx, tableLoop, rowLoop) {
        let clone = table.row(rowIdx).data().slice(0);
        clone[[clone.length - 1]] = '<input type="checkbox" class="record" checked="checked">'
        table.row(rowIdx).data(clone);
      });
    }
    runningTotal = 0;
    table.rows().every(function(rowIdx, tableLoop, rowLoop) {
      var data = this.data();
      if ($(data[data.length - 1]).prop("checked")) {
        runningTotal++
      }
    });
    $('#dvcount').html(runningTotal);
  });
  $('#example tbody').on("click", ".record", function() {
    let clone = table.row($(this).closest('tr')).data().slice(0);
    let checkbox = clone[clone.length - 1];
    if ($(checkbox).prop("checked")) {
      clone[[clone.length - 1]] = '<input type="checkbox" class="record">'
    } else {
      clone[[clone.length - 1]] = '<input type="checkbox" class="record" checked="checked">';
    }
    table.row($(this).closest('tr')).data(clone);
    runningTotal = 0;
    table.rows().every(function(rowIdx, tableLoop, rowLoop) {
      var data = this.data();
      if ($(data[data.length - 1]).prop("checked")) {
        runningTotal++
      }
    });
    $('#dvcount').html(runningTotal);
  });
  $("#export").on("click", function() {
    let exportedRows = [];
    table.rows().every(function(rowIdx, tableLoop, rowLoop) {
      let data = table.row(rowIdx).data()
      if ($(data[data.length - 1]).prop("checked")) {
        exportedRows.push(data.slice(0, -1));
      }
    });
    console.log(exportedRows);
  });
});