Retrieve dataTable object of element

夙愿已清 提交于 2020-01-05 05:36:12

问题


I am trying to get an object of a jQuery Data Table using JQuery. I am using this thread to accomplish this:

https://datatables.net/forums/discussion/2913/how-to-get-the-otable-datatable-object-from-the-tables-id

For some reason this does not work. When I call $('.dynamic-wide-table').dataTable(); it causes my table to initialize AGAIN and make the table render twice, like so:

According to the link posted above it should NOT be doing that, but instead just fetch the object.

Here is my code:

main.js (loaded into HTML head of every page)

$(document).ready(function() {
    $(".dynamic-wide-table").dataTable({
        "aaSorting": [],
        "scrollX": true,
        "scrollY": 530,
        "scrollCollapse": true,
        "lengthMenu": [
            [100, 400, 1000, 5000, -1],
            [100, 400, 1000, 5000, "All"]
        ],
        "retrieve": true
    });
});

get object:

$(document).ready(function() {
    $('.dynamic-wide-table').dataTable();
});

回答1:


Following the comments:

You wont be able to get the single tables object because each element with that class will have it's own dataTable object.

You could loop through the classes and retrieve each individual one. In order to get a single elements dataTable object you will need to know which table element it is that you need the object from:

$(document).ready(function() {
    $('.dynamic-wide-table').each(function(i){
        console.log('table no. ' + i );
        // Should print the dataTable object for each element to the console
        console.log($(this).dataTable());
    });
});

The ideal thing to do would be to give each individual table an id, then retrieve the dataTable object with the id:

$('#tableId').dataTable();



回答2:


Mmm I don't think you need to set both..?

If you remove the get object code it should stop firing twice.

You are already doing this in main.js , but loading options at the same time.



来源:https://stackoverflow.com/questions/38486062/retrieve-datatable-object-of-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!