0

我正在尝试实现一个主从数据表,除了详细数据表中的标题未显示外,该表大部分工作正常。当我检查标题时,我看到它的高度为零。

我搜索了一个解决方案,似乎可以解释问题的一件事是,当表被隐藏然后被初始化时,它最终的页眉和页脚高度为零。

我尝试了一些方法来解决它,但都无济于事。我会很感激一些建议。

这是我所拥有并尝试过的:

// Master data tabl
<table id="AITable" class="table table-striped table-bordered table-hover display compact">
    <thead>
        <tr>
            <th></th>
            <th>Col 1</th>
            <th>Col 2</th>
        </tr>
    </thead>
    <tbody>
    </tbody>
    <tfoot>
    </tfoot>
</table>

<script type="text/javascript">
    var iTableCounter = 1;
    var oInnerTable = new Object;

    var tblAI = $("#AITable").DataTable({
        jQueryUI: true,
        data: [],
        dom: 'frtip',
        scrollY: 450,
        scrollCollapse: true,
        autoWidth: true,
        paging: false,
        sortCellsTop: true
        },
        "columns": [
            {
                "className": 'details-control',
                "orderable": false,
                "data": null,
                "defaultContent": ''
            },
            {
                "data": "COL1"
            },
            {
                "data": "COL2"
            }
    });

    function addDataTableListener() {
        // Add event listener for opening and closing details
        $('#AITable tbody').on('click', 'td.details-control', function () {
            var tr = $(this).closest('tr');
            var row = tblAI.row(tr);
            var col1 = row.data().COL1;

            if (row.child.isShown()) {
                // This row is already open - close it
                row.child.hide();
                tr.removeClass('shown');
            }
            else {
                // Add the html table shell of the datatable.
                row.child(formatEDLTableDetailRow(iTableCounter)).show();

                //show the datatable row.
                tr.addClass('shown');

                // try datatable stuff
                oInnerTable = buildSubDataTable(row.data(), iTableCounter);
                populateSubTable(oInnerTable, col1);
                iTableCounter = iTableCounter + 1;
            }
        });
    }

    $(document).ready(function () {
        // This populates the top data table
        populateAITable(-1);
        addDataTableListener();
        ...
    });

    function buildSubDataTable(parentObjData, tableCounter) {

        //if ($.isEmptyObject(oInnerTable)) {
            oInnerTable = $("table#itemsDataTable_" + tableCounter).DataTable({
                jQueryUI: true,
                data: [],
                dom: 'tip',
                info: false,
                showEntries: false,
                autoWidth: false,
                paging: false
                },
                "columns": [
                    {
                        "orderable": false,
                        "data": null,
                        "defaultContent": ''
                    }, {
                        "data": "COL3",
                        "title": "Col 3"   // Tried adding "title", didn't work
                        }
                    },
                    {
                        "data": "COL4",
                        "title": "Col 4"
                    }
                ],
                "columnDefs": [
                    ...
                ],
                "fnInitComplete": function () {  // Tried this, didn't work
                    this.fnAdjustColumnSizing()
                }
            })
        //}
        return oInnerTable;
    }

    function populateSubTable(oInnerTable, someID) {
        $.ajax({
            type: "POST",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            url: "../WebService/AIR.asmx/GetSomething",
            cache: false,
            data: JSON.stringify({ SomeID: someID }),
        }).done(function (result) {
            oInnerTable.clear().draw();
            if (!result || result.d === "") {
            }
            else {
                jResult = JSON.parse(result.d);
                oInnerTable.rows.add(jResult).draw();
                oInnerTable.draw(false);
            }
        }).fail(function (jqXHR, textStatus, errorThrown) {
        });
    }

    function formatTableDetailRow(table_id) {
        // Tried adding class "header" with "height" and "line-height" set to 2rem; didn't help
        var tblMarkup = 
            '<div style="font-size: 1rem">' +
                '<table id="itemsDataTable_' + table_id + '" class="table table-striped table-bordered table-hover responsive display compact pull-left">' +
            '        <thead class="header">' +
            '            <tr class="header">' +
            '                <th></th>' +
            '                <th>Col 3</th>' +
            '                <th>Col 4</th>' +
            '            </tr>' +
            '        </thead>' +
            '        <tbody></tbody>' +
            '    </table>' +
            '</div>';
        return tblMarkup;
    }
4

1 回答 1

0

代码按原样正常运行,并按预期工作。标题行未显示的原因在 CSS 下面:

table#AITable thead {
    visibility: collapse;  <-- commented this out and header showed.
}
于 2020-06-23T16:44:05.003 回答