javascript – 如果使用angular js的任何属性的所有json值为null,如何隐藏表列

前端之家收集整理的这篇文章主要介绍了javascript – 如果使用angular js的任何属性的所有json值为null,如何隐藏表列前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Plunker sample

How to hide table column if all json value is null for any property
using angular js

index.js

  1. var app = angular.module('plunker',[]);
  2. app.controller('MainCtrl',function($scope) {
  3. $scope.name = 'World';
  4. $scope.isArray = angular.isArray;
  5. $scope.data = [{
  6. "Id": null,"Title": "US","Description": "English - United States","Values": [{
  7. "Id": 100,"LanId": 1,"KeyId": 59,"Value": "Save"
  8. }]
  9. },{
  10. "Id": null,"Title": "MX","Description": "test","Title": "SE","Description": "Swedish - Sweden","Value": "Save"
  11. }]
  12. }]
  13. $scope.cols = Object.keys($scope.data[0]);
  14. $scope.notSorted = function(obj) {
  15. if (!obj) {
  16. return [];
  17. }
  18. return Object.keys(obj);
  19. }
  20. });

的index.html

  1. 最佳答案
    您可以过滤掉只有空值的列:

  2. JavaScript

  3. $scope.cols = Object.keys($scope.data[0]).filter(function(col) {
  4.     return $scope.data.some(function(item) {
  5.       return item[col] !== null;
  6.     });
  7. });
  8. 并检查模板是否应该呈现此列:

  9. HTML

  10. Plunker

    http://plnkr.co/edit/PIbfvX6xvX5eUhYtRBWS?p=preview

  11. 猜你在找的CSS相关文章