Last active
April 26, 2022 06:27
-
-
Save anlisha-maharjan/ec849fa732ae2cea0dea20778b1d55df to your computer and use it in GitHub Desktop.
Build Notice DataTable class with html builder.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\DataTables; | |
use App\Models\Notice; | |
use Yajra\DataTables\Html\Button; | |
use Yajra\DataTables\Html\Column; | |
use Yajra\DataTables\Html\Editor\Editor; | |
use Yajra\DataTables\Html\Editor\Fields; | |
use Yajra\DataTables\Services\DataTable; | |
class NoticeDataTable extends DataTable | |
{ | |
/** | |
* Build DataTable class. | |
* | |
* @param mixed $query Results from query() method. | |
* @return \Yajra\DataTables\DataTableAbstract | |
*/ | |
public function dataTable($query) | |
{ | |
return datatables() | |
->eloquent($query) | |
->addColumn('action', 'notice.action'); | |
} | |
/** | |
* Get query source of dataTable. | |
* | |
* @param \App\Models\Notice $model | |
* @return \Illuminate\Database\Eloquent\Builder | |
*/ | |
public function query(Notice $model) | |
{ | |
return $model->newQuery()->select('notice.*'); | |
} | |
/** | |
* Optional method if you want to use html builder. | |
* | |
* @return \Yajra\DataTables\Html\Builder | |
*/ | |
public function html() | |
{ | |
return $this->builder() | |
->setTableId('notice-data-table') | |
->columns($this->getColumns()) | |
->minifiedAjax(route('dashboard.notice')) | |
->dom('lfrtip'); | |
} | |
/** | |
* Get columns. | |
* | |
* @return array | |
*/ | |
protected function getColumns() | |
{ | |
return [ | |
Column::make('id'), | |
Column::make('name'), | |
Column::computed('action') | |
->exportable(false) | |
->printable(false) | |
->width(80) | |
->addClass('text-center') | |
]; | |
} | |
/** | |
* Get filename for export. | |
* | |
* @return string | |
*/ | |
protected function filename() | |
{ | |
return 'Notice_' . date('YmdHis'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment