Laravel Datatables通过ajax源提交表单

时间:2015-05-13 07:09:31

标签: laravel-4 datatables

我想通过Datatables ajax source提交表单字段,我按照以下方式提交:

表格

<form id="search_fields">
    <input type="text" name="condition" value='1'>
</form>

<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>

数据表配置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>

控制器功能

//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发布回复

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
        )

)

任何人都可以告诉我我的代码有什么问题? 提前致谢

1 个答案:

答案 0 :(得分:0)

您没有正确使用ajax.data,只接受对象或功能。

您的ajax.data属性应更改为:

"data": function(d){
   $.each($('#search_fields').serializeArray(), function(i, obj){
      d['form_' + obj['name']] = obj['value'];
   });
}    

这将添加form_参数,并将元素名称附加到服务器端请求。我使用form_前缀来避免与其他DataTables参数(例如startlengthsearch等)发生名称冲突,请相应修改。