【问题标题】:data is not displaying in jquery DataTable数据未显示在 jquery DataTable 中
【发布时间】:2018-07-20 11:09:20
【问题描述】:

我正在尝试从数据库中获取数据并显示在 jquery DataTable 中,但数据未显示在 DataTable 中。表是

ordered_books with attributes 'BookID', 'BilledNum','BilledDate', 'Qunatity', 'Price', 'Remarks'

查看页面代码

<table id="showBooksIn" class="table table-bordered">
    <thead>
        <tr>
            <th>BOOK ID</th>
            <th>BILLED DATE</th>
            <th>BILLED NUMBER</th>
            <th>QUANTITY</th>
            <th>PRICE</th>
            <th>REMARKS</th>
        </tr>
    </thead>
</table>

<script type="text/javascript">
    $(document).ready(function(){
        $('#showBooksIn').DataTable({
            "processing":true,
            "serverside":true,
            "ajax":{{route('data')}}
            "columns":[
                {"data": "BookID"},
                {"data": "BilledNum"},
                {"data": "BilledDate"},
                {"data": "Qunatity"},
                {"data": "Price"},
                {"data": "Remarks"},
            ]
        });
    });
</script>

控制器代码

public function index()
    {
        return view('pages.booksin', $this->fetchData());
    }

function fetchData()
{
    $ordered_books = OrderedBook::select('BookID', 'BilledNum','BilledDate', 'Qunatity', 'Price', 'Remarks');
    return Datatables::of($ordered_books)->make(true); //return an instance of the class or interface you request
}

型号代码

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class OrderedBook extends Model
{
    //
}

路线代码

Route::resource('/order','OrderedBookController');
Route::get('/order/data','OrderedBookController@fetchData')->name('data');

我认为问题出在 DataTable javascript 代码上。但我不确定。请帮忙!!!

【问题讨论】:

    标签: php jquery laravel laravel-5 datatables


    【解决方案1】:

    这一行定义,"processing":true,"serverside":true,你是否使用服务器端数据表。

    请按照以下链接实施。

    Server side datatable using laravel

    【讨论】:

      【解决方案2】:

      更改后尝试

      "ajax":{{route('data')}} to ajax: '{!! URL("/data") !!}',
      

      【讨论】:

        【解决方案3】:

        尝试用这样的引号包裹你的 AJAX 路由,

        '{!! route("data") !!}'
        

        【讨论】:

          【解决方案4】:

          name 的索引也需要在 columns 上,如下所示:

          "columns":[
             {data: "BookID", name: "BookID"},
             {data: "BilledNum", name: "BilledNum"},
             {data: "BilledDate", name: "BilledDate"},
             {data: "Qunatity", name: "Qunatity"},
             {data: "Price", name: "Price"},
             {data: "Remarks", name: "Remarks"},
           ]
          

          从data 中删除了双引号,而不是"data" 到data。

          所以,如果需要,您可以复制并粘贴上面的代码。

          【讨论】:

            猜你喜欢
            • 2021-06-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多