Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Are there any specific configurations needed for Yajra Datatables to handle multiple instances


Are there any specific configurations needed for Yajra Datatables to handle multiple instances


Yes, there are a few specific configurations needed for Yajra Datatables to handle multiple instances on a single view:

1. Create separate DataTable classes for each table you want to display, such as `UsersDataTable` and `ProductsDataTable`[1][2].

2. In your controller, pass the required DataTable instances to the view:

php
public function index(UsersDataTable $usersDataTable, ProductsDataTable $productsDataTable)
{
    return view('dashboard', [
        'usersDataTable' => $usersDataTable,
        'productsDataTable' => $productsDataTable,
    ]);
}

3. In your view, render each DataTable using the `@extends` and `@section` directives, specifying a unique table ID for each instance:

html
@extends('layouts.app')

@section('content')
    <div class="row">
        <div class="col-md-6">
            {!! $usersDataTable->table(['id' => 'users-table']) !!}
        </div>
        <div class="col-md-6">
            {!! $productsDataTable->table(['id' => 'products-table']) !!}
        </div>
    </div>
@endsection

@push('scripts')
    {!! $usersDataTable->scripts() !!}
    {!! $productsDataTable->scripts() !!}
@endpush

4. Customize the JavaScript to interact with each DataTable instance using the unique table IDs:

javascript
$(document).ready(function() {
    var usersTable = $('#users-table').DataTable();
    var productsTable = $('#products-table').DataTable();

    // Add event listeners or perform other actions on the tables
});

By following these steps and ensuring each DataTable instance has a unique table ID, you can successfully handle multiple instances of Yajra Datatables on a single Laravel view[2][3].

Citations:
[1] https://github.com/yajra/laravel-datatables-buttons/issues/91
[2] https://devs.keenthemes.com/question/multiple-datatables-in-one-view-controller-laravel-yajra-service
[3] https://github.com/yajra/laravel-datatables/issues/1320
[4] https://www.medianova.com/maximizing-web-performance-with-yajra-datatables/
[5] https://github.com/yajra/laravel-datatables/discussions/2599