首页 > 解决方案 > 使用 div 创建手风琴表

问题描述

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>

我有这个table。将tbody使用由生成的行动态填充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>

到目前为止我尝试的是给每个traclass="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

有任何想法吗?

标签: javascriptjqueryhtmlajaxaccordion

解决方案


我想这就是你要找的

点击表格行数据的详细视图

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


推荐阅读