2016-12-01 56 views
2

这是我的表NG-会显示多个值

<table class="table table-bordered table-responsive table-hover add-lineheight table_scroll"> 
     <thead> 
      <tr> 
       <th ng-hide="hidecolumn == key" ng-repeat="(key, value) in localNew[0]"> 
        {{key}} 
       </th> 
      </tr> 
     </thead> 
     <tbody> 
      <tr ng-repeat="test in localNew"> 
       <td ng-hide="hidecolumn == key" ng-repeat="(key, value) in test"> 
        {{value}} 
       </td> 
      </tr> 
     </tbody> 
    </table> 

这里hidecolumn将隐藏我的专栏,我不想显示 比如,对于我的控制器

这是我的数据

$scope.localNew = [{ 'name': 'name1', 'age': 24, "salary": 2500, "s": 5 }, { 'name': 'name2', 'age': 26, "salary": 2600, "s": 5 }]; 

而这个我的隐藏列变量

$scope.hidecolumn = "salary"; 

这工作得很好

现在我的问题是,我想隐藏多列 所以我的范围变量会像

$scope.hidecolumn = "name,salary"; 

所以,我怎么能在我的HTML表格中隐藏多个管理这个列即多个隐藏值???如果你仍然不得到我的问题让我知道我会加plunker.Thanku

回答

2

你还是使用数组来代替

:字符串

和功能检查,如果当前列应该隐藏或不:

$scope.shouldHideColumn = function(column) { 
    if ($scope.hidecolumns.indexOf(column.salary)) { 
    return true; 
    } 

    return false; 
}; 

,然后在HTML

<th ng-hide="shouldHideColumn(value)" ng-repeat="(key, value) in localNew[0]"> 
    {{key}} 
</th> 
+0

我的隐藏列是一个字符串,它将是逗号分隔的字符串,因为我写了一个指令,这将作为属性传递给该指令,它将根据该指令隐藏 –

+0

这样做感觉很奇怪,米想知道用例,但确定:) – Maxime

+0

我可以通过一个数组属性指令 –

0

你有没有尝试过这样的:

<td ng-repeat="(key, value) in test" ng-hide="key === 'name' || key === 'salary'"> 
    {{value}} 
</td> 
+0

没有这个,我不想如果我添加另一列,我需要在表中添加,我不想,,,,根据我的范围变量我的HTML表必须动态地作出反应 –

1

你也可以做到这一点的方法:

<td ng-hide="hidecolumn.indexOf(key) !== -1" ng-repeat="(key, value) in test"> 
    {{value}} 
</td> 
+0

你会解释这? –

+0

如果“hidecolumn”字符串不包含密钥,则indexOf方法返回-1。 – plvice

+0

我的隐藏列是一个字符串,它将是逗号分隔的字符串,因为我写了一个指令,这将作为属性传递给它将隐藏取决于该指令 –

2

您可以使用Array及其indexOf方法:

$scope.hidecolumn = [ "name", "salary" ]; 

//查看

<td ng-hide="hidecolumn.indexOf(key) !== -1" ng-repeat="(key, value) in test"> 
    {{value}} 
</td> 
+0

我的hiddencolumn是一个字符串,它将是逗号分隔的字符串,因为我写了一个指令,这将作为属性传递给该指令,它将隐藏取决于该指令 –

+0

对不起,先生我不能这样做,我有一个逗号分离的字符串,这是我的向导创建的,所以我可以改变那..其他任何想法我怎么能用逗号分隔字符串? –

0

你可以调用功能,如:

ng-hide="hidecolumn(key)" 

JS

var hideColumn = function(key) { 
    var fruits = ["Banana", "Orange", "Apple", "Mango"]; 
    if (fruits.indexOf(key)) === 0 return false; 
    } 

NG-会隐藏 '隐藏' 元素,如果函数返回true。

+0

我隐藏的列是一个字符串,它将是逗号分隔的字符串,因为我写了一个指令,这将作为属性传递给该指令,它将根据该指令隐藏 –

+0

Ohk thanku它解决了我的问题你可以检查这个问题http://stackoverflow.com/questions/40866246/angular-directive-add-template-on-textbox-enter-of-spacebar –

+0

如果它解决了你的问题,请你投票或标记为正确的? – Yaser

0

的indexOf工作完全正常,但如果你关心的其他地方重复同样的方法,你可能想创建一个包含阵列的方法在这样一个全球性的地方:

Array.prototype.contains = function contains(obj) { 
    for (var i = 0; i < this.length; i++) { 
     if (this[i] === obj) { 
      return true; 
     } 
    } 
    return false; 
}; 

然后创建数组:

$scope.hiddencolumns = [ "name", "salary" ]; 

那么你根本就使用这样的:

<td ng-hide="hiddencolumns.contains(key)" ng-repeat="(key, value) in test"> 
    {{value}} 
</td> 

为了您的逗号分隔字符串转换为数组使用:

var array = $scope.hidecolumn.split(','); 
+0

我的隐藏列是一个字符串,它将逗号分隔的字符串,因为我写了一个指令,这将作为属性传递给该指令,它将隐藏取决于该指令,或者如果我可以传递属性中的数组? –

+0

奥克thanku它解决了我的问题你可以检查这个问题http://stackoverflow.com/questions/40866246/angular-directive-add-template-on-textbox-enter-of-spacebar –

0

Thanku所有,我只是将我的字符串数组var array = string.split(','); ,并得到想要的结果,Thanku所有对你的帮助