Description: - DataTables is a powerful jQuery plugin for creating table listings and adding interactions to them. It provides searching, sorting and pagination without any configuration (usually used in admin panel's resources listing). - The backend support handled using "yajra/laravel-datatables-oracle" package. Backend Usage: - Create two functions in the resource controller, the first will return the view that contains the table. the second function responds to the ajax request coming from datatables ajax request (return the data to dill the table). -- example: public function index() // returns the view { return view('index'); } public function apiIndex() // responds to the ajax request { return DataTables::of(Model::all())->make(true); // use orderBy('sort') here in case you want to use the sort feature } // in case you use a sort feature public function sort{ $initial_sort = DB::select('select min(sort) as sort from {table} where id in('.implode(request('ids'), ',').')'); $i = $initial_sort[0]->sort; foreach (request('ids') as $id) { $data = Model::find($id); $data->sort = $i; $data->save(); $i++; } $msg['code'] = 1; $msg['message'] = 'Sorted successfully .'; return response()->json($msg); } // in case you use a delete multiple feature public function deleteMulti(Request $request) { foreach ($request->ids as $id) { Model::findOrFail($id)->delete(); } $msg['code'] = 1; $msg['message'] = 'Deleted successfully .'; return response()->json($msg); } - Note/ make sure to use "DataTables" class at the top of your controller Frontend Usage: - Create the table with id="data-table" without a body () as it will be filled by the datatable. - Add the attribute 'data-fill' to each element in the , and set the value to the attribute name you want to fill this column with (make sure to type the data-fill value exactly as returned from the backend). -- example: // for image column // for external links column // for internal links column (link from the database) // for input column // for sumitting input columns // for badges column
# Name Email Image Childs Childs Whatever Whatever "1,2,3" badge-success-fill="the word to type in the badge" // it's filled by the data-fill attribute by default badge-warning="same as above" badge-warning-fill="same as above" badge-danger="same as above" badge-danger-fill="same as above" badge-primary="same as above" badge-primary-fill="same as above" badge-info="same as above" badge-info-fill="same as above" badge-dark="same as above" badge-dark-fill="same as above" badge-light="same as above" badge-light-fill="same as above" badge-secondary="same as above" badge-secondary-fill="same as above" // note=> you can use badge-{type}="_others" to show this badge for non included values of the data-fill atribute in the other badges > Activation
- Include the 'datatables' blade file in the 'scripts' section at the end of your view. which contains the logic for datatables written in javascript. - you can pass options to the datatables script. -- example @section('scripts') @include('admin.ajax.datatables', [ 'ajax_url' => route('route.name'), => required 'options' => true, => add options column to the end of the table(default false) 'edit_row' => true, => set to false by default 'edit_url' => route('route.name', ['!id!']), => required when edit_row is true 'delete_row' => true, => set to false by default 'delete_url' => route('route.name', ['!id!']), => required when delete_row is true 'multi_delete' => true, => set to false by default 'multi_delete_url' => route('route.name', ['!id!']), => required when multi_delete_row is true 'sort' => true, => set to false by default 'sort_url' => route('route.name'), => required when sort is true 'extra_options' => [ [ 'url' => route('route.name', ['!id!']), 'title' => 'Title to show in the design', 'logo_class' => 'fa fa-edit' ], . . . ], 'actions' => [ [ 'column_name' => 'show_in_offer', 'positive_action_title' => 'show In Offer', 'positive_action_case' => 'no', 'positive_action_url' => route('route.name', ['!id!']), 'negative_action_title' => 'Hide From Offer', 'negative_action_case' => 'yes', 'negative_action_url' => route('route.name', ['!id!']) ], . . . ] ]) /* important notes: any url params passed to route function must be wrapped by "!" */ @endsection