Laravel数据表未显示

时间:2019-12-17 06:38:28

标签: php json ajax laravel datatable

我在laravel 5.8中使用yajra laravel datatable软件包。我认为数据表未显示。我在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');

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

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的新手。感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

尝试在标头中添加令牌

<meta name="csrf-token" content="{{ csrf_token() }}" />

然后在您的ajaxSetup或数据中添加令牌

$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});

还更改getAppointmentData函数。

public function getAppointmentData(Request $request)
    {
        if ($request->ajax())
        {
            $getData = DB::table('jobs')->select('id', 'date', 'time', 
            'payment_status', 'amount');
            return DataTables::of($getData)->make(true);
        } 
    }

答案 1 :(得分:0)

进行如下更改:
希望对您有帮助

路由文件:

ListView.OnItemSelectedListener onItemSelectedListener = new ListView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            MenuAdapter menuAdapter = (MenuAdapter)((WMSListBoxUI)adapterView).getAdapter();
            if (menuAdapter != null) {
                menuAdapter.setSelection(i);                    
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    };
    ListView.OnItemClickListener listViewOnItemClick = new ListView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapter, View arg1, int position, long id) {
            MenuAdapter menuAdapter = (MenuAdapter)((WMSListBoxUI)adapter).getAdapter();
            if (menuAdapter != null) {
                menuAdapter.setSelection(position);                    
            }

        }
    };

    this.setOnItemClickListener(listViewOnItemClick);
    this.setOnItemSelectedListener(onItemSelectedListener);

HTML代码:
将此放在您的头部

Route::get('/appointments','NavigationController@Appointments')->name('appointments');


这是你的桌子:

<meta name="csrf-token" content="{{ csrf_token() }}">

控制器代码:

<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>

Ajax代码:

public function Appointments(Request $request)
    {
        if ($request->ajax()) {
            $data =  DB::table('jobs')->select('id', 'date', 'time', 'payment_status', 'amount')->get();
            return Datatables::of($data)
                    ->addIndexColumn()
                    ->make(true);
        }

        return view('admin.show_appointments');
    }

希望对您有帮助