DataTables行详细信息row()undefined

时间:2015-05-19 10:17:37

标签: javascript jquery datatables jquery-datatables datatables-1.10

我目前正在尝试实施此类表格: https://datatables.net/examples/server_side/row_details.html

我做了一个示例javascript的复制/粘贴,将它添加到我的表中,但它一直在崩溃:

   $("#dyntable2 tbody").on("click", "tr td.details-control", function () {
                var tr = $(this).closest("tr");
                var row = dt.row(tr); <<< At this line

出现该错误:&#34; Uncaugth TypeError:undefined不是函数(匿名函数)&#34;

编辑:这是完整的javascript:

    var $ = jQuery.noConflict();

    function format(d) {
        return 'Flow Name + Id: ' + d.Flow.Name + ' ' + d.Flow.Id + '<br>' +
            'Id Session: ' + d.Id + '<br>' +
            'Placeholder for now, quick monitoring tool after';
    }

    $(document).ready(function() {

        var dt = $('#dyntable2').dataTable({
            lengthMenu: [[10, 25, 50, -1], ["10", "25", "50", "All"]],
            processing: true,
            searching: true,
            paging: true,
            pagingType: "full_numbers",
            serverSide: true,
            ajax: {
                url: "url",
                type: "POST"
            },
                    language: {
                url: "jquery/datatables/French.txt"
            },
            columns: [
                {
                   "class": "details-control",
                    data: null,
                    orderable: false,
                    defaultContent: ""
                },
               [...]
            ]
        });

        var detailsRows = [];

        $("#dyntable2 tbody").on("click", "tr td.details-control", function() {
            var tr = $(this).closest("tr");
            var row = dt.row(tr);
            var idx = $.inArray(tr.attr("id"), window.detailRows);

            if (row.child.isShown()) {
                tr.removeClass("details");
                row.child.hide();

                // Remove from the 'open' array
                window.detailRows.splice(idx, 1);
            } else {
                tr.addClass("details");
                row.child(format(row.data())).show();

                // Add to the 'open' array
                if (idx === -1) {
                    window.detailRows.push(tr.attr("id"));
                }
            }
        });

        // On each draw, loop over the `detailRows` array and show any child rows
        dt.on("draw", function() {
            $.each(window.detailRows, function(i, id) {
                $("#" + id + " td.details-control").trigger("click");
            });
        });

    });

我如何解决这个问题,因为我使用了与示例相同的代码?

1 个答案:

答案 0 :(得分:3)

在您的代码中dt是一个没有row()函数的jQuery对象。使用APIdt.api().row(tr)获取行。

PS:这是从版本1.10到之前的更改为1.11。