javascript - Angularjs - 隐藏表格行

标签 javascript angularjs

我在表格行中有一个切换按钮,点击后,我想让最后点击的行可见,同时隐藏表格的所有其他行。

这是使用两个标志完成的:“showAll”(全局)和“showGridRow”- 每行。

如果其中一个标志为真,则显示一行: ng-show="{{showAll}}||product.showGridRow"

代码:

$scope.showAll = showAll;

$scope.toggleReadings = function toggleReadings(product) {
  if (product.showGridRow == undefined) {
    //if undefined, this is first click - set to false so it will be enabled immediatelly
    product.showGridRow = false;
  }

  product.showGridRow = !product.showGridRow;
  //if true, hide all others, else show all
  showAll = !product.showGridRow;

};
<table class="table table-striped">
  <thead>
    <tr>
      <th>Product ID</th>
      <th>Product Name</th>
      <th>Topic</th>
      <th>Active</th>
      <th>Readings</th>
    </tr>
  </thead>
  <tbody>                      
    <tr ng-repeat="product in products" ng-show="{{showAll}}||product.showGridRow">
      <td>{{product.productId}}</td>
      <td>{{product.productName}}</td>
      <td>{{product.topic}}</td>
      <td>{{product.Active}}</td>
      <td>
        <input type="button" value="..." ng-click="toggleReadings(product)" >
      </td>
    </tr>
  </tbody>
</table>

我的问题:

仅当在 toggleReadings() 函数的开头放置断点并调试代码时,这才有效。 “正常”运行代码时没有任何反应。 我错过了什么? 谢谢!

最佳答案

试试这个:https://jsfiddle.net/o5aofze1/

$scope.showAll = true;
$scope.products = [{
  productId: 2,
  productName: 'asd',
  topic: '123',
  Active: 'true'
}, {
  productId: 3,
  productName: 'asdada',
  topic: '213123',
  Active: 'false'
}]

$scope.toggleReadings = function toggleReadings(product) {
  if ($scope.productToShow == product) {
    $scope.productToShow = undefined;
  } else {
    $scope.productToShow = product;
  }

  $scope.showAll = !$scope.productToShow;

};

过滤器为:ng-show="showAll||productToShow == product"

关于javascript - Angularjs - 隐藏表格行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35018277/

相关文章:

javascript - 如何在 Bootstrap 导航栏中为桌面和移动设备订购元素

javascript - jQuery 在当前行的 div 上添加行号作为类

javascript - 在动态加载的脚本中访问 google ga var

html - 延迟加载 Angular View

javascript - 如何使用 angularJS 在多个项目之间共享代码

javascript - Chrome 扩展内容脚本之间的 postMessage 安全性如何?可能的替代方案?

javascript - 如何检查 document.styleSheets[i] 对象中是否存在属性 cssRules?

javascript - 当模型更改时如何在 md-select 上触发 ng-change ?

angularjs - 为什么 AngularJS 中模型改变时不调用 ngModel 的 $render ?

javascript - 等待测试中的 stateChange