【问题标题】:Datatable - Server side data is not displaying数据表 - 服务器端数据未显示
【发布时间】:2016-06-22 03:21:43
【问题描述】:

我第一次使用datatableLaravel 5,所以我尝试使用此代码,我在ajax 上获取数据,但无法在datatable 上显示数据。

任何想法如何将响应数据加载到数据表。

这是我的结构:

结果:

路线:

Route::get('admin/pages/datatable', 'admin\PagesController@getDataTable'); // Pages (Static Pages)

pages.blade.php:

<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-header">
                    <h3 class="box-title">Pages Table</h3>
                </div><!-- /.box-header -->
                <div class="box-body">
                    <table id="example32" class="table table-bordered table-hover">
                        <thead>
                            <tr>
                                <th>PageId</th>
                                <th>Title</th>
                                <th>Text</th>
                                <th>MetaKeywords</th>
                                <th>MetaDescription</th>
                                <th>PostedBy</th>
                            </tr>
                        </thead>
                        <tfoot>
                            <tr>
                                <th>PageId</th>
                                <th>Title</th>
                                <th>Text</th>
                                <th>MetaKeywords</th>
                                <th>MetaDescription</th>
                                <th>PostedBy</th>
                            </tr>
                        </tfoot>
                    </table>
                </div><!-- /.box-body -->
            </div><!-- /.box -->
        </div><!-- /.col -->
    </div><!-- /.row -->
</section><!-- /.content -->

PagesController.php:

public function getDataTable()
{
    $Pages = new Pages();
    return $GetAllPages = $Pages->GetPages();
}

Pages.php:

public function GetPages()
{
    return DB::table('pages')->select('PageId', 'Title', 'Text', 'MetaKeywords', 'MetaDescription', 'PostedBy')
            ->get();
}

【问题讨论】:

    标签: php jquery laravel datatable laravel-5


    【解决方案1】:

    您必须传递索引数组而不是关联数组。你的回复应该是这样的

    {['pageid', 'Title', 'Text', ... ]}
    

    只需要遵循正确的列顺序即可。

    也许你可以使用,

    public function GetPages()
    {
         $pages = DB::table('pages')->select('PageId', 'Title', 'Text', 'MetaKeywords', 'MetaDescription', 'PostedBy')->get();
         $pages = $pages->map(function($item){
             return [$item->PageId, $item->Title,$item->Text,$item->MetaKeywords,$item->MetaDescription, $item->PostedBy ];
         });
         return $pages;
    }
    

    【讨论】:

    • 收到此错误:Call to undefined method Illuminate\Database\Query\Builder::map()
    • 对不起!尝试在$pages = DB::table('pages')-&gt;select('PageId', 'Title', 'Text', 'MetaKeywords', 'MetaDescription', 'PostedBy') 之后添加-&gt;get()。我更新了答案。
    猜你喜欢
    • 2021-12-11
    • 1970-01-01
    • 2016-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-11
    • 2021-10-11
    相关资源
    最近更新 更多