gpt4 book ai didi

javascript - Angularjs - 如何在单击父行时动态添加行

转载 作者:搜寻专家 更新时间:2023-10-31 08:51:40 25 4
gpt4 key购买 nike

我是 angularjs 的新手,遇到了以下问题。

将创建一个从 JSON 读取数据的行表。假设显示了 6 行。但实时行数会有所不同。

每一行的第一个 td 中都有一个 Accordion (+ 符号)。如果单击此 Accordion ,则应通过从另一个不同的 JSON 读取数据来显示该行的子行。

对于剩余的 5 行,它也应该显示子行,因为单击了相应行的 Accordion 。

我创建了显示 6 行的表格。但我面临的挑战是如何在单击时动态地将子行链接到现有行。这是 plunkr - https://plnkr.co/edit/FTbjn9ZbAOTqc3b6j52h?p=preview

感谢任何帮助。

<html>
<head>
<script src="angular.min.js"></script>

<script src="script.js"></script>
<link rel="stylesheet" href="style.css"/>
<link rel="stylesheet" href="font-awesome.min.css"/>
<link rel="stylesheet" href="bootstrap.min.css" />
</head>
<body data-ng-app="testApp" data-ng-controller="treeTable">

<hr>
<button type="button" class="btn btn-success" data-dismiss="modal" data-ng-click="save()">SaveFilter</button>
<button type="button" class="btn btn-Default" data-dismiss="modal" data-ng-click="delete()">Delete</button>
<div class="row">
<div class="col-md-9">
<div style=" margin-left:50px;" class="tableheight">
<table class="table-nested ">
<thead>
<tr>
<!-- <th >
<input data-ng-checked="(list | selected).length == list.length" data-ng-click="toggleAllCheckboxes($event)" type="checkbox" />
</th> -->

<th>
Repository
</th>
<th >
Version
</th>
<th >
Size
</th>
<th >
Modified By
</th>
<th >
Labels
</th>
<th >
Description
</th>
</tr>
</thead>
<tbody style="font-size:12px" data-ng-repeat="item in list">
<tr >
<td ><button style="background-color: #fff;" class="accordion"></button>
{{item.name}}
</td>

<td >
{{item.Version}}
</td>
<td >
{{item.Size}}
</td>
<td >
{{item.ModifiedBy}}
</td>
<td >
{{item.Labels}}
</td>
<td >
{{item.Description}}
</td>
</tr>
</tbody>
</table>
</div>

</div>
</div>
</body>
</html>

最佳答案

See the Plunkr solution

你想要第二个<tr>ng-repeat接下来是第一个:

<tr ng-if="item.Labels==child.Labels" ng-show="item.opened" ng-repeat="child in children">
<td>{{child.name}}</td>
...etc
</tr>

这将构建额外的行,其中捆绑 .Labels匹配 repo .Labels并且只会在 repo 的 opened 时显示属性是 true . +按钮将切换 opened每个元素的属性。您需要对数据进行两次小的修改才能使其生效:

  1. 添加children数据到 $scope (用于第 2 个 ng-repeat 中的访问)。
  2. 添加默认 opened: false所有存储库的属性(第一个除外,即 true )。

这不会显示组件,但希望您能理解。

See the Plunkr solution

关于javascript - Angularjs - 如何在单击父行时动态添加行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41911449/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com