Laravel 5服务器端数据可用

时间:2021-12-08 15:38:16

I'm trying to use datatables plugin together with laravel since I need to manage large tables, and the laravel pagination won't be good enough.

我正在尝试使用datatables插件和laravel,因为我需要管理大型表,laravel的分页不够好。

I'm using yajra/laravel-datatables component, but I can't get it to work, it throws an error:

我正在使用yajra/laravel-datatables组件,但是我无法让它正常工作,它会抛出一个错误:

DataTables warning: table id=project-table - Ajax error. Fore more information about this error, please see http://datatables.net/tn/7

DataTables警告:表id=项目表- Ajax错误。关于此错误的更多信息,请参见http://datatables.net/tn7。

After reading it, I don't know how to solve it, I'm pretty sure that it has something to do with my routing, because I don't quite understand how the ajax is fetching the result.

在阅读了它之后,我不知道如何解决它,我确信它与我的路由有关,因为我不太理解ajax是如何获取结果的。

This is what I've done:

这就是我所做的:

routes.php

routes.php

Route::controllers([
'projects'       => 'ProjectController'

]);

]);

ProjectController.php (just the function that fetch the data)

ProjectController。php(获取数据的函数)

    public function getDataTable()
{
    $projectes = Project::select(['id', 'nom', 'desc', 'preu', 'hores', 'created_at']);

    return Datatables::of($projectes)->make(true);
}

The view:

视图:

<table id="project-table" class="table table-condensed table-bordered table-striped">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>Titol</th>
                        <th>Desc</th>
                        <th>Preu</th>
                        <th>Hores</th>
                        <th>Data Alta</th>
                    </tr>
                </thead>
            </table>

Finally, the js:

最后,js:

$(function() {
$('#project-table').DataTable({
    processing: true,
    serverSide: true,
    ajax: '{{ url("projects/getDataTable") }}',
    columns: [
        {data: 'id', name: 'id'},
        {data: 'nom', name: 'nom'},
        {data: 'desc', name: 'desc'},
        {data: 'preu', name: 'preu'},
        {data: 'hores', name: 'hores'},
        {data: 'created_at', name: 'created_at'}
    ]
});

});

});

2 个解决方案

#1


2  

Change your function to getDatatable (make the T lowercase) in your ProjectController.php. Then change the url in your ajax request to projects/datatable (without the get. Since you used a controller route, the controller will listen for a GET request at projects/datatable).

将函数更改为ProjectController.php中的getDatatable(小写的T)。然后将ajax请求中的url更改为projects/datatable(没有get)。由于使用了控制器路由,控制器将在projects/datatable上侦听GET请求)。

If that doesn't do it, please post the response when you open the projects/datatable page directly in your browser.

如果没有,请在浏览器中直接打开projects/datatable页面时发布响应。

#2


0  

Laravel 5.1 must be installed at the datatables version 6.0:

Laravel 5.1必须安装在datatables 6.0版本:

composer require yajra/laravel-datatables-oracle:~6.0

#1


2  

Change your function to getDatatable (make the T lowercase) in your ProjectController.php. Then change the url in your ajax request to projects/datatable (without the get. Since you used a controller route, the controller will listen for a GET request at projects/datatable).

将函数更改为ProjectController.php中的getDatatable(小写的T)。然后将ajax请求中的url更改为projects/datatable(没有get)。由于使用了控制器路由,控制器将在projects/datatable上侦听GET请求)。

If that doesn't do it, please post the response when you open the projects/datatable page directly in your browser.

如果没有,请在浏览器中直接打开projects/datatable页面时发布响应。

#2


0  

Laravel 5.1 must be installed at the datatables version 6.0:

Laravel 5.1必须安装在datatables 6.0版本:

composer require yajra/laravel-datatables-oracle:~6.0