如何在服务器端处理模式下发送表单数据和jQuery DataTables数据

How to send form data along with jQuery DataTables data in server-side processing mode

本文关键字:数据 表单 DataTables jQuery 服务器端 处理 模式      更新时间:2023-09-26

我试图发布表单数据没有成功,数据无法加载。

如何将所有表单数据与数组和单个文本框,组合框等传递给fnServerdata ?

table_obj = $('#group-table').dataTable({
   "sAjaxSource": "URL Goes here",
   fnServerData: function(sSource, aoData, fnCallback,oSettings) {
      oSettings.jqXHR = $.ajax( {
         "dataType": 'json',
         "type": "POST",
         "url": sSource+'?'+$.param(aoData),
         "data": $("#frm").serializeArray(),
         "success": fnCallback
      } );
   },
   aaSorting: [[ 1, "desc" ]],
   bProcessing: true,
   bServerSide: true,
   processing : true,
   columnDefs: [{
        'targets': 0,
        'searchable':false,
        'orderable':false,
        'className': 'dt-body-center',
        'render': function (data, type, full, meta){
            return '<label><input type="checkbox" name="user_id[]" value="' + $('<div/>').text(data).html() + '"></label>';
        }
     }],
   rowCallback: function(row, data, dataIndex){
       // If row ID is in list of selected row IDs
       if($.inArray(data[0], rows_selected) !== -1){
          $(row).find('input[type="checkbox"]').prop('checked', true);
          $(row).addClass('selected');
       }
   },
   iDisplayLength: '50',
});

如果要格式化POST数据,也可以使用jquery .each()函数格式化表单数据。让我使用上面的答案,使用解决方案#1,但使用jquery .each()来格式化数据。

$('table').DataTable({
  "ajax": {
     "url": "URL HERE",
     "type": "POST",
     "data": function(d) {
       var frm_data = $('form').serializeArray();
       $.each(frm_data, function(key, val) {
         d[val.name] = val.value;
       });
     }
  }
});

你可以在PHP中访问它,比如:

var $data = $_POST['name'];

解决方案1

替换:

$('#group-table').dataTable({
   "sAjaxSource": "URL Goes here",
   fnServerData: function(sSource, aoData, fnCallback,oSettings) {
      oSettings.jqXHR = $.ajax( {
         "dataType": 'json',
         "type": "POST",
         "url": sSource+'?'+$.param(aoData),
         "data": $("#frm").serializeArray(),
         "success": fnCallback
      } );
   },

:

$('#group-table').dataTable({
   "ajax": {
      "url": "URL Goes here",
      "type": "POST",
      "data": function(d){
         d.form = $("#frm").serializeArray();
      }
   },

您的表单数据将在form参数中作为具有参数namevalue的对象数组,以下是JSON表示:

"form": [{"name":"param1","value":"val1"},{"name":"param2","value":"val2"}]

解决方案2

如果您希望将表单数据作为名称/值对,请参阅此jsFiddle以获取另一种解决方案的示例。


指出

数据表中有复选框。上面的解决方案不适用于数据表中的表单元素,因为DataTable从DOM中删除了不可见的节点。

这个怎么样?

$('#group-table').DataTable( {
          "processing": true,
          "serverSide": true,
          "bDestroy": true,
          "bJQueryUI": true,
          "ajax": {
              "url": "url here",
              "type": "POST",
              "data": {
                   store: $('#store').val(),
                   month: $('#m').val(),
                   year: $('#y').val(),
                   status: $('#status').val(),
                },
          }
    } );

,那么您可以通过PHP访问上面的示例数据。

$_POST['store']
$_POST['month']
$_POST['year']
$_POST['status]

希望对你有帮助。