Laravel Yajra Datatable问题,带有分页和recordsFiltered

时间:2018-06-28 09:36:56

标签: jquery ajax laravel datatables

控制器:

    use Datatables;

    public function index(Request $request)
    {

        $services = Service::select(['id', 'name_en', 'name_ar', 'color', 'duration', 'friday', 'saturday', 'sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'created_at']);

        return Datatables::eloquent($services)->addColumn('action','
        <div class="card-action">
        <button type="button" class="background-green edit-service" data-toggle="tooltip" data-placement="top" data-original-title="Edit">
        <i class="fa fa-edit" data-toggle="modal" data-target="#edit-service"></i>
        </button>
        <button type="button" class="sweet-5 background-red remove-service" data-toggle="tooltip" data-placement="top" data-original-title="Delete">
        <i class="fa fa-trash"></i>
        </button>
        </div>
        ')->make(true);
    }

路线:

Route::get('servicesAPI' , [
'uses'       => 'ServiceController@index',
'as'         => 'servicesAPI.index',
'middleware' => 'roles',
'roles'      => ['Admin', 'View Services'],

]);

AJAX:

$(function() {
$('#services_table').DataTable({
    processing      : true,
    serverSide      : true,
    responsive      : true,
    stateSave       : true,
    autoWidth       : true,
    lengthMenu      : [[5, 10, 15, 25, 50, 100], [5, 10, 15, 25, 50, 100]],
    paging          : true,
    pagingTypeSince : 'numbers',
    pagingType      : 'full_numbers',
    ajax            : 'servicesAPI',
    deferRender: true,
    order: [[ 10, "asc" ]],
    columns         : [
        { data: 'name_en',   name: 'name_en',
                "render": function ( data, type, row ) {
                    return '<b style="color:'+row['color']+'">' + data + '</b>';
                    }
        },
        { data: 'color',     name: 'color' , visible: false},
        { data: 'duration',  name: 'duration' ,
            "render": function ( data, type, row ) {
                return data + ' Min';
            }
        },
        { data: 'saturday',  name: 'saturday',
                "render": function ( data, type, row ) {
                    switch (data) {
                        case '0':
                            return '<i class="fa fa-close text-red"></i>';
                            break;
                        case '1':
                            return '<i class="fa fa-check text-blue"></i>';
                            break;
                    }
                }
        },
        { data: 'sunday',    name: 'sunday',
            "render": function ( data, type, row ) {
                switch (data) {
                    case 0:
                        return '<i class="fa fa-close text-red"></i>';
                        break;
                    case '1':
                        return '<i class="fa fa-check text-blue"></i>';
                        break;
                }
            }
        },
        { data: 'monday',    name: 'monday',
            "render": function ( data, type, row ) {
                switch (data) {
                    case 0:
                        return '<i class="fa fa-close text-red"></i>';
                        break;
                    case '1':
                        return '<i class="fa fa-check text-blue"></i>';
                        break;
                }
            }
        },
        { data: 'tuesday',   name: 'tuesday',
            "render": function ( data, type, row ) {
                switch (data) {
                    case 0:
                        return '<i class="fa fa-close text-red"></i>';
                        break;
                    case '1':
                        return '<i class="fa fa-check text-blue"></i>';
                        break;
                }
            }
        },
        { data: 'wednesday', name: 'wednesday',
            "render": function ( data, type, row ) {
                switch (data) {
                    case 0:
                        return '<i class="fa fa-close text-red"></i>';
                        break;
                    case '1':
                        return '<i class="fa fa-check text-blue"></i>';
                        break;
                }
            }
        },
        { data: 'thursday',  name: 'thursday',
            "render": function ( data, type, row ) {
                switch (data) {
                    case 0:
                        return '<i class="fa fa-close text-red"></i>';
                        break;
                    case '1':
                        return '<i class="fa fa-check text-blue"></i>';
                        break;
                }
            }
        },
        { data: 'friday',    name: 'friday',
            "render": function ( data, type, row ) {
                switch (data) {
                    case 0:
                        return '<i class="fa fa-close text-red"></i>';
                        break;
                    case '1':
                        return '<i class="fa fa-check text-blue"></i>';
                        break;
                }
            }
        },
        { data: 'id', name: 'id' , visible: false},
        { data: 'action',     name: 'action', orderable: false, searchable: false}
    ]
});

});

查看:

            <table id="services_table" class="table table-bordered table-center table-striped">
            <thead>
            <tr>
                <th>@lang('services.service_name')</th>
                <th>@lang('services.color')</th>
                <th>@lang('services.duration')</th>
                <th>@lang('services.sat')</th>
                <th>@lang('services.sun')</th>
                <th>@lang('services.mon')</th>
                <th>@lang('services.tue')</th>
                <th>@lang('services.wed')</th>
                <th>@lang('services.thu')</th>
                <th>@lang('services.fri')</th>
                <th>@lang('services.service_created')</th>
                <th>@lang('clients.action')</th>
            </tr>
            </thead>
        </table>

网络:

  

绘制:5,记录总数:12,记录已过滤:12,数据:[,…]   {绘制:“ 5”,列:[{数据:“ name_en”,名称:“ name_en”,可搜索:“ true”,可排序:“ true”,…},…],…}   recordsFiltered:12   recordsTotal:12

排序正在工作,它返回新数据并更正。 更改长度(每页)有效。 但由于以下原因无法分页 recordsFiltered:12 记录总数:12 它应该是: recordsFiltered:5或10(每页) recordsTotal:12

原因何在?

  

更新:终于奏效了! ..我更新了jquery.datatables.min.js   从v1.10.7到v1.10.19,而无需降级Yajra Datatable或   升级jquery.datatables.min.css

1 个答案:

答案 0 :(得分:0)

终于成功了! ..我将jquery.datatables.min.js从v1.10.7更新到v1.10.19,而没有降级Yajra Datatable或升级jquery.datatables.min.css