简体   繁体   English

来自同一页面上不同API的两个DataTables显示相同的数据

[英]Two DataTables sourced from different APIs on the same page show the same data

I have two DataTables on the same web page with the same number of columns that are sourced from DIFFERENT APIs. 我在同一网页上有两个DataTables,它们的列数均来自DAPIFERENT API。 If I add the class "grid" to one or the other the table displays with the correct data for that table. 如果我将“网格”类添加到一个或另一个中,则该表将显示该表的正确数据。 However, if I add the class "grid" to both, the data for the first table shows up on BOTH tables. 但是,如果我向两者都添加了“ grid”类,则第一个表的数据将显示在两个表中。

The class "grid" is a very complex DataTable that involves quite a bit of configuration but here is how "grid" is initialized: “ grid”类是一个非常复杂的DataTable,涉及大量配置,但是以下是“ grid”的初始化方式:

var t = $(".grid").DataTable({
        iDisplayLength: 10,
        columnDefs: [{
            "searchable": false,
            "orderable": bOrderBy,
            "targets": 0
        }],
        order: [[0, orderDir]],
        ajax: {
            url: src,
            dataSrc: ""
        },
        columns: [
            {
                data: f1
            },
            {
                data: f2
            },
            {
                data: f3
            },
            {
                data: f4
            },
            {
                data: f5,
. . . "blah, blah, blah"
});

t.on('post-body.bs.table', function () {
        $('[data-toggle="tooltip"]').tooltip({
            container: 'body',
            placement: 'top'
        });
    });


    if (autoNum == "Y") {
        t.on('order.dt search.dt', function () {
            t.column(0, { search: 'applied', order: 'applied' }).nodes().each(function (cell, i) {
                cell.innerHTML = i + 1;
            });
        }).draw();
    }

How do I fix this? 我该如何解决?

Right now, it is applying the same DataTable instance to anything selected in your .grid selector which is why they are coming back the same. 现在,它将相同的DataTable实例应用于.grid选择器中选择的任何内容,这就是为什么它们返回相同的原因。 If the table options are the same, you can save this into a obj and then pass it to each call. 如果表选项相同,则可以将其保存到obj中,然后将其传递给每个调用。 Name the 2 tables with different id's or different classes 用不同的ID或不同的类命名这两个表

var dtOptions = {
    iDisplayLength: 10,
    columnDefs: [{
        "searchable": false,
        "orderable": bOrderBy,
        "targets": 0
    }],
    order: [[0, orderDir]],
    ajax: {
        url: src,
        dataSrc: ""
    },
    columns: [
        {
            data: f1
        },
        {
            data: f2
        },
        {
            data: f3
        },
        {
            data: f4
        },
        {
            data: f5,
. . . "blah, blah, blah"
};

$("#mytable1).DataTable(dtOptions);
$("#mytable2).DataTable(dtOptions);

You may need to change the ajax src for the 2nd table as you said they are coming from separate api's. 您可能需要更改第二张表的ajax src,因为您说它们来自单独的api。 You could possibly copy the options object and modify the ajax src if needed. 您可能需要复制options对象并修改ajax src。

var dtOptions2= $.extend(true, {}, dtOptions);
dtOptions2.ajax.url = "somthing-else...";
$("#mytable2).DataTable(dtOptions2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM