Laravel 数据表未显示

我正在使用带有 laravel 5.8 的 yajra laravel 数据表包。数据表未显示在我的视图中。我在 jquery 之后添加了数据表。我无法弄清楚问题出在哪里。帮我解决这个问题。谢谢。


数据表脚本:


$(document).ready( function () {

    $('#appointment-datatable').DataTable({

        processing: true,

        serverSide: true,

        ajax: '/get_appointment_data',

        columns: 

        [

            {

                data: 'id', 

                name: 'id'

            },

            {

                data: 'date', 

                name: 'date'

            },

            {

                data: 'time', 

                name: 'time'

            },

            {

                data: 'payment_status', 

                name: 'payment_status'

            }

        ]

    })

} );

控制器代码:


    public function getAppointmentData(Request $request)

    {

        if ($request->ajax())

        {

            $getData = DB::table('jobs')->select('id', 'date', 'time', 'payment_status', 'amount')->get();

            $datatable = DataTables::of($json)->make(true);

            return $datatable;

        } 

    }

    public function ShowAppointments(Request $request)

    {

        return view('admin.show_appointments');

    }

路线:


Route::get('/show_appointments', 'NavigationController@ShowAppointments')->name('show_appointments');

Route::get('/get_appointment_data', 'NavigationController@getAppointmentData');


我想在 admin.show_appointments 视图中显示数据表返回 ShowAppointments 方法


视图中的 HTML:


        <table class="table" id="appointment-datatable">

            <thead>

              <tr>

                <th scope="col">id</th>

                <th scope="col">date</th>

                <th scope="col">time</th>

                <th scope="col">payment_status</th>

                <th scope="col">amount</th>

              </tr>

            </thead>

       </table>


我以前从未使用过数据表,而且我是 laravel 的新手。感谢你的帮助

一只斗牛犬
浏览 211回答 2
2回答

拉风的咖菲猫

像这样进行更改:希望它会帮助你路线文件:Route::get('/appointments','NavigationController@Appointments')->name('appointments');Html 代码:把它放在你的 head 部分<meta name="csrf-token" content="{{ csrf_token() }}">这是你的桌子:<table class="table" id="appointment-datatable">&nbsp; &nbsp; <thead>&nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; <th scope="col">id</th>&nbsp; &nbsp; &nbsp; &nbsp; <th scope="col">date</th>&nbsp; &nbsp; &nbsp; &nbsp; <th scope="col">time</th>&nbsp; &nbsp; &nbsp; &nbsp; <th scope="col">payment_status</th>&nbsp; &nbsp; &nbsp; &nbsp; <th scope="col">amount</th>&nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; </thead></table>控制器代码:public function Appointments(Request $request)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if ($request->ajax()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $data =&nbsp; DB::table('jobs')->select('id', 'date', 'time', 'payment_status', 'amount')->get();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Datatables::of($data)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->addIndexColumn()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->make(true);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return view('admin.show_appointments');&nbsp; &nbsp; }阿贾克斯代码:$(function () {&nbsp; &nbsp; $('#appointment-datatable').DataTable({&nbsp; &nbsp; &nbsp; &nbsp; processing: true,&nbsp; &nbsp; &nbsp; &nbsp; serverSide: true,&nbsp; &nbsp; &nbsp; &nbsp; ajax: "{{ route('appointments') }}",&nbsp; &nbsp; &nbsp; &nbsp; columns: [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {data: 'id', name: 'id'},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {data: 'date', name: 'date'},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {data: 'time', name: 'time'},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {data: 'payment_status', name: 'payment_status'},&nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; });&nbsp; });我希望它会帮助你

慕盖茨4494581

尝试在标题中添加令牌<meta name="csrf-token" content="{{ csrf_token() }}" />并在您的 ajaxSetup 或数据中添加令牌$.ajaxSetup({&nbsp; &nbsp; headers: {&nbsp; &nbsp; &nbsp; &nbsp; 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')&nbsp; &nbsp; }});还要更改 getAppointmentData 函数。public function getAppointmentData(Request $request)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if ($request->ajax())&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $getData = DB::table('jobs')->select('id', 'date', 'time',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'payment_status', 'amount');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return DataTables::of($getData)->make(true);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP