yajra / laravel-datatables

jQuery DataTables API for Laravel
https://yajrabox.com/docs/laravel-datatables
MIT License
4.74k stars 861 forks source link

how to add a custom button? #2988

Closed Not-Net closed 1 year ago

Not-Net commented 1 year ago

Summary of problem

This issue is related to this issue #2973

I did follow the quick starter in the docs to create users datatable and everything works perfectly fine https://yajrabox.com/docs/laravel-datatables/10.0/quick-starter

i want add a custom Button that redirect the user to the create user page.

    protected $actions = ['print', 'excel', 'myCustomAction'];

    public function html(): HtmlBuilder
    {
        return $this->builder()
                    ->setTableId('users-table')
                    ->columns($this->getColumns())
                    ->minifiedAjax()
                    ->orderBy(1)
                    ->selectStyleSingle()
                    ->parameters([
                        'dom'          => 'Bfrtip',
                        'buttons'      => ['excel', 'print', 'myCustomAction'],
                    ]);
    }

    public function myCustomAction(){

    }

and i added the js counterpart as you told me in this issue #2973

@extends('layouts.main')

@section('content')
    <div class="card">
        <div class="card-body">
            {{ $dataTable->table() }}
        </div>
    </div>
@endsection

@push('scripts')
    {{ $dataTable->scripts(attributes: ['type' => 'module']) }}

    <script>
        $.fn.dataTable.ext.buttons.myCustomAction = {
            name: 'add',
            className: 'buttons-add btn-success',
            text: '<i class="fa fa-plus"></i> New',
            action: function (e, dt, button, config) {
                alert('myCustomAction');
            }
        };
    </script>
@endpush

now i am getting this console error: Uncaught TypeError: Cannot read properties of undefined (reading 'ext') i rly dont know what i am missing right here...

System details

yajra commented 1 year ago

See https://github.com/yajra/laravel-datatables-vite/blob/main/js/buttons/add.js for ref.

github-actions[bot] commented 1 year 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 1 year ago

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