Last active
March 15, 2018 10:52
-
-
Save chriscalip/46ee6a32264eb6184b5800e0e1bcd8a7 to your computer and use it in GitHub Desktop.
Rough draft example for adding button "publish" https://github.com/yajra/laravel-datatables-editor/issues/1
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\Asset; | |
use Yajra\DataTables\Services\DataTable; | |
class AssetsDataTable extends DataTable | |
{ | |
/** | |
* Build DataTable class. | |
* | |
* @param mixed $query Results from query() method. | |
* @return \Yajra\DataTables\DataTableAbstract | |
*/ | |
public function dataTable($query) | |
{ | |
return datatables($query)->setRowId('id'); | |
} | |
/** | |
* Get query source of dataTable. | |
* | |
* @param \App\Asset $model | |
* @return \Illuminate\Database\Eloquent\Builder | |
*/ | |
public function query(Asset $model) | |
{ | |
return $model->newQuery()->select('id', 'name'); | |
} | |
/** | |
* Optional method if you want to use html builder. | |
* | |
* @return \Yajra\DataTables\Html\Builder | |
*/ | |
public function html() | |
{ | |
return $this->builder() | |
->columns($this->getColumns()) | |
->minifiedAjax() | |
->parameters([ | |
'dom' => 'Bfrtip', | |
'order' => [1, 'asc'], | |
'select' => [ | |
'style' => 'os', | |
'selector' => 'td:first-child', | |
], | |
'buttons' => [ | |
['extend' => 'create', 'editor' => 'editor'], | |
['extend' => 'edit', 'editor' => 'editor'], | |
['extend' => 'remove', 'editor' => 'editor'], | |
[ | |
'extend' => 'selected', | |
'text' => 'Publish', | |
'action' => "function ( e, dt, node, config ) { | |
var formData = { | |
action: 'publish', | |
data: $.map(dt.rows({selected: true}).data(), function (o) { | |
return o.DT_RowId; | |
}) | |
}; | |
$.post(editor.s.ajax, formData).then(function(response) { dt.draw(false); }); | |
}", | |
], | |
] | |
]); | |
} | |
/** | |
* Get columns. | |
* | |
* @return array | |
*/ | |
protected function getColumns() | |
{ | |
return [ | |
[ | |
'data' => null, | |
'defaultContent' => '', | |
'className' => 'select-checkbox', | |
'title' => '', | |
'orderable' => false, | |
'searchable' => false | |
], | |
'id', | |
'name', | |
]; | |
} | |
/** | |
* Get filename for export. | |
* | |
* @return string | |
*/ | |
protected function filename() | |
{ | |
return 'assets_' . time(); | |
} | |
} |
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\Asset; | |
use Illuminate\Validation\Rule; | |
use Yajra\DataTables\DataTablesEditor; | |
use Illuminate\Database\Eloquent\Model; | |
use Illuminate\Http\Request; | |
class AssetsDataTablesEditor extends DataTablesEditor | |
{ | |
protected $model = Asset::class; | |
protected $actions = ['create', 'edit', 'remove', 'publish']; | |
/** | |
* Get create action validation rules. | |
* | |
* @return array | |
*/ | |
public function createRules() | |
{ | |
return [ | |
'name' => 'required', | |
]; | |
} | |
/** | |
* Get edit action validation rules. | |
* | |
* @param Model $model | |
* @return array | |
*/ | |
public function editRules(Model $model) | |
{ | |
return [ | |
'name' => 'sometimes|required', | |
]; | |
} | |
/** | |
* Get remove action validation rules. | |
* | |
* @param Model $model | |
* @return array | |
*/ | |
public function removeRules(Model $model) | |
{ | |
return []; | |
} | |
public function creating(Model $model, array $data) | |
{ | |
return $data; | |
} | |
public function updating(Model $model, array $data) | |
{ | |
return $data; | |
} | |
/** | |
* Process publish action request. | |
* | |
* @param Request $request | |
* @return JsonResponse | |
*/ | |
public function publish(Request $request) | |
{ | |
$affected = []; | |
$errors = []; | |
$assetIds = array_map('intval', $request->get('data')); | |
$assetIds = array_filter($assetIds); | |
if (empty($assetIds)) { | |
$errors[] = 'empty data'; | |
return $this->toJson($assetIds, $errors); | |
} | |
$updated = Asset::whereIn('id', $assetIds)->update(['status' => 1]); | |
if (empty($updated)) { | |
$errors[] = 'data not updated'; | |
return $this->toJson($affected, $errors); | |
} | |
$affected[] = Asset::whereIn('id', $assetIds)->get(); | |
return $this->toJson($affected, $errors); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for sharing the gist. BTW, you can remove
updating
andcreating
methods as it is optional and only required if you want to modify the data before saving. 👍