Laravel-datatables: Render two datatables in same view by clicking different tab

Created on 25 Apr 2018  路  2Comments  路  Source: yajra/laravel-datatables

Hi all, I followed the solution on this post (https://github.com/yajra/laravel-datatables/issues/1202) and I achieved my two datatables working at the same time, but..
I don't want that are loaded both at the same time, when the general page is being loaded.

I want that to load the 2nd datatable when I click a specific tab/button.
I saw that in the page: https://datatables.yajrabox.com/services/two-datatables

function postsDataTables() {
    if (!$.fn.dataTable.isDataTable('#postsTable')) {
        $('#postsTable').DataTable({
            dom: 'Bfrtip',
            processing: true,
            serverSide: true,
            order: [[0, 'desc']],
            buttons: [
                'csv', 'excel', 'pdf', 'print', 'reset', 'reload'
            ],
            ajax: '/services/two-datatables/posts',
            columns: [
                {data: 'id', name: 'posts.id'},
                {data: 'title', name: 'posts.title'},
                {data: 'created_by', name: 'users.name', width: '110px'},
                {data: 'created_at', name: 'posts.created_at', width: '120px'},
                {data: 'updated_at', name: 'posts.updated_at', width: '120px'},
            ],
            order: [[0, 'desc']]
        });
    }
}

That consists basically to add a if statement wrapping the JS of datatable, but I want a solution through DataTable Service implementation, like the post I referenced before. Something like this:

function postsDataTables() {
    if (!$.fn.dataTable.isDataTable('#postsTable')) {

        {!! $secondDataTable->html()->scripts() !!}

    }
}

But this can't be done because the function "scripts()" print the tags itself.

I don't know if what I'm asking is very easy to solve, or I'll have to write manually the JavaScript of the datatable and dispense DataTable Service in the second table.

Thanks for your attention.

question

Most helpful comment

I think what you need is generateScripts?

function postsDataTables() {
    if (!$.fn.dataTable.isDataTable('#postsTable')) {

        {!! $secondDataTable->html()->generateScripts() !!}

    }
}

All 2 comments

I think what you need is generateScripts?

function postsDataTables() {
    if (!$.fn.dataTable.isDataTable('#postsTable')) {

        {!! $secondDataTable->html()->generateScripts() !!}

    }
}

Awesome! Are you a genious, @yajra . Thanks for create this great and useful package. 馃挴 馃憤

Was this page helpful?
0 / 5 - 0 ratings

Related issues

t0n1zz picture t0n1zz  路  3Comments

ahmadbadpey picture ahmadbadpey  路  3Comments

FilipeBorges1993 picture FilipeBorges1993  路  3Comments

SGarridoDev picture SGarridoDev  路  3Comments

vipin733 picture vipin733  路  3Comments