@@ -5,133 +5,186 @@ See [DataTables Editor](https://editor.datatables.net/purchase/index) for detail
55
66## Pre-requisites
77
8- This tutorial requires https://yajrabox.com/docs/laravel-datatables/master /quick-starter .
8+ This tutorial requires https://yajrabox.com/docs/laravel-datatables/10.0 /quick-starter .
99
1010## Install DataTables Editor assets.
1111
12- yarn add datatables.net-editor datatables.net-editor-bs4 datatables.net-select-bs4
12+ ``` sh
13+ npm i datatables.net-editor datatables.net-editor-bs5
14+ ```
1315
1416## Editor License
1517
1618Copy and rename your ` Editor.XX.zip ` to ` Editor.zip ` and move it to project folder.
1719
1820## Register postinstall script to package.json
1921
22+ ``` json
2023 "scripts" : {
21- "postinstall ": "node ./node_modules/datatables.net-editor/install.js ./Editor.zip ",
22- "dev ": "npm run development ",
23- .....
24+ "dev " : " vite " ,
25+ "build " : " vite build " ,
26+ "postinstall" : " node node_modules/datatables.net-editor/install.js ./Editor.zip "
2427 },
28+ ```
2529
26- ## Register editor script on ` resources/js/bootstrap.js `
27-
28- try {
29- window.Popper = require('popper.js').default;
30- window.$ = window.jQuery = require('jquery');
30+ ## Register editor script on ` resources/js/app.js `
3131
32- require('bootstrap');
33- require('datatables.net-bs4');
34- require('datatables.net-buttons-bs4');
35- require('datatables.net-select-bs4');
36- require('datatables.net-editor-bs4');
37- } catch (e) {}
32+ ``` js
33+ import ' ./bootstrap' ;
34+ import ' laravel-datatables-vite' ;
3835
36+ import " datatables.net-editor" ;
37+ import Editor from " datatables.net-editor-bs5" ;
38+ Editor (window , $);
39+ ```
3940
4041## Add editor styles on ` resources/sass/app.scss ` .
4142
42- @import "~datatables.net-select-bs4/css/select.bootstrap4.css";
43- @import "~datatables.net-editor-bs4/css/editor.bootstrap4.css";
44-
45- ## Recompile assets.
43+ ``` css
44+ // Fonts
45+ @import url (' https://fonts.bunny.net/css?family=Nunito' );
4646
47- yarn
48- yarn watch / dev / prod
47+ // Variables
48+ @import ' variables ' ;
4949
50+ // Bootstrap
51+ @import ' bootstrap/scss/bootstrap' ;
5052
51- ## Update UsersDataTable and register the Editor buttons.
52-
53- > Note: CREATE button is in conflict with ` buttons.server-side.js ` . You need to remove the create button or rename it to something else like ` add ` button.
54-
55- DataTable.ext.buttons.add = {
56- className: 'buttons-add',
53+ // DataTables
54+ @import ' bootstrap-icons/font/bootstrap-icons.css' ;
55+ @import " datatables.net-bs5/css/dataTables.bootstrap5.css" ;
56+ @import " datatables.net-buttons-bs5/css/buttons.bootstrap5.css" ;
57+ @import " datatables.net-editor-bs5/css/editor.bootstrap5.css" ;
58+ @import ' datatables.net-select-bs5/css/select.bootstrap5.css' ;
59+ ```
5760
58- text: function (dt) {
59- return '<i class="fa fa-plus"></i> ' + dt.i18n('buttons.add', 'Create');
60- },
61+ ## Recompile assets.
6162
62- action: function (e, dt, button, config) {
63- window.location = window.location.href.replace(/\/+$/, "") + '/create';
64- }
65- };
63+ ``` sh
64+ npm run dev
65+ ```
6666
6767### UsersDataTable.php
6868
6969Create a new editor instance and add some fields for name and email.
7070
7171``` php
72+ namespace App\DataTables;
73+
74+ use App\Models\User;
75+ use Illuminate\Database\Eloquent\Builder as QueryBuilder;
76+ use Yajra\DataTables\EloquentDataTable;
77+ use Yajra\DataTables\Html\Builder as HtmlBuilder;
78+ use Yajra\DataTables\Html\Button;
79+ use Yajra\DataTables\Html\Column;
7280use Yajra\DataTables\Html\Editor\Editor;
7381use Yajra\DataTables\Html\Editor\Fields;
82+ use Yajra\DataTables\Services\DataTable;
7483
75- ...
84+ class UsersDataTable extends DataTable
85+ {
7686 /**
7787 * Build DataTable class.
7888 *
79- * @param mixed $query Results from query() method.
80- * @return \Yajra\DataTables\DataTableAbstract
89+ * @param QueryBuilder $query Results from query() method.
90+ * @return \Yajra\DataTables\EloquentDataTable
91+ */
92+ public function dataTable(QueryBuilder $query): EloquentDataTable
93+ {
94+ return (new EloquentDataTable($query))
95+ ->addColumn('action', 'users.action')
96+ ->setRowId('id');
97+ }
98+
99+ /**
100+ * Get query source of dataTable.
101+ *
102+ * @param \App\Models\User $model
103+ * @return \Illuminate\Database\Eloquent\Builder
81104 */
82- public function dataTable($query)
105+ public function query(User $model): QueryBuilder
83106 {
84- return datatables()
85- ->eloquent($query)
86- ->setRowId('id') // Set the RowID
87- ...
107+ return $model->newQuery();
88108 }
89109
90- public function html()
110+ /**
111+ * Optional method if you want to use html builder.
112+ *
113+ * @return \Yajra\DataTables\Html\Builder
114+ */
115+ public function html(): HtmlBuilder
91116 {
92117 return $this->builder()
93118 ->setTableId('users-table')
94119 ->columns($this->getColumns())
95120 ->minifiedAjax()
96- ->dom('Bfrtip')
97121 ->orderBy(1)
98- ->buttons(
122+ ->selectStyleSingle()
123+ ->editors([
124+ Editor::make()
125+ ->fields([
126+ Fields\Text::make('name'),
127+ Fields\Text::make('email'),
128+ ]),
129+ ])
130+ ->buttons([
99131 Button::make('create')->editor('editor'),
100132 Button::make('edit')->editor('editor'),
101133 Button::make('remove')->editor('editor'),
102- Button::make('export'),
134+ Button::make('excel'),
135+ Button::make('csv'),
136+ Button::make('pdf'),
103137 Button::make('print'),
104138 Button::make('reset'),
105- Button::make('reload')
106- )
107- ->editor(
108- Editor::make()
109- ->fields([
110- Fields\Text::make('name'),
111- Fields\Text::make('email'),
112- Fields\Password::make('password'),
113- ])
114- );
139+ Button::make('reload'),
140+ ]);
141+ }
142+
143+ /**
144+ * Get the dataTable columns definition.
145+ *
146+ * @return array
147+ */
148+ public function getColumns(): array
149+ {
150+ return [
151+ Column::make('id'),
152+ Column::make('name'),
153+ Column::make('email'),
154+ Column::make('created_at'),
155+ Column::make('updated_at'),
156+ ];
157+ }
158+
159+ /**
160+ * Get filename for export.
161+ *
162+ * @return string
163+ */
164+ protected function filename(): string
165+ {
166+ return 'Users_'.date('YmdHis');
115167 }
168+ }
116169```
117170
118171## Create Editor Class to handle CRUD actions.
119172
120- ```
173+ ``` sh
121174php artisan datatables:editor Users
122175```
123176
124177## Register Editor Route
125178
126179Edit ` routes/web.php ` and register the store user route.
127180
128- ```
181+ ``` php
129182Route::post('/users', 'UsersController@store')->name('users.store');
130183```
131184
132185## Update users controller
133186
134- ```
187+ ``` php
135188namespace App\Http\Controllers;
136189
137190use Illuminate\Http\Request;
0 commit comments