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

How to initialize datatable with existing array? #9681

Open
ainsleybernard opened this issue Oct 12, 2023 · 3 comments
Open

How to initialize datatable with existing array? #9681

ainsleybernard opened this issue Oct 12, 2023 · 3 comments

Comments

@ainsleybernard
Copy link

Hello,

Is it possible to set the data attribute on the data-wb-tables with an existing array?

@wewhite
Copy link
Contributor

wewhite commented Oct 12, 2023

Need a little more information, maybe a code snippet of what you have, and what you are trying to accomplish.
I can guess three or four different scenarios.

1 - Setting data attribute in the data-wb-tables array columns:

<table id="personTable" class="table wb-tables" data-wb-tables='{ 
                        "searching" : false,
                        "ordering"  : false,
                        "paging"    : false,
                        "bInfo"     : false,
                        "responsive" : true,
                        "processing": true,
                        "serverSide": true,
                        "ajax"      : {"url":"{!! route("otherperson.people.all") !!}", "dataSrc":"data"},
                        "cache": false,
                        "columns": [       
                            {"data": "id", "name":"id", "visible": false},                     
                            {"data": "firstName", "name":"firstName"},
                            {"data": "lastName",  "name": "lastName"},
                            {"data": "association",  "name": "association"},
                            {"data": "action",  "name": "action", "orderable": false, "searchable": false}
                        ]
                     }'>
            <thead>

@ainsleybernard
Copy link
Author

ainsleybernard commented Oct 12, 2023

Thanks for help! I guess im wondering if instead of "ajax" property in your example can we just the set "data"

on the DataTable docs, they show you can init a table with "data" prop, is it possible to do with the "data-wb-tables" attribute?

https://datatables.net/examples/data_sources/js_array.html

$(document).ready(function() {
    $('#example').DataTable( {
        "data":  [['Tiger Nixon', 'System Architect', 'Edinburgh', '5421', '2011/04/25', '$320,800']],
        "columns": [
        { title: 'Name' },
        { title: 'Position' },
        { title: 'Office' },
        { title: 'Extn.' },
        { title: 'Start date' },
        { title: 'Salary' }
    ]
    } );
} );

@wewhite
Copy link
Contributor

wewhite commented Oct 12, 2023

Yes you can, you must define each columns data, for the data you supply.

Here is working code for what you want,

<table id="staticDataExample" class="wb-tables table display" style="width:100%"  data-wb-tables='{
                        "data" : [{"Name":"Tiger Nixon","position":"System Architect","city":"Edinburgh","id":"5421","date":"2011/04/25","salary":"$320,800"}]  ,
                        "columns": [
                            {"data": "Name", "name":"Name"},
                            {"data": "position", "name":"Position"},
                            {"data": "city",  "name": "city"},
                            {"data": "id",  "name": "id"},
                            {"data": "date", "name": "date"},
                            {"data": "salary", "name": "salary"}
                        ],
                        "searching" : true
                    }'>
        <thead>
            <tr>
                <th>Name</th>
                <th>Position</th>
                <th>City</th>
                <th>id</th>
                <th>date</th>
                <th>Salary</th>
            </tr>
        </thead>
            <tbody>
            </tbody>
</table>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants