diff --git a/src/Generators/Scaffold/ControllerGenerator.php b/src/Generators/Scaffold/ControllerGenerator.php index e4bb6a234..b5fa11360 100644 --- a/src/Generators/Scaffold/ControllerGenerator.php +++ b/src/Generators/Scaffold/ControllerGenerator.php @@ -14,10 +14,14 @@ class ControllerGenerator /** @var string */ private $path; + /** @var string */ + private $templateType; + public function __construct(CommandData $commandData) { $this->commandData = $commandData; $this->path = $commandData->config->pathController; + $this->templateType = config('infyom.laravel_generator.path.templates', 'core-templates'); } public function generate() @@ -54,10 +58,30 @@ private function generateDataTable() $templateData = TemplateUtil::fillTemplate($this->commandData->dynamicVars, $templateData); + $headerFieldTemplate = TemplateUtil::getTemplate('scaffold.views.datatable_column', $this->templateType); + + $headerFields = []; + + foreach ($this->commandData->inputFields as $field) { + if (!$field['inIndex']) { + continue; + } + $headerFields[] = $fieldTemplate = TemplateUtil::fillTemplateWithFieldData( + $this->commandData->dynamicVars, + $this->commandData->fieldNamesMapping, + $headerFieldTemplate, + $field + ); + } + $path = $this->commandData->config->pathDataTables; $fileName = $this->commandData->modelName.'DataTable.php'; + $fields = implode(",".infy_nl_tab(1, 3), $headerFields); + + $templateData = str_replace('$DATATABLE_COLUMNS$', $fields, $templateData); + FileUtil::createFile($path, $fileName, $templateData); $this->commandData->commandComment("\n$fileName created: "); diff --git a/src/Generators/Scaffold/ViewGenerator.php b/src/Generators/Scaffold/ViewGenerator.php index 4caf664dc..a0a97255c 100644 --- a/src/Generators/Scaffold/ViewGenerator.php +++ b/src/Generators/Scaffold/ViewGenerator.php @@ -116,30 +116,7 @@ private function generateDataTableBody() { $templateData = TemplateUtil::getTemplate('scaffold.views.datatable_body', $this->templateType); - $templateData = TemplateUtil::fillTemplate($this->commandData->dynamicVars, $templateData); - - $templateData = str_replace('$FIELD_HEADERS$', $this->generateTableHeaderFields(), $templateData); - - $cellFieldTemplate = TemplateUtil::getTemplate('scaffold.views.datatable_column', $this->templateType); - - $tableBodyFields = []; - - foreach ($this->commandData->inputFields as $field) { - if (!$field['inIndex']) { - continue; - } - - $tableBodyFields[] = TemplateUtil::fillTemplateWithFieldData( - $this->commandData->dynamicVars, - $this->commandData->fieldNamesMapping, - $cellFieldTemplate, - $field - ); - } - - $tableBodyFields = implode(",".infy_nl_tab(1, 5), $tableBodyFields); - - return str_replace('$DATATABLE_COLUMNS$', $tableBodyFields, $templateData); + return TemplateUtil::fillTemplate($this->commandData->dynamicVars, $templateData); } private function generateIndex() diff --git a/templates/scaffold/datatable.stub b/templates/scaffold/datatable.stub index b05f27709..556562f68 100644 --- a/templates/scaffold/datatable.stub +++ b/templates/scaffold/datatable.stub @@ -2,7 +2,7 @@ namespace $NAMESPACE_DATATABLES$; -use App\Models\$MODEL_NAME$; +use $NAMESPACE_MODEL$\$MODEL_NAME$; use Yajra\Datatables\Services\DataTable; class $MODEL_NAME$DataTable extends DataTable @@ -15,7 +15,21 @@ class $MODEL_NAME$DataTable extends DataTable { return $this->datatables ->eloquent($this->query()) - ->addColumn('action', 'path.to.action.view') + ->addColumn('actions', function ($data) { + return ' + ' . Form::open(['route' => ['$MODEL_NAME_PLURAL_CAMEL$.destroy', $data->id], 'method' => 'delete']) . ' +
+ ' . Form::close() . ' + '; + }) ->make(true); } @@ -39,10 +53,30 @@ class $MODEL_NAME$DataTable extends DataTable public function html() { return $this->builder() - ->columns($this->getColumns()) - ->ajax('') - ->addAction(['width' => '80px']) - ->parameters($this->getBuilderParameters()); + ->columns(array_merge( + $this->getColumns(), + [ + 'actions' => [ + 'orderable' => false, + 'searchable' => false, + 'printable' => false, + 'exportable' => false + ] + ] + )) + ->parameters([ + 'dom' => 'Bfrtip', + 'scrollX' => true, + 'buttons' => [ + 'create', + 'csv', + 'excel', + 'pdf', + 'print', + 'reset', + 'reload' + ], + ]); } /** @@ -53,10 +87,7 @@ class $MODEL_NAME$DataTable extends DataTable private function getColumns() { return [ - 'id', - // add your columns - 'created_at', - 'updated_at', + $DATATABLE_COLUMNS$ ]; } @@ -67,6 +98,6 @@ class $MODEL_NAME$DataTable extends DataTable */ protected function filename() { - return '$$MODEL_NAME_PLURAL_CAMEL$'; + return '$MODEL_NAME_PLURAL_CAMEL$'; } } \ No newline at end of file