如何在Laravel 5.1中使用dataTable

时间:2016-07-03 02:48:25

标签: php ajax laravel datatable

由于我一直在使用dataTable来处理来自数据库的数据,所以我在某处遇到了AJAX错误。

以下是我在视图中的代码,

<table id="prodTable" class="ui red very basic collapsing celled table tableProd" cellspacing="0" width="113%">
          <thead>
            <tr>
                <th></th>
                <th></th>
            </tr>
        </thead>
          <tbody>
        @if(isset($wholesalerConfig) && !empty($wholesalerConfig))
          <?php $products = DB::connection($wholesalerdb)->table('product')->get(); ?>
          @foreach($products as $product)

          <tr class="griDdProd sep" id="productData">
            <td class="colProd">
                            <div class="field">
                                <img class="imglist" src="data:image;base64,'..'"">
                <h2>{{$product->Name}}</h2>
                            </div>
                            <p>&nbsp;</p>
                            <div  class="field">
                                {{$product->Description}}
                            </div>
                            <p>&nbsp;</p>
                            <div class="field">
                                <h3>{{$product->Price}}</h3>
                            </div>
                            <p>&nbsp;</p>
                            <div class="field">
                                <i class="large add to cart icon">
                                        <img src="{{URL::asset('images/add.png')}}" height="23px" width="80px" >
                                </i> 
                            </div>
                </td>
        <td>
        {{$product->Category}}
        {{$product->Subcategory}}
        {{$product->Field1}}
        {{$product->Field2}}
        {{$product->Field3}}
        </td>
            </tr>
            @endforeach
      @endif
        </tbody>
    </table>

上面的表格会读取我的所有数据并在这里显示...而在js方面..我得到了这个

$(document).ready(function() {
    $('#prodTable').DataTable( {
        "processing": true,
        "serverSide": true,
        "pagingType": "full_numbers",
        "columnDefs": [
            {
                "targets": [ 1 ],
                "visible": false,
            },
        ],
        "ajax": {
            "url": "postDatatable",
            "type": "POST",
             "headers": {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
        },

        "columns": [
            { "data": "Name" },
            { "data": "Description" },
        ]
    } );
} ); 

而且,在我的控制器方面,

    public function searchData()
    {
        // $whosalerDb = Session::get('wholesalerDb');

        // if(isset($whosalerDb) && !empty($whosalerDb))
        // {
            // DB table to use
            $table = DB::table('wholesaler1');

            // Table's primary key
            $primaryKey = 'ID';

            // Array of database columns which should be read and sent back to DataTables.
            // The `db` parameter represents the column name in the database, while the `dt`
            // parameter represents the DataTables column identifier. In this case object
            // parameter names
            $columns = array(
                array( 'db' => 'Name', 'dt' => 'Name' ),
                array( 'db' => 'Description',  'dt' => 'Description' ),
            );

            // SQL server connection information
            $sql_details = array(
                'user' => 'root',
                'pass' => '',
                'db'   => 'wholesaler1',
                'host' => 'localhost'
            );


            /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
             * If you just want to use the basic configuration for DataTables with PHP
             * server-side, there is no need to edit below this line.
             */

            include(app_path().'\classes\ssp.class.php' );

            echo json_encode(
                SSP::simple( $_POST, $sql_details, $table, $primaryKey, $columns )
            );
                }
}
它显示我的错误 1)服务器端的方法异常错误......以及在视图端,Ajax错误

我的路线是

Route::post('postDatatable','PostController@searchData');

1 个答案:

答案 0 :(得分:0)

相反

echo json_encode(
            SSP::simple( $_POST, $sql_details, $table, $primaryKey, $columns )
        );

做的:

return response()->json(SSP::simple($_POST, $sql_details, $table, $primaryKey, $columns));

并显示完整的错误消息。