简体   繁体   English

如何将嵌套数据表添加到HTML表

[英]How to add nested datatable to HTML table

I am building a table dynamically with JavaScript, and I need to nest another table which will be a jQuery datatable inside the first table which is HTML. 我正在使用JavaScript动态构建一个表,并且需要在另一个表(即HTML)中嵌套另一个表,该表将是jQuery数据表。

I have what I thought would work and after researching, I don't see why it isn't working. 我有我认为可以工作的东西,经过研究,我看不出为什么它不工作。 I am defining my first table, building out the header and then adding rows. 我正在定义我的第一个表,建立标题,然后添加行。 Inside of a cell, I build the table that will be the datatable. 在单元格内部,我构建了将成为数据表的表。

Using console.log, it looks to be built correctly, but it doesn't display correctly. 使用console.log,它看起来可以正确构建,但是显示不正确。 Instead, it shows only the first table and then appears as if it is not in a table, but rather just haphazardly placed on the page. 而是,它仅显示第一个表,然后看起来好像它不在表中,而是随意地放在页面上。 Here is my code. 这是我的代码。 I would greatly appreciate it if someone will look at it and see if they see a problem with it. 如果有人看到它,看看他们是否发现问题,我将不胜感激。

By the way, I don't think it would make any difference, but my openDetailRow function is based on a click coming from a row in an existing datatable. 顺便说一句,我认为这没有什么区别,但是我的openDetailRow函数基于来自现有数据表中一行的点击。

在此处输入图片说明

function openDetailRow() {
  $("#gridTbl tr td:nth-child(1)").on("click",
    function () {
      var ndx = $(this).closest('tr').find('td:eq(0)').text();
      var dataRow = reportApp.grid.fnGetData(this.parentNode);
      addElements(dataRow);
    });
  }

  function getDetails() {
    $("#hdrTbl").dialog({
      resizable: false,
      modal: true,
      title: "Order Details",
      height: 250,
      width: 700,
      buttons: {
        "Close": function () {
          $(this).dialog('destroy');
          $(this).remove();
          $("#ordDiv").remove();
        }
      }
    });
  }

  function buildHdrTable(dataRow){
    var hdrDets = [];
    hdrDets[0] = dataRow.ordnbr;
    hdrDets[1] = dataRow.custordnbr;
    hdrDets[2] = dataRow.carrier;
    hdrDets[3] = dataRow.custid;
    var rowDets = [];
    dataRow.detail.forEach(
      function (el) {
        var rowAr = [];
        rowAr[0] = el.invtid;
        rowAr[1] = el.descr;
        rowAr[2] = el.pcs;
        rowAr[3] = el.status;
        rowDets.push(rowAr);
      });

      hdrTbl = document.createElement('table');
      hdrTbl.cellPadding = 5;
      hdrTbl.style.width = '750px';
      hdrTbl.style.display = 'none';
      hdrTbl.setAttribute("id", "hdrTbl");
      var hdrVals = ["Ord #", "Cust Ord #", "Ship Via", "Cust ID" ];
      var tblHead = document.createElement('thead');
      hdrTbl.appendChild(tblHead);
      tblHeadRow = document.createElement("tr");
      tblHead.appendChild(tblHeadRow);
      for(var i =0; i < hdrVals.length; i++){
        tblHeadRow.appendChild(document.createElement("th")).
        appendChild(document.createTextNode(hdrVals[i]));
      }
      var hdrBody = document.createElement("tbody");
      hdrTbl.appendChild(hdrBody);
      var tr = hdrBody.insertRow();
      var td1 = tr.insertCell();
      var td2 = tr.insertCell();
      var td3 = tr.insertCell();
      var td4 = tr.insertCell();

      td1.appendChild(document.createTextNode(hdrDets[0]));
      td2.appendChild(document.createTextNode(hdrDets[1]));
      td3.appendChild(document.createTextNode(hdrDets[2]));
      td4.appendChild(document.createTextNode(hdrDets[3]));
      var bdy = hdrBody.insertRow();
      var bdyTbl = bdy.insertCell();
      tbl = document.createElement('table');
      tbl.style.width = '100%';
      tbl.style.display = 'none';
      //tbl.style.border = "1px solid black";
      tbl.setAttribute("id", "ordertable");

      var headVals = ["Inventory Number", "Description", "Number of Pieces", "Status"];
      var thead = document.createElement('thead');
      tbl.appendChild(thead);
      var theadRow = document.createElement("tr");
      thead.appendChild(theadRow);
      for (var i = 0; i < headVals.length; i++) {
        theadRow.appendChild(document.createElement("th"))
                .appendChild(document.createTextNode(headVals[i]));
      }
      var tbody = document.createElement("tbody");
      tbl.appendChild(tbody);
      for (var i = 0; i < rowDets.length; i++) {
        var tr = tbody.insertRow();
        var td1 = tr.insertCell();
        var td2 = tr.insertCell();
        var td3 = tr.insertCell();
        var td4 = tr.insertCell();

        td1.appendChild(document.createTextNode(rowDets[i][0]));
        td2.appendChild(document.createTextNode(rowDets[i][1]));
        td3.appendChild(document.createTextNode(rowDets[i][2]));
        td4.appendChild(document.createTextNode(rowDets[i][3]));
        bdyTbl.appendChild(tbl);
      }

      return hdrTbl;
    }

    function addElements(dataRow) {
      var body = document.body;
      var hdrTbl = buildHdrTable(dataRow);
      ordDiv = document.createElement("div");
      ordDiv.appendChild(hdrTbl);
      ordDiv.setAttribute("id", "ordDiv");

      body.appendChild(ordDiv);

      $("#ordertable").css('display', 'none');

      $("#ordertable").dataTable(tbl);
      getDetails();
      console.log(hdrTbl);
    }

这是对话框加载时的样子。如您所见,它根本不会渲染嵌套表,并且会使页面上的第一个表倾斜。

The issue was caused because I was using display:none. 造成此问题的原因是我正在使用display:none。 Now here is the thing.. If you don't use that particular style attribute, then the table will show up on the page. 现在是这样。如果您不使用该特定的样式属性,则该表将显示在页面上。 However, since it is nested inside my first table, and the first table has the display:none style attribute already applied to it, then by applying it to the second table, it would not allow that table to be shown on page. 但是,由于它嵌套在我的第一个表中,并且第一个表已经应用了display:none样式属性,因此将其应用到第二个表后,将不允许该表显示在页面上。 As for the skewed look, I had not set a colspan. 至于偏斜的外观,我没有设置斜角。 So now, it is working perfectly. 所以现在,它运行良好。 I commented out the display none and added this one line of code... 我没有注释掉显示内容,并添加了这一行代码...

bdyTbl.setAttribute("colspan", 4);

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

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