yajra / laravel-datatables-editor

Laravel DataTables Editor Integration.
https://yajrabox.com/docs/laravel-datatables/editor-installation
MIT License
115 stars 14 forks source link

Integration of editor datatable with laravel 8 and latest yajra/laravel-datatables #60

Closed sheefa03 closed 5 months ago

sheefa03 commented 3 years ago

Can anyone please share an example of how to use editor with yajra datatable. I am unable to find the same in the documentation. Below I am sharing the code for datatable which I have used.

<?php

namespace App\DataTables;

use App\Models\BusinessSector;
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 BusinessSectorDataTable 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', 'businesssector.action');
    }

    /**
     * Get query source of dataTable.
     *
     * @param \App\Models\BusinessSector $model
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function query(BusinessSector $model)
    {
        return $model->newQuery();
    }

    /**
     * Optional method if you want to use html builder.
     *
     * @return \Yajra\DataTables\Html\Builder
     */
    public function html()
    {
        return $this->builder()
                    ->setTableId('businesssector-table')
                    ->columns($this->getColumns())
                    ->minifiedAjax()
                    ->dom('Bfrtip')
                    ->orderBy(0)
                    ->buttons(
                        Button::make('excel'),
                        Button::make('reload')
                    );
    }

    /**
     * Get columns.
     *
     * @return array
     */
    protected function getColumns()
    {
        return [
            'bsec_id',
            'bsec_sector',
            'bsec_display_name',
        ];
    }

    /**
     * Get filename for export.
     *
     * @return string
     */
    protected function filename()
    {
        return 'BusinessSector_' . date('YmdHis');
    }
}
yajra commented 3 years ago

Here is an example Users DataTable with Editor, this is an actual code in my project and just omitted the some complex stuffs.

DataTable

<?php

namespace Modules\Users\DataTables;

use App\Models\User;
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 UsersDataTable extends DataTable
{
    /**
     * Build DataTable class.
     *
     * @param  mixed  $query  Results from query() method.
     * @return \Yajra\DataTables\DataTableAbstract
     */
    public function dataTable($query)
    {
        return datatables()
            ->eloquent($query)
            ->setRowId('id');
    }

    /**
     * Get query source of dataTable.
     *
     * @param  \App\Models\User  $model
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function query(User $model)
    {
        return $model->newQuery()->select('users.*');
    }

    /**
     * Optional method if you want to use html builder.
     *
     * @return \Yajra\DataTables\Html\Builder
     * @throws \Exception
     */
    public function html()
    {
        return $this->builder()
            ->setTableId('users-table')
            ->columns($this->getColumns())
            ->select()
            ->minifiedAjax()
            ->orderBy(1)
            ->responsive(false)
            ->buttons([
                Button::make('create')->editor('editor'),
                Button::make('edit')->editor('editor'),
                Button::make('remove')->editor('editor'),
                Button::make('reload'),
            ])
            ->editors([
                Editor::make()
                    ->fields([
                        Fields\Text::make('name'),
                        Fields\Text::make('email'),
                        Fields\Password::make('password'),
                        Fields\Password::make('password_confirmation', 'Confirm Password'),
                    ]),
            ]);
    }

    /**
     * Get columns.
     *
     * @return array
     */
    protected function getColumns(): array
    {
        return [
            Column::checkbox(),
            Column::make('name'),
            Column::make('email'),
            Column::make('id'),
        ];
    }

    /**
     * Get filename for export.
     *
     * @return string
     */
    protected function filename(): string
    {
        return 'Users_'.date('YmdHis');
    }
}

Editor

<?php

namespace Modules\Users\DataTables;

use App\Actions\Fortify\PasswordValidationRules;
use App\Models\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Validation\Rule;
use Yajra\DataTables\DataTablesEditor;

class UsersDataTableEditor extends DataTablesEditor
{
    use PasswordValidationRules;

    protected $model = User::class;

    /**
     * Get create action validation rules.
     *
     * @return array
     */
    public function createRules(): array
    {
        return [
            'email' => 'required|email|unique:'.$this->resolveModel()->getTable(),
            'name' => 'required',
            'password' => $this->passwordRules(),
        ];
    }

    /**
     * Get edit action validation rules.
     *
     * @param  Model|User  $model
     * @return array
     */
    public function editRules(Model $model): array
    {
        return [
            'email' => 'sometimes|required|email|'.Rule::unique($model->getTable())->ignore($model->getKey()),
            'name' => 'sometimes|required',
            'password' => array_merge(['sometimes'], $this->passwordRules()),
        ];
    }

    /**
     * @param  Model|User  $model
     * @param  array  $data
     * @return array
     */
    public function updating(Model $model, array $data): array
    {
        if (empty(data_get($data, 'password'))) {
            unset($data['password']);
        }

        return $data;
    }

    /**
     * @param  Model|User  $model
     * @param  array  $data
     * @return array
     */
    public function saving(Model $model, array $data): array
    {
        if (data_get($data, 'password')) {
            data_set($data, 'password', bcrypt($data['password']));
        }

        return $data;
    }

    /**
     * Get remove action validation rules.
     *
     * @param  Model  $model
     * @return array
     */
    public function removeRules(Model $model): array
    {
        return [];
    }
}

Controller

<?php

namespace Modules\Users\Http\Controllers;

use Illuminate\Contracts\Support\Renderable;
use Illuminate\Routing\Controller;
use Modules\Users\DataTables\UsersDataTable;
use Modules\Users\DataTables\UsersDataTableEditor;

class UsersController extends Controller
{
    /**
     * @param  \Modules\Users\DataTables\UsersDataTable  $dataTable
     * @return mixed
     */
    public function index(UsersDataTable $dataTable)
    {
        return $dataTable->render('users::index');
    }

    /**
     * @param  \Modules\Users\DataTables\UsersDataTableEditor  $editor
     * @return \Illuminate\Http\JsonResponse|mixed
     * @throws \Yajra\DataTables\DataTablesEditorException
     */
    public function store(UsersDataTableEditor $editor)
    {
        return $editor->process(request());
    }
}
github-actions[bot] commented 6 months ago

This issue is stale because it has been open for 30 days with no activity. Remove stale label or comment or this will be closed in 7 days.

github-actions[bot] commented 5 months ago

This issue was closed because it has been inactive for 7 days since being marked as stale.