@@ -23,72 +23,84 @@ This will create a `PostsDataTable` class in the `app\DataTables` directory.
2323``` php
2424namespace App\DataTables;
2525
26- use App\User;
26+ use App\Models\Post;
27+ use Illuminate\Database\Eloquent\Builder as QueryBuilder;
28+ use Yajra\DataTables\EloquentDataTable;
29+ use Yajra\DataTables\Html\Builder as HtmlBuilder;
30+ use Yajra\DataTables\Html\Button;
31+ use Yajra\DataTables\Html\Column;
32+ use Yajra\DataTables\Html\Editor\Editor;
33+ use Yajra\DataTables\Html\Editor\Fields;
2734use Yajra\DataTables\Services\DataTable;
2835
2936class PostsDataTable extends DataTable
3037{
3138 /**
32- * Build DataTable class.
39+ * Build the DataTable class.
3340 *
34- * @return \Yajra\DataTables\DataTableAbstract
41+ * @param QueryBuilder $query Results from query() method.
3542 */
36- public function dataTable()
43+ public function dataTable(QueryBuilder $query): EloquentDataTable
3744 {
38- return $this->datatables
39- ->eloquent($this->query() )
40- ->addColumn('action', 'path.to.action.view ');
45+ return (new EloquentDataTable($query))
46+ ->addColumn('action', 'posts.action' )
47+ ->setRowId('id ');
4148 }
4249
4350 /**
44- * Get the query object to be processed by dataTables.
45- *
46- * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder|\Illuminate\Support\Collection
51+ * Get the query source of dataTable.
4752 */
48- public function query()
53+ public function query(Post $model): QueryBuilder
4954 {
50- $query = User::query();
51-
52- return $this->applyScopes($query);
55+ return $model->newQuery();
5356 }
5457
5558 /**
56- * Optional method if you want to use html builder.
57- *
58- * @return \Yajra\DataTables\Html\Builder
59+ * Optional method if you want to use the html builder.
5960 */
60- public function html()
61+ public function html(): HtmlBuilder
6162 {
6263 return $this->builder()
64+ ->setTableId('posts-table')
6365 ->columns($this->getColumns())
64- ->ajax('')
65- ->addAction(['width' => '80px'])
66- ->parameters($this->getBuilderParameters());
66+ ->minifiedAjax()
67+ //->dom('Bfrtip')
68+ ->orderBy(1)
69+ ->selectStyleSingle()
70+ ->buttons([
71+ Button::make('excel'),
72+ Button::make('csv'),
73+ Button::make('pdf'),
74+ Button::make('print'),
75+ Button::make('reset'),
76+ Button::make('reload')
77+ ]);
6778 }
6879
6980 /**
70- * Get columns.
71- *
72- * @return array
81+ * Get the dataTable columns definition.
7382 */
74- protected function getColumns()
83+ public function getColumns(): array
7584 {
7685 return [
77- 'id',
78- // add your columns
79- 'created_at',
80- 'updated_at',
86+ Column::computed('action')
87+ ->exportable(false)
88+ ->printable(false)
89+ ->width(60)
90+ ->addClass('text-center'),
91+ Column::make('id'),
92+ Column::make('add your columns'),
93+ Column::make('created_at'),
94+ Column::make('updated_at'),
8195 ];
8296 }
8397
8498 /**
85- * Get filename for export.
86- *
87- * @return string
99+ * Get the filename for export.
88100 */
89- protected function filename()
101+ protected function filename(): string
90102 {
91- return 'posts_ ' . time( );
103+ return 'Posts_ ' . date('YmdHis' );
92104 }
93105}
94106```
@@ -104,81 +116,6 @@ php artisan datatables:make Posts --model
104116This will generate an ` App\DataTables\PostsDataTable ` class that uses ` App\Post ` as the base model for our query.
105117The exported filename will also be set to ` posts_(timestamp) ` .
106118
107- ``` php
108- <?php
109-
110- namespace App\DataTables;
111-
112- use App\Post;
113- use Yajra\DataTables\Services\DataTable;
114-
115- class PostsDataTable extends DataTable
116- {
117- /**
118- * Build DataTable class.
119- *
120- * @return \Yajra\DataTables\DataTableAbstract
121- */
122- public function dataTable()
123- {
124- return $this->datatables
125- ->eloquent($this->query())
126- ->addColumn('action', 'path.to.action.view');
127- }
128-
129- /**
130- * Get the query object to be processed by dataTables.
131- *
132- * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder|\Illuminate\Support\Collection
133- */
134- public function query()
135- {
136- $query = Post::query();
137-
138- return $this->applyScopes($query);
139- }
140-
141- /**
142- * Optional method if you want to use html builder.
143- *
144- * @return \Yajra\DataTables\Html\Builder
145- */
146- public function html()
147- {
148- return $this->builder()
149- ->columns($this->getColumns())
150- ->ajax('')
151- ->addAction(['width' => '80px'])
152- ->parameters($this->getBuilderParameters());
153- }
154-
155- /**
156- * Get columns.
157- *
158- * @return array
159- */
160- protected function getColumns()
161- {
162- return [
163- 'id',
164- // add your columns
165- 'created_at',
166- 'updated_at',
167- ];
168- }
169-
170- /**
171- * Get filename for export.
172- *
173- * @return string
174- */
175- protected function filename()
176- {
177- return 'posts_' . time();
178- }
179- }
180- ```
181-
182119
183120### Model Namespace Option
184121
0 commit comments