I want to submit a form fields via Datatables ajax source and i did it like follow:
我想通过Datatables ajax source提交表单字段,我这样做如下:
Form
<form id="search_fields">
<input type="text" name="condition" value='1'>
</form>
Table
<table class="table" id="list">
<thead>
<tr>
<th>#</th>
<th>بارکد</th>
<th>نمبر صادره مرجع</th>
<th>نمبر وارده#</th>
<th>مرجع ارسال کننده</th>
<th>وضعیت سند</th>
<th>عملیه</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
Datatable configuration js
数据表配置js
<script type="text/javascript">
$(document).ready(function() {
$('#list').dataTable(
{
"ajax": {
"url": "{{URL::to('/docscom/getSearchResultData')}}",
"type": "POST",
"data": $('#search_fields').serialize(),
"success": function(response)
{
alert(response);
}
},
'sDom': 'lf<"clearfix">tip',
"bProcessing": true,
"bServerSide": true,
"iDisplayLength": 1,
"sServerMethod": "POST",
//"sAjaxSource": "{{URL::to('/docscom/getSearchResultData')}}",
"aaSorting": [[ 1, "desc" ]],
"aoColumns": [
{ 'sWidth': '30px' },
{ 'sWidth': '100px' },
{ 'sWidth': '130px', 'sClass': 'center' },
{ 'sWidth': '250px', 'sClass': 'center' },
{ 'sWidth': '130px', 'sClass': 'center' },
{ 'sWidth': '100px', 'sClass': 'center' },
{ 'sWidth': '100px', 'sClass': 'center' }
],
"language": {
"lengthMenu": "نمایش _MENU_ ریکارد در هر صفحه",
"zeroRecords": "ریکارد موجود نیست",
"info": "نمایش صفحه _PAGE_ از _PAGES_",
"infoEmpty": "ریکارد موجود نیست",
"infoFiltered": "(filtered از _MAX_ مجموع ریکارد)"
}
}
);
});
</script>
Controller function
//get search result
public function getSearchResultDataTable()
{
print_r($_POST);exit;
//get result from database
$result = Docscom::getSearchResult();
return Datatables::of($result)
->add_column('operations', '<a href="{{URL::route(\'getDocDetails\',$id)}}" class="table-link">
<span class="fa-stack">
<i class="fa fa-square fa-stack-2x"></i>
<i class="fa fa-search-plus fa-stack-1x fa-inverse"></i>
</span>
</a>')
//->set_row_class('@if($status==0) danger @elseif($status == 1) success @endif')
//->set_row_data('Test','{{$status}}')
->make();
}
Ajax Print_r Post Response
Ajax Print_r发布回复
Array
(
[0] => c
[1] => o
[2] => n
[3] => d
[4] => i
[5] => t
[6] => i
[7] => o
[8] => n
[9] => =
[10] => 1
[draw] => 1
[columns] => Array
(
[0] => Array
(
[data] => 0
[name] =>
[searchable] => true
[orderable] => true
[search] => Array
(
[value] =>
[regex] => false
)
)
[1] => Array
(
[data] => 1
[name] =>
[searchable] => true
[orderable] => true
[search] => Array
(
[value] =>
[regex] => false
)
)
[2] => Array
(
[data] => 2
[name] =>
[searchable] => true
[orderable] => true
[search] => Array
(
[value] =>
[regex] => false
)
)
[3] => Array
(
[data] => 3
[name] =>
[searchable] => true
[orderable] => true
[search] => Array
(
[value] =>
[regex] => false
)
)
[4] => Array
(
[data] => 4
[name] =>
[searchable] => true
[orderable] => true
[search] => Array
(
[value] =>
[regex] => false
)
)
[5] => Array
(
[data] => 5
[name] =>
[searchable] => true
[orderable] => true
[search] => Array
(
[value] =>
[regex] => false
)
)
[6] => Array
(
[data] => 6
[name] =>
[searchable] => true
[orderable] => true
[search] => Array
(
[value] =>
[regex] => false
)
)
)
[order] => Array
(
[0] => Array
(
[column] => 1
[dir] => desc
)
)
[start] => 0
[length] => 1
[search] => Array
(
[value] =>
[regex] => false
)
)
Any one can tell me what is wrong my code? Thanks in advance
任何人都可以告诉我我的代码有什么问题?提前致谢
1 个解决方案
#1
You are not using ajax.data correctly, only object or function is accepted.
您没有正确使用ajax.data,只接受对象或函数。
Your ajax.data property should be changed to:
您的ajax.data属性应更改为:
"data": function(d){
$.each($('#search_fields').serializeArray(), function(i, obj){
d['form_' + obj['name']] = obj['value'];
});
}
This will add form_
parameters with element names appended to server-side request. I'm using form_
prefix to avoid name collision with other DataTables parameters (such as start
, length
, search
, etc.) please modify accordingly.
这将添加form_参数,并将元素名称附加到服务器端请求。我正在使用form_前缀以避免名称与其他DataTables参数(如开始,长度,搜索等)发生冲突,请相应地进行修改。
#1
You are not using ajax.data correctly, only object or function is accepted.
您没有正确使用ajax.data,只接受对象或函数。
Your ajax.data property should be changed to:
您的ajax.data属性应更改为:
"data": function(d){
$.each($('#search_fields').serializeArray(), function(i, obj){
d['form_' + obj['name']] = obj['value'];
});
}
This will add form_
parameters with element names appended to server-side request. I'm using form_
prefix to avoid name collision with other DataTables parameters (such as start
, length
, search
, etc.) please modify accordingly.
这将添加form_参数,并将元素名称附加到服务器端请求。我正在使用form_前缀以避免名称与其他DataTables参数(如开始,长度,搜索等)发生冲突,请相应地进行修改。