简体   繁体   English

在另一个数据表的子行中添加嵌套数据表

[英]Add nested datatable within child row of another datatable

I have seen where others have asked this question (like here: Add child row as nested datatable within exisiting datatable ) but I have yet to see it answered. 我已经看到其他人提出这个问题的地方(比如这里: 在现有的数据表中添加子行作为嵌套数据 )但我还没有看到它的答案。

I have a "master" datatable that makes use of the child row feature. 我有一个“主”数据表,它使用子行功能。 Here's the code that initializes the "master" datatable. 这是初始化“master”数据表的代码。 (I'm including it just to be thorough, but everything here works perfect.) (我包括它只是为了彻底,但这里的一切都很完美。)

var table = $('#members');

var oTable = table.dataTable({

    "language": {
        "aria": {
            "sortAscending": ": activate to sort column ascending",
            "sortDescending": ": activate to sort column descending"
        },
        "emptyTable": "No data available in table",
        "info": "Showing _START_ to _END_ of _TOTAL_ entries",
        "infoEmpty": "No entries found",
        "infoFiltered": "(filtered1 from _MAX_ total entries)",
        "lengthMenu": "_MENU_ entries",
        "search": "Search:",
        "zeroRecords": "Loading..."
    },
    "buttons": [],
    "columnDefs": [
        {className: 'control'},
        {orderable: false, targets: 0 }
    ],
    // setup responsive extension: http://datatables.net/extensions/responsive/
    "responsive": true,
    "order": [

        [1, 'asc']

    ],
    "order-column": false,
    "lengthMenu": [
        [5, 10, 15, 20, -1],
        [5, 10, 15, 20, "All"] // change per page values here
    ],
    // set the initial value
    "pageLength": 5,
    "dom": "<'row' <'col-md-12'B>><'row'<'col-md-6 col-sm-12'l><'col-md-6 col-sm-12'f>r><'table-scrollable't><'row'<'col-md-5 col-sm-12'i><'col-md-7 col-sm-12'p>>", 
});

This next bit of code is located right after the above code and accomplishes 2 things: 1) It prevents multiple child rows from being opened at once (just a personal preference) and 2) Uses an AJAX call to populate the child row that was just toggled open. 下一段代码位于上面的代码之后,完成了两件事:1)它可以防止一次打开多个子行(只是个人偏好)和2)使用AJAX调用来填充刚刚生成的子行切换打开。

 table.on('click', 'tr', function () {

        var id = $(this).attr('id');
        if($(this).hasClass('parent'))
        {
            table.$('tr.parent').not(this).find('td:first-child').trigger('click');

            var load_member_leads = $(this).next().find('td.child > ul > li span.dtr-data');
            //var load_member_leads = $('#test');

            $.ajax({
                url: base_url + 'process/load_member_leads',
                type: 'POST',
                data: {test:id},
                dataType: "html", 
                success: function(data){
                    console.log ("success");
                    load_member_leads.html(data);
                    formRepeater();
                    initTable1(id);
                },
                failure: function (data) {
                    console.log ("failed");
                }
            });
        }
    });

This portion of the code appears to be working well, but here is where it gets tricky. 这部分代码似乎运行良好,但这里是棘手的。 Within the returned AJAX data, I have an html table that I would like to initialize using the datatables plugin. 在返回的AJAX数据中,我有一个html表,我想使用datatables插件初始化。 Essentially what I'd have is a nested datatable within a child row of another "master" datatable. 基本上我所拥有的是另一个“主”数据表的子行中的嵌套数据表。 The problem is, everything works great until I try to initialize the datatable plugin on the nested table using: 问题是,在我尝试使用以下命令初始化嵌套表上的数据表插件之前,一切正常:

  initTable1(id);

(Note: the id variable being passed is to prevent multiple tables with the same ids. You can see in the code below how it's appended to the datatable init call.) Once this function is called, everything in the child row disappears and is erased from the DOM. (注意:传递的id变量是为了防止多个表具有相同的ID。您可以在下面的代码中看到它是如何附加到datatable init调用的。)一旦调用此函数,子行中的所有内容都会消失并被删除来自DOM。 Just gone. 刚走了 And I have no idea why. 我不明白为什么。

Here is the code within the initTable1() function that initializes the 2nd datatable: 这是初始化第二个数据表的initTable1()函数中的代码:

 var table2 = $('#leads_' + id);

    var oTable2 = table2.dataTable({
        "language": {
            "aria": {
                "sortAscending": ": activate to sort column ascending",
                "sortDescending": ": activate to sort column descending"
            },
            "emptyTable": "No data available in table",
            "info": "Showing _START_ to _END_ of _TOTAL_ entries",
            "infoEmpty": "No entries found",
            "infoFiltered": "(filtered1 from _MAX_ total entries)",
            "lengthMenu": "_MENU_ entries",
            "search": "Search:",
            "zeroRecords": "No matching records found"
        },

        destroy: true,

        //setup buttons extentension: http://datatables.net/extensions/buttons/
        buttons: [],

        // setup responsive extension: http://datatables.net/extensions/responsive/
        responsive: {
            details: {
                type: 'column',
                target: -1
            }
        },
        "columnDefs": [ {
           className: 'control',
           orderable: false,
           targets:   -1
        } ],
        "order": [
            [0, 'asc']
        ],
        "ordering": false,
        "lengthMenu": [
            [5, 10, 15, 20, -1],
            [5, 10, 15, 20, "All"] // change per page values here
        ],
        // set the initial value
        "pageLength": 10,
        "dom": ""

    });

And for good measure, here is the code being fetched by the AJAX call. 好的方法是,这是AJAX调用提取的代码。 (Note the $test variable is the same as the id variable being passed to the initTable1 function) (注意$ test变量与传递给initTable1函数的id变量相同)

 <table class="table table-striped table-bordered dt-responsive" width="100%" id="leads_<?php echo $test; ?>">
                            <thead>
                                <tr>
                                    <th class="all">Column 1</th>
                                    <th class="all">Column 2</th>
                                    <th class="all">Column 3e</th>
                                    <th class="all">Column 4</th>
                                    <th class="all">Column 5</th>
                                    <th class="all">Column 6</th>
                                    <th class="all">Column 7e</th>
                                    <th class="all">Column 8</th>
                                    <th class="none">Column 9</th>
                                    <th class="all">Column 10</th>
                                </tr>
                            </thead>
                            <tbody>
                            <!-- table rows populated here - this is currently hardcoded for testing -->
                            </tbody>
                        </table>

Here's what DOES work, which may shed some light as to what's actually causing the problem. 以下是DOES的工作原理,可以解释实际导致问题的原因。

1) It works if I comment out the initTable1() function within the AJAX call...well everything but the initialization of the datatable plugin on the 2nd table. 1)如果我在AJAX调用中注释掉initTable1()函数,那么它是有效的...除了第二个表上的数据表插件的初始化之外的一切。 But the html populates where it should within the child row of the "master" datatable. 但是html填充了它应该在“master”数据表的子行中的位置。

2) It works if I change where the AJAX data is being populated - specifically, it works outside of the "master" datatable. 2)如果我改变了AJAX数据的填充位置,它就可以工作 - 具体来说,它可以在“master”数据表之外工作。 It not only populates where it should (note the commented out var load_member_leads that points to $('#test').), but it initializes datatables correctly as well. 它不仅填充它应该的位置(注意注释掉的var load_member_leads指向$('#test')。),但它也正确初始化数据表。

It only seems to break if I both initialize datatables on the second table, and place said table in the child row of the "master" datatable. 如果我在第二个表上初始化数据表,并将所述表放在“主”数据表的子行中,它似乎只会中断。 And by break, I mean completely disappear as if the AJAX call failed - which it isn't according to the console.log. 通过休息,我的意思是完全消失,好像AJAX调用失败 - 这不是根据console.log。

This problem is driving me crazy, what am I doing wrong? 这个问题让我发疯,我做错了什么? Any help is much appreciated! 任何帮助深表感谢!

I feel kind of stupid after spending hours on this one issue, but it turns out I was using the responsive extension rather than the built in functionality with datatables. 在这个问题上花了好几个小时后,我觉得有点愚蠢,但事实证明我使用的是响应式扩展而不是内置的数据表功能。 For anyone else running into this issue, just follow and modify the code as needed from the example here: https://datatables.net/examples/api/row_details.html 对于遇到此问题的其他任何人,只需按照以下示例中的说明关注并修改代码: https//datatables.net/examples/api/row_details.html

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

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