如何在服务器端处理模式下将表单数据与 jQuery DataTables 数据一起发送
How to send form data along with jQuery DataTables data in server-side processing mode
我正在尝试 post 形成数据但没有成功,无法加载数据。
如何将所有带有数组和单个文本框、组合框等的表单数据传递给 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',
});
解决方案 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
参数中作为具有参数 name
和 value
的对象数组,下面是 JSON 表示:
"form": [{"name":"param1","value":"val1"},{"name":"param2","value":"val2"}]
解决方案 2
如果您想将表单数据作为 name/value 对,请参阅 this jsFiddle 替代解决方案示例。
注释
您的数据中有复选框 table。上面的解决方案不适用于数据 table 中的表单元素,因为 DataTable 从 DOM.
中删除了不可见的节点
如果要格式化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'];
这个怎么样?
$('#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]
希望对您有所帮助。
我正在尝试 post 形成数据但没有成功,无法加载数据。
如何将所有带有数组和单个文本框、组合框等的表单数据传递给 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',
});
解决方案 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
参数中作为具有参数 name
和 value
的对象数组,下面是 JSON 表示:
"form": [{"name":"param1","value":"val1"},{"name":"param2","value":"val2"}]
解决方案 2
如果您想将表单数据作为 name/value 对,请参阅 this jsFiddle 替代解决方案示例。
注释
您的数据中有复选框 table。上面的解决方案不适用于数据 table 中的表单元素,因为 DataTable 从 DOM.
中删除了不可见的节点如果要格式化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'];
这个怎么样?
$('#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]
希望对您有所帮助。