javascript - 如何使用 javascript 使子行在单击父行时可见

标签 javascript html

我有一个 HTML 表,其中有父行,并且每个父行都很少有子行。我必须首先隐藏这些子行,当单击父行时,应该显示子行。

我有一个数据列表,我正在迭代该列表,对于每次迭代,我都会将行附加到表中,如下所示。

var json = [{
  Message: "abc",
  name: "Some name",
  id: 345,
  col4: 2,
  col5: 5
}];

var $container = $("#container");
var $thead = $("#container table thead");
var $tbody = $("#container table tbody");
var $row = $("#container table tbody tr");

json.forEach(function(item) {
  var $button = $("<button>" + item.Message + "</button>");
  $container.prepend($button);

  var table = $("<table>");
  table.append($("<tr><th>col1</th><th>col2</th><th>col3</th><th>col4</th><th>col5</th></tr>"));

  $button.on("click", function() {
    //parent row
    var row = $('<tr><td>' + item.Message + '</td>' + '<td>' + item.name + '</td>' + '<td>' + item.id + '</td>' + '</td>' + "" + '</td>' + '<td>' + "" + '</td></tr>');
    table.append(row);
    //child row
    var row = $('<tr><td>' + "" + '</td>' + '<td>' + "" + '</td>' + '<td>' + "" + '</td>' + '<td>' + item.col4 + '</td>' + '<td>' + item.col5 + '</td></tr>');
    table.append(row);

    $("#table").html(table);
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="container">
  <div id="table">
  </div>
</div>

我想为每个父行和子行的类提供一个 ID,当单击父行时,应该显示子行。

最佳答案

您需要做的是为父行提供类,然后根据 jquery next 函数切换子行

 var json = [{
     Message: "abc",
     name: "Some name",
     id: 345,
     col4:2,
     col5:5
 }];

 var $container = $("#container");
 var $thead = $("#container table thead");
 var $tbody = $("#container table tbody");
 var $row = $("#container table tbody tr");

 // Loop through items in JSON data..
 json.forEach(function(item) {
   var $button = $("<button>" + item.Message + "</button>");
   $container.prepend($button);
   var table = $("<table>");
   table.append($("<tr><th>col1</th><th>col2</th><th>col3</th><th>col4</th><th>col5</th></tr>"));
          
	 // Button click handler..
   $button.on("click", function() {
     
     // Replace row HTML..
     //parent row
     var row=$('<tr class="parent_row" ><td>' + item.Message + '</td>' + '<td>' + item.name + '</td>' + '<td>' + item.id + '</td>' + '</td>' +      "" + '</td>' + '<td>' + "" + '</td></tr>');
     
     table.append(row);
     //child row
     var row=$('<tr style="display: none"><td>' + "" + '</td>' + '<td>' + "" + '</td>' + '<td>' + "" + '</td>' + '<td>' + item.col4 + '</td>' + '<td>' + item.col5 + '</td></tr>');
     table.append(row);
    
     $("#table").html(table);
      $('.parent_row').click(function() {
	
			$(this).next().toggle();
})
     // Show table if it's not already visible..
     

   });
 });
 
 
table {
   margin-top: 20px;
   border: 1px solid silver;
   width: 500px;
 }

 th {
   text-align: left;
 }
 
 button {
   margin-left: 15px;
 }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
 <div id="container">
   <div id="table">
   </div>

 </div>

如果您有多个 child ,您可以使用 nextUntill('.parent_row')

关于javascript - 如何使用 javascript 使子行在单击父行时可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58302754/

相关文章:

javascript - 选择下拉列表不显示(jquery-chosen)

javascript - 检测国外网站参数变化

javascript - 从 html "array"到 javascript 数组

javascript - Onsubmit 事件拒绝触发

php - 执行 mysqli fetch 时出错

javascript - 如果用户中断重定向到新页面,禁用的提交按钮将保持禁用状态

javascript - 表单未按时自动提交

javascript:谷歌地理 - map

javascript - 在页面加载 Javascript 时选择默认单选输入

javascript - 对象不支持从 iframe 调用的 MS Edge 中的属性或方法 'slideToggle'