用div创建手风琴表

时间:2018-07-19 13:10:22

标签: javascript jquery html ajax accordion

function insertObject() {


  var data = [{
      "nodeid": 1,
      "vendor": "0x0345",
      "product_id": "0x0201",
      "product_type": "0x0008",
      "home_id": "0xD087E344",
      "secure": "1",
    },
    {
      "nodeid": 2,
      "vendor": "0x0285",
      "product_id": "0x0777",
      "product_type": "0x0001",
      "home_id": "0xD087D213",
      "secure": "0",
    },
    {
      "nodeid": 3,
      "vendor": "0x1145",
      "product_id": "0x7899",
      "product_type": "0x0851",
      "home_id": "0xD034T13",
      "secure": "0",
    },
    {
      "nodeid": 4,
      "vendor": "0x8992",
      "product_id": "0x1236",
      "product_type": "0x8101",
      "home_id": "0xD0682F13",
      "secure": "1",
    }
  ];



  var tbl = document.getElementById('tableData');
  var tblBody = document.getElementById('tableBody');
  for (var i = 0; i < data.length; i++) {
    var row = document.createElement('tr');
    for (var value in data[i]) {
      var cell = document.createElement("td");
      var cellText = document.createTextNode(data[i][value]);
      cell.appendChild(cellText);
      row.appendChild(cell);
    }
    tblBody.appendChild(row);
  }
  tbl.appendChild(tblBody);
}
th {
  white-space: nowrap;
  color: #D5DDE5;
  background: #1b1e24;
  border-bottom: 4px solid #9ea7af;
  border-right: 1px solid #343a45;
  font-size: 23px;
  font-weight: 100;
  padding: 24px;
  text-align: left;
  text-shadow: 0 1px 1px rgba(0, 0, 0, 0.1);
  vertical-align: middle;
}

tr {
  border-top: 1px solid #C1C3D1;
  border-bottom-: 1px solid #C1C3D1;
  color: #666B85;
  font-size: 16px;
  font-weight: normal;
  text-shadow: 0 1px 1px rgba(256, 256, 256, 0.1);
  cursor: pointer;
}


/*grey row*/

tr:hover td {
  background: #4E5066;
  color: #FFFFFF;
  border-top: 1px solid #22262e;
}

tr:nth-child(odd) td {
  background: #EBEBEB;
}

tr:nth-child(odd):hover td {
  background: #4E5066;
}

td {
  text-align: center;
  background: #FFFFFF;
  vertical-align: middle;
  font-weight: 300;
  font-size: 18px;
  text-shadow: -1px -1px 1px rgba(0, 0, 0, 0.1);
  border-right: 1px hidden #C1C3D1;
}

tr:hover a {
  text-decoration: none;
  color: white;
}

tr a {
  text-decoration: none;
  color: black;
}
<!DOCTYPE html>
<html>

<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>

<body onLoad="insertObject();">
  <table id="tableData">
    <thead>
      <tr class="tableheader">
        <th>NODE ID</th>
        <th>VENDOR</th>
        <th>PRODUCT ID</th>
        <th>PRODUCT TYPE</th>
        <th>HOME ID</th>
        <th>SECURE</th>
      </tr>
    </thead>

    <tbody id="tableBody">
    </tbody>

</body>

</html>

我有这个tabletbody将动态填充由AJAX request生成的行(每一行代表一个特定的节点):

<table id="tableData">
    <thead>
        <tr class="tableheader" >
            <th>NODE ID</th>
            <th>VENDOR</th>
            <th>PRODUCT ID</th>
            <th>PRODUCT TYPE</th>
            <th>HOME ID</th>
            <th>SECURE</th>
        </tr>
    </thead>
    <tbody id="tableBody">
    </tbody>
</table>

我想要的是手风琴类型表,在其中单击一行并打开此div,其中包含有关该节点的更多信息。这是div(也填充了AJAX请求):

<div id="endDiv" style="display: none">
    <ol class="rounded-list">
        <li><label>ID: <input id="roomName"/></label></li>
        <li><label>LOC. NAME: <input id="loc"/></label></li>
        <li><label>EPID: <span id="epid"></span></label></li>
        <li><label>CLSLIST: <span id="clslist"></span></label></li>
        <li><label>TYPE: <span id="type"></span></label></li>
        <li><label>ZPLUS: <span id="zplus"></span></label></li>
        <button onclick="submitData();">Submit changes</button>
    </ol>
</div>

到目前为止,我一直尝试给每个tr一个class="breakrow",但是我遇到的问题是出现在表格左侧的div。这是代码:

$("tr.breakrow").click(function(test) {
  $(this).nextUntil('tr.breakrow').slideToggle(100);
  $("#endDiv").contents().appendTo(".breakrow");

  getEndpoints( $(test.currentTarget).find(".nodeid")[0].innerHTML);
}); //getEndpoints function populate the div with specific values

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

我认为这就是您要寻找的

有关表行数据的详细视图,请单击

HTML

<table border="1">
  <tr class="header">
    <td colspan="2">Row 1</td>
  </tr>
  <tr>
    <td>data</td>
    <td>data</td>
  </tr>
  <tr>
    <td>data</td>
    <td>data</td>
  </tr>
  <tr class="header">
    <td colspan="2">Row 2</td>
  </tr>
  <tr>
    <td>date</td>
    <td>data</td>
  </tr>
  <tr>
    <td>data</td>
    <td>data</td>
  </tr>
  <tr>
    <td>data</td>
    <td>data</td>
  </tr>
</table>

jQuery代码

$('tr.header').click(function(){
    $(this).nextUntil('tr.header').css('display', function(i,v){
        return this.style.display === 'table-row' ? 'none' : 'table-row';
    });
});

CSS

tr {
    display: none;
}

tr.header {
    display: table-row;
}

JSFIDDLE

答案 1 :(得分:0)

您可以通过使用具有单个td的表格行并切换其显示属性(而不是使用div)来实现此目的。您必须在单个td上将colspan设置为等于最大列数,以使单个列拉伸到表格的整个宽度。

像这样:

<table>
    <tr>
        <td> node id </th>
        <td> vendor </th>
        <td> product id </th>
        <td> product type </th>
        <td> home id </th>
        <td> secure </th>
    </tr>
    <tr>
        <td colspan="6"></td>
    <tr>
</table>

如果出于样式或其他原因需要将其用作div,则可以在该td内放置一个div,并使用CSS隐藏列的边框。