首页 > 解决方案 > Laravel Yajra 上的分页 分页不起作用

问题描述

我正在开发一个使用 API 的项目,它包含大量数据,所以我需要一个数据表来管理所有数据。不幸的是,当我应用数据表时,分页不起作用,当我单击分页时,页面加载并显示第一页内容。有什么解决方案。

这是我的控制器代码

public function json()
{
    $token = session()->get('accessToken');
    $ch = curl_init('http://api-link');
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('x-access-token:' . $token . ''));
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $response = curl_exec($ch);
    curl_close($ch);
    $data_response = json_decode($response)->data;
    return Datatables::of($data_response)->make(true);
}

这是我的刀片代码

@section('content')
<div id="page-content" class="page-content">    
    <div id="page-content-scroll"><!--Enables this element to be scrolled -->   
        <div class="content">
            @component('layouts.topnav-admin')@endcomponent
            <a href="/tag/create" class="button button-red button-full button-rounded button-s uppercase ultrabold button-add">Tambah</a>
            <table class="table-borders-dark table-list" id="table_tag">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>Name</th>
                    </tr>
                </thead>
            </table>

        </div>
    </div>
</div>
@endsection

@section('script')
<script>

    $(document).ready(function () {
        $('#table_tag').DataTable({
            processing: true,
            serverSide: true,
            order: [[ 0, "desc" ]],
            ajax: {
                url: "tag/json",
                type: "GET",
            },
            columns: [
                {data: 'id', name: 'id', render:function(data, type, row){
                        return "<a href='/tag/edit/"+ row.id +"'>" + row.id + "</a>"},
                },
                {data: 'name', name: 'name', render:function(data, type, row){
                        return "<a href='/tag/edit/"+ row.id +"'>" + row.name + "</a>"},
                },
            ]
        });
    });

</script>
@endsection

这是用于响应数据

// 20191206112726
// http://localhost:8000/tag/json

{
    "draw": 0,
    "recordsTotal": 40,
    "recordsFiltered": 40,
    "data": [
        {
            "id": "16",
            "name": "abc"
        },
        {
            "id": "19",
            "name": "xyz"
        },
        {
            "id": "30",
            "name": "athletic"
        }
    ],
    "input": [

    ]
}

谢谢

标签: phplaraveldatatable

解决方案


推荐阅读