Skip to content

$.fn.dataTable.Editor is not a constructor #57

@vincent-treco

Description

@vincent-treco

Hello, I try to use editor datatable trial fot my work. I follow this tuto but I can't get the editor buttons to work. I got this error when i add the editor function in the html function on my ClientsDataTable : $.fn.dataTable.Editor is not a constructor

See my code

index.blade.twig :

@extends('dashboard.base')
@section('css')
    <link rel="stylesheet" href="{{ asset('css/editor.bootstrap4.css') }}">
    <link rel="stylesheet" href="{{ asset('css/buttons.bootstrap4.css') }}">
    <link rel="stylesheet" href="{{ asset('css/select.bootstrap4.css') }}">
    <link rel="stylesheet" href="{{ asset('css/editor.dataTables.css') }}">
@endsection
@section('content')
    {{ $dataTable->table() }}
@endsection

@section('javascript')
    <script src="{{ asset('js/jquery.dataTables.js') }}"></script>
    <script src="{{ asset('js/dataTables.select.js') }}"></script>
    <script src="{{ asset('js/select.bootstrap4.js') }}"></script>
    <script src="{{ mix('js/dataTables.editor.js') }}"></script>
    <script src="{{ asset('js/dataTables.bootstrap4.min.js') }}"></script>
    <script src="{{ asset('js/dataTables.buttons.js') }}"></script>
    <script src="{{ asset('js/buttons.bootstrap4.js') }}"></script>
    <script src="{{ asset('/vendor/datatables/buttons.server-side.js') }}"></script>
    {!! $dataTable->scripts() !!}
@endsection

ClientsDatatable :

<?php

namespace App\DataTables;

use App\Models\Client;
use DataTables\Editor\Field;
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 ClientsDataTable 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', 'clients.action')
            ->setRowId('id');
    }

    /**
     * Get query source of dataTable.
     *
     * @param \App\Models\Client $model
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function query(Client $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('clients-table')
            ->columns($this->getColumns())
            ->minifiedAjax()
            ->dom('Bfrtip')
            ->select(true)
            ->orderBy(0, 'ASC')
            ->rowId('id')
            ->serverSide(true)
            ->buttons(
                Button::make('add')->editor('editor')->text('Ajouter'),
                Button::make('edit')->editor('editor')->text('Modifier'),
                Button::make('remove')->editor('editor')->text('Supprimer'),
                Button::make('export')->text('Exporter'),
                Button::make('print')->text('Imprimer'),
//                Button::make('reset'),
                Button::make('reload')->text('Actualiser')
            )
            ->editor(
                Editor::make()
                    ->fields([
                        Fields\Text::make('cli_nom1')->label('Nom client 1'),
                        Fields\Text::make('cli_nom2'),
                        Fields\Text::make('cli_prenom1')->label('Prenom client 1'),
                        Fields\Text::make('cli_prenom2')
                    ])
            );
    }

    /**
     * Get columns.
     *
     * @return array
     */
    protected function getColumns()
    {
        return [
            Column::make('id')->name('id'),
            Column::make('cli_nom1'),
//            Column::make('cli_nom2'),
            Column::make('cli_prenom1'),
//            Column::make('cli_prenom2')
        ];
    }

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

ClientDataTableEditor.php :

<?php

namespace App\DataTables;

use App\Models\Client;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Validation\Rule;
use Yajra\DataTables\DataTablesEditor;

class ClientDataTableEditor extends DataTablesEditor
{
    protected $model = Client::class;

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

    /**
     * Get edit action validation rules.
     *
     * @param Model $model
     * @return array
     */
    public function editRules(Model $model)
    {
        return [
            'cli_nom1' => 'required',
            'cli_prenom1'  => 'required',
        ];
    }

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

I installed all dependencies buth this code doesn't want to work !
If someone can help me, thank you

  • Operating System : Ubuntu 20.04
  • PHP Version : 7.4
  • Laravel Version : 8.0
  • Laravel-Datatables Version 9.17.1

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions