简体   繁体   English

带有数据表的Laravel 5.4

[英]Laravel 5.4 with datatables

I have added this package https://github.com/yajra/laravel-datatables into my package and i am trying to use it in my application. 我已经将此包https://github.com/yajra/laravel-datatables添加到我的包中,并且我试图在我的应用程序中使用它。

This is the view 这是视图

@section('content')
    <table class="table table-bordered" id="users-table">
        <thead>
            <tr>
                <th>Emp No</th>
                 <th>Birth Date</th>
                  <th>First Name</th>
                   <th>Last Name</th>
                    <th>Gender</th>
                     <th>Hire Date</th>

            </tr>
        </thead>
    </table>
@stop

@push('scripts')

<script type="text/javascript">
$(function() {
    $('#users-table').DataTable({
        processing: true,
        serverSide: true,
        responsive: true,
        ajax: 'http://localhost:8000/tasks',
        columns: [
            { data: 'emp_no', name: 'emp_no' }
            { data: 'birth_date', name: 'birth_date' },
            { data: 'first_name', name: 'first_name' },
            { data: 'last_name', name: 'last_name' },
            { data: 'gender', name: 'gender' },
            { data: 'hire_date', name: 'hire_date' }
        ]
    });
});
</script>

@endpush
@endsection

This is the route 这是路线

Route::get('tasks', 'PrototypeController@getTasks')->name('datatable.tasks');

This is the controller 这是控制器

public function getTasks()
    {
       return Datatables::of(Employees::query())->make(true);
       //returns json
    }

This code loads the view containining the datatables. 此代码加载包含数据表的视图。

The url http://localhost:8000/tasks returns the json in the web browser but the datatable is never rendered in my view. 网址http:// localhost:8000 / tasks在网络浏览器中返回json,但数据表从未在我的视图中呈现。 When i check in my view, there are no browser errors. 当我查看时,没有浏览器错误。

What could be the problem?. 可能是什么问题呢?。

Create two method, one display our view and other method that will process our datatables ajax request. 创建两个方法,一个显示我们的视图,另一个显示将处理我们的数据表ajax请求的方法。

Display our view 显示我们的观点

public function viewTasks()
{
    return view('Tasks.index');
}

Process our datatables ajax request 处理我们的数据表ajax请求

public function getTasks()
{
    return Datatables::of(Employees::query())->make(true);
}

Reference Link :- Yajra Datatable 参考链接: -Yajra数据表

You should change in your controller. 您应该更改控制器。 Add code below for showing data table: 在下面添加代码以显示数据表:

 public function task()
    {
        return view('datatables.Index');
    }

Change in web.php file for add new route: 更改web.php文件以添加新路由:

Route::get( 'employee','PrototypeController@task');

Add data table CDN and bootstrap also 添加数据表CDN和引导程序

<link href="https://datatables.yajrabox.com/css/app.css" rel="stylesheet">
    <link href="https://datatables.yajrabox.com/css/datatables.bootstrap.css" rel="stylesheet">
    <link href='https://fonts.googleapis.com/css?family=Lato:400,700,300|Open+Sans:400,600,700,800' rel='stylesheet'
          type='text/css'>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM