简体   繁体   English

如何在angularJS中隐藏表格中的行?

[英]How to hide a row in a table in angularJS?

HTML code to display table - HTML代码显示表 -

    <table class="table table-bordered table-striped table-condensed">
        <thead class="TableHeader"> 
            <tr>
                <th>Role</th>
                <th>Name</th>
                <th>ID</th>
            </tr>
        </thead>
        <tbody> 
            <tr ng-repeat="i in allCustomerInfoArray track by $index" ng-click="showCustomerinfo($index)">
                <td>{{i.role }}</td>
                <td>{{i.name}}</td>
                <td>{{i.id}}</td>
            </tr>
        </tbody>
    </table>

Angular JS code to display the customer info in the console log - Angular JS代码在控制台日志中显示客户信息 -

    $scope.showCustomerinfo=function(index) {
        console.log("table clicked row -- "+index);
        console.log("DOB -- "+$scope.allCustomerInfoArray[index].role);
        console.log("Age -- "+$scope.allCustomerInfoArray[index].name);
        console.log("Age -- "+$scope.allCustomerInfoArray[index].id);
        console.log("DOB -- "+$scope.allCustomerInfoArray[index].dob);
        console.log("Age -- "+$scope.allCustomerInfoArray[index].age);
    }

Now I want to modify the above code to hide the rows where the customer role is "XXX". 现在我想修改上面的代码来隐藏客户角色为“XXX”的行。 Please let me know how to achieve it. 请让我知道如何实现它。

Note - I do not want to delete the customer with role "XXX" from allCustomerInfoArray. 注 - 我不想从allCustomerInfoArray中删除角色为“XXX”的客户。

You can use ng-hide to do this. 您可以使用ng-hide来执行此操作。 Like so: 像这样:

<tr ng-repeat="i in allCustomerInfoArray track by $index"
    ng-click="showCustomerinfo($index)"
    ng-hide="i.role === 'XXX'">

If you want to pull the tag out of the DOM altogether, you can use ng-if : 如果你想完全从DOM中取出标签,你可以使用ng-if

<tr ng-repeat="i in allCustomerInfoArray track by $index"
    ng-click="showCustomerinfo($index)"
    ng-if="i.role !== 'XXX'">

But be warned, performance of ng-if isn't as good as ng-hide , but you will pull the tr tag completely out of the DOM if that's your thing. 但要注意, ng-if性能不如ng-hide ,但是如果那是你的话,你会将tr标签完全拉出DOM。

<table class="table table-bordered table-striped table-condensed">
    <thead class="TableHeader"> 
        <tr>
            <th>Role</th>
            <th>Name</th>
            <th>ID</th>
        </tr>
    </thead>
    <tbody> 
        <tr ng-repeat="i in allCustomerInfoArray track by $index" ng-click="showCustomerinfo($index)" ng-if="i.role!='XXX'>
            <td>{{i.role }}</td>
            <td>{{i.name}}</td>
            <td>{{i.id}}</td>
        </tr>
    </tbody>
</table>

OR 要么

 ng-class=" ... ? 'class-1' : ( ... ? 'class-2' : 'class-3')"

Read more: https://en.wikipedia.org/wiki/%3F:#JavaScript 阅读更多: https//en.wikipedia.org/wiki/%3F#JavaScript

You show use ng-hide like so: 你这样显示使用ng-hide:

        <tr ng-repeat="i in allCustomerInfoArray track by $index" ng-click="showCustomerinfo($index)" ng-hide="i.role=='XXX'">

Hope it helps 希望能帮助到你

Use ng-hide='i.role=="XXX"' on the row 在行上使用ng-hide ='i.role ==“XXX”'

Or 要么

ng-show!='i.role=="XXX"' Or ng-show!='i.role ==“XXX”'或

ng-if=='i.role!="XXX"' NG-如果== 'i.role!= “XXX”'

Anyone of above works to get expected result 以上任何人都可以获得预期的结果

all answers above are correct, but you should know that ng-if will remove the object from dom. 上面的所有答案都是正确的,但你应该知道ng-if会从dom中删除对象。 ng-hide or ng-show will just hide it. ng-hide或ng-show只会隐藏它。 Additional ng-if will create a own scope. 额外的ng-if将创建一个自己的范围。

what is the difference between ng-if and ng-show/ng-hide ng-if和ng-show / ng-hide有什么区别

You could use angular filter to achieve the same and alias it with as filterCustomer to use filtered result using filterCustomer anywhere on the page. 您可以使用角度filter来实现相同的效果,并使用as filterCustomer将其替换as filterCustomer使用filterCustomer在页面上的任何位置使用过滤结果。

Markup 标记

<tbody> 
    <tr ng-repeat="i in allCustomerInfoArray | filter: {role: 'XXX' } track by $index as filterCustomer" ng-click="showCustomerinfo($index)">
        <td>{{i.role }}</td>
        <td>{{i.name}}</td>
        <td>{{i.id}}</td>
    </tr>
</tbody>

More convenient way to implement a filter would be, use it in controller so that filter will not get evaluate of each digest cycle. 实现过滤器的更方便的方法是在控制器中使用它,以便过滤器不会评估每个摘要周期。 Whenever you retrieve a collection at that time only you could filter it out. 每当你在那个时候检索一个集合时,你只能过滤掉它。

$http.get('/api/getentity')
.then(function(res){
   $scope.filterCustomers = $filter('filter')(res.data, {role: 'XXX' }); //filtered result
}, 
function(res){
   console.log("Log error");
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM