Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Render two datatables in same view by clicking different tab #1723

Closed
manelsanz opened this issue Apr 25, 2018 · 4 comments
Closed

Render two datatables in same view by clicking different tab #1723

manelsanz opened this issue Apr 25, 2018 · 4 comments

Comments

@manelsanz
Copy link

manelsanz commented Apr 25, 2018

Hi all, I followed the solution on this post (#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 <script></script> 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.

@yajra
Copy link
Owner

yajra commented Apr 28, 2018

I think what you need is generateScripts?

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

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

    }
}

@yajra yajra added the question label Apr 28, 2018
@manelsanz
Copy link
Author

Awesome! Are you a genious, @yajra . Thanks for create this great and useful package. 💯 👍

@github-actions
Copy link

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 github-actions bot added the stale label Sep 11, 2023
@github-actions
Copy link

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

@github-actions github-actions bot locked and limited conversation to collaborators Oct 4, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants