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

标签 javascript jquery css json angularjs

Plunker sample

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

索引.js

var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
  $scope.name = 'World';
  $scope.isArray = angular.isArray;
  $scope.data = [{
    "Id": null,
    "Title": "US",
    "Description": "English - United States",
    "Values": [{
      "Id": 100,
      "LanId": 1,
      "KeyId": 59,
      "Value": "Save"
    }]
  }, {
    "Id": null,
    "Title": "MX",
    "Description": "test",
    "Values": [{
      "Id": 100,
      "LanId": 1,
      "KeyId": 59,
      "Value": "Save"
    }]
  }, {
    "Id": null,
    "Title": "SE",
    "Description": "Swedish - Sweden",
    "Values": [{
      "Id": 100,
      "LanId": 1,
      "KeyId": 59,
      "Value": "Save"
    }]
  }]
  $scope.cols = Object.keys($scope.data[0]);
  $scope.notSorted = function(obj) {
    if (!obj) {
      return [];
    }
    return Object.keys(obj);
  }
});

index.html

<table border=1 style="margin-top: 0px !important;">
  <thead>
    <tr>
      <th ng-repeat="(k,v) in data[0]">{{k}}</th>
    </tr>
  </thead>
  <tbody>
    <tr ng-repeat="item in data">
      <td ng-repeat="(prop, value) in item" ng-init="isArr = isArray(value)">
        <table ng-if="isArr" border=1>
          <thead>
            <tr>
              <td>
                <button ng-click="expanded = !expanded" expand>
                  <span ng-bind="expanded ? '-' : '+'"></span>
                </button>
              </td>
            </tr>
            <tr>
              <th ng-repeat="(sh, sv) in value[0]">{{sh}}</th>
            </tr>
          </thead>
          <tbody>
            <tr ng-repeat="sub in value" ng-show="expanded">
              <td ng-repeat="(sk, sv) in sub">{{sv}}</td>
            </tr>
          </tbody>
        </table>
        <span ng-if="!isArr">{{value}}</span>
      </td>
    </tr>
  </tbody>
</table>

最佳答案

您可以过滤掉只有 null 值的列:

JavaScript

$scope.cols = Object.keys($scope.data[0]).filter(function(col) {
    return $scope.data.some(function(item) {
      return item[col] !== null;
    });
});

并在模板中检查是否应呈现此列:

HTML

<table border=1 style="margin-top: 0px !important;">
    <thead>
        <tr>
            <!-- Iterate over non-null columns -->
            <th ng-repeat="col in cols">{{col}}</th>
        </tr>
    </thead>
    <tbody>
        <tr ng-repeat="item in data">
            <!-- Use ngIf to hide redundant column -->
            <td ng-if="cols.indexOf(prop)>=0" ng-repeat="(prop, value) in item" ng-init="isArr = isArray(value)" >
...

笨蛋

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

关于javascript - 如果使用angular js的任何属性的所有json值都为null,如何隐藏表列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31502823/

相关文章:

javascript - 为什么在没有发出双击时进入 dblclick 处理程序?

javascript - 如何访问值为未知整数的 JS 对象属性?

javascript - 从 OnClicks 目标 react 中获取嵌套元素

javascript - 如何在单击 JavaScript 函数中的按钮时添加文本 "Please wait a while..."

javascript - 提交后如何禁用输入字段

html - 第一个 div 使所有其他 div 移出位置

html - 所有 child 的 CSS 非选择器

javascript - 了解幻想世界 `ap`

javascript - 使用 jquery 为 "some selects with same parent class"内的所有选项添加类?

css - 使用CSS设置div的绝对位置