AngularJS:根据表一中的已检查行隐藏空表(AngularJS: Hide Empty Table based on checked rows in Table One)
我有一个数据表,每个都有一个复选框。 检查的任何行都移动到第二个表中进行处理(处理未显示)。 我想隐藏第二个表,除非它有行但似乎无法弄清楚ng-show。 (更新以显示需要隐藏第二个表)
这是我的html(我的行无法注释掉):
<span>Table One</span> <div ng-controller="checkBoxCtrl"> <table width="400" border="1"> <tr> <th>✔</th> <th>Key</th> <th>Value</th> </tr> <tr ng-repeat="data in tableOne" id="item{{data.key}}"> <td width="20px"> <input type="checkbox" ng-model="data.checked"> </td> <td>{{data.key}}</td> <td>{{data.value}}</td> </tr> </table> <br> <!--<div ng-show="tableOne.key.checked == true"> --> <div> <span>Table Two</span> <table width="400" border="1"> <tr> <th>Key</th> <th>Value</th> </tr> <tr ng-repeat="data in tableOne | filter: {checked:true}"> <td>{{data.key}}</td> <td>{{data.value}}</td> </tr> <tr> <td colspan="2"> <button>New Group</button> </td> </tr> </table> </div>
这是javascript:
var app = angular.module('myApp', []); function checkBoxCtrl($scope){ $scope.tableOne=[ {key: '1', value: 'a'}, {key: '2', value: 'b'}, {key: '3', value: 'c'}, {key: '4', value: 'd'} ]; };
I have a table of data each with a checkbox. Any row that is checked moves into a second table for processing (processing not shown). I want the second table hidden unless it has rows but can't seem to figure out the ng-show. (Updated to show the need to hide the second table)
Here's the updated jsfiddle example.
Here's my html (I have the line that doesn't work commented out):
<span>Table One</span> <div ng-controller="checkBoxCtrl"> <table width="400" border="1"> <tr> <th>✔</th> <th>Key</th> <th>Value</th> </tr> <tr ng-repeat="data in tableOne" id="item{{data.key}}"> <td width="20px"> <input type="checkbox" ng-model="data.checked"> </td> <td>{{data.key}}</td> <td>{{data.value}}</td> </tr> </table> <br> <!--<div ng-show="tableOne.key.checked == true"> --> <div> <span>Table Two</span> <table width="400" border="1"> <tr> <th>Key</th> <th>Value</th> </tr> <tr ng-repeat="data in tableOne | filter: {checked:true}"> <td>{{data.key}}</td> <td>{{data.value}}</td> </tr> <tr> <td colspan="2"> <button>New Group</button> </td> </tr> </table> </div>
and here's the javascript:
var app = angular.module('myApp', []); function checkBoxCtrl($scope){ $scope.tableOne=[ {key: '1', value: 'a'}, {key: '2', value: 'b'}, {key: '3', value: 'c'}, {key: '4', value: 'd'} ]; };
原文:https://stackoverflow.com/questions/23903036
最满意答案
只需在控制器中添加一个函数
isAtLeastOneDataChecked()
,如果至少检查了一个数据,则返回true,否则返回false,并在模板中使用它:<table width="400" border="1" ng-show="isAtLeastOneDataChecked()"> $scope.isAtLeastOneDataChecked = function() { return $scope.tableOne.some(function(data) { return data.checked; }); };
Simply add a function
isAtLeastOneDataChecked()
in your controller, which returns true if at least one data is checked, and false otherwise, and use it in your template:<table width="400" border="1" ng-show="isAtLeastOneDataChecked()"> $scope.isAtLeastOneDataChecked = function() { return $scope.tableOne.some(function(data) { return data.checked; }); };
相关问答
更多-
我编辑了每个循环的选择器,只选择具有单击的复选框过滤器值的表行。 然后我正在检查是否选中了复选框。 如果它被检查我正在显示tr,如果它不是我隐藏tr。 $(function() { $('.user-filter').click(function() { var user_type = $(this).data("id3"); var checked = $(this).is(':checked'); $(".accordion-users tr[data-id3=" + ...
-
你可以使用ng-hide来做到这一点。 像这样:
如果您想要将标签全部拉出DOM,您可以使用ng-if : 根据日期隐藏表格行(hide table rows based on date)[2021-08-25]
这可能会帮助你 $(document).ready(function(e) { $('.event_display_table tbody tr').each(function(index, element) { event_day = $(this).attr('data-date'); event_day = new Date(event_day); //today = new Date('2017-03-14'); today = new Date(); ...使用jquery,我怎样才能隐藏所有未检查的表行?(Using jquery, how can I hide all table rows that are not checked?)[2020-01-25]
我想你在找这个...... $('#mybutton').click(function(){ $('table tr td:first-child :checkbox:not(:checked)').closest('tr').hide(); }); * {margin: 0px; padding: 0px;} body {background-color: rgb(248,248,248)} #nav { list-style:none; } #nav li { float ...如果您正在使用此答案中的setCookie()函数,那么您将cookie设置为具有30秒的生命周期。 这可能不足以满足您的要求,您可能应该考虑增加它的量。 :-) 编辑 Re:你的问题编辑 - 如果你想设置多个cookie,你将遇到以下问题: document.cookie = name+"="+value+expires+"; path=/"; 在这里,每次调用setCookie()时都会覆盖cookie属性。 如果要设置多个cookie,则需要在每次调用setCookie()为字符串添加/追加新的名称 ...如何在Angularjs中来自服务器的数据为空时隐藏表行(How to hide table rows when data coming from the server is empty in Angularjs)[2022-01-02]
无需解析值:应该足够第一行(类似于其他两行),确保JSON响应存储在$scope.data (我假设已经是因为你在td输出它)。 如果指定值为truthy(包括空字符串 - 这就是我删除比较的原因),它将被隐藏。 No need to parse the values: should be enough for the first line (similar ... 由于表格控件的visibleRowCount属性,“空”行是可见的。 通过为绑定的更改事件添加处理程序,可以在每次行绑定更改时动态更新此更新。 例如... 视图: