【问题标题】:Applying sort and filter to an Angular table对 Angular 表应用排序和过滤
【发布时间】:2017-02-03 00:01:07
【问题描述】:

我下载了一个 Angular 演示应用程序来学习和学习 Angular 基础知识。我在对数据表应用过滤器和排序时遇到问题。我参考了一些例子,不确定是否正确。

我的桌子是:

<div class="widget-content" ng-controller="getAllBenchersController">

                <table ng-table="talentPoolList" show-filter="true" class="table table-striped table-bordered">
                    <tr ng-repeat="employee in data | filter: testFilter">

                        <td data-title="'#'">{{$index + 1}}</td>
                        <td data-title="'Employee ID'" sortable="'empno'" filter="{ 'empno': 'text' }">
{{employee.employee.employeeNo}}
</td>
<td data-title="'First Name'" sortable="'employee.employee.firstName'" filter="{ 'employee.employee.firstName': 'text' }">
                            {{employee.employee.firstName}}
                        </td>
  </tr>
                </table>

            </div>

控制器:

myApp.controller('getAllBenchersController', ['$scope', 'employeeTalentPoolServices', function ($scope, employeeTalentPoolServices) {
    employeeTalentPoolServices.getAllBenchers().then(function (result) {
        $scope.data = result.data;
$scope.testFilter = function (item) {
            return (item.state.state.toLowerCase() === 'available' || item.state.state.toLowerCase() === 'blocked');
        }
   });

我不知道它是如何工作的。任何人都可以解释并提供解决此问题的解决方案吗?

提前致谢

【问题讨论】:

  • 你能分享你的json数据吗?
  • 它不是来自 json。我从后端 API 获取它
  • 你能用你得到的result.data的结果更新你的问题吗?
  • @Phoenix 你解决了吗?

标签: angularjs angularjs-ng-repeat


【解决方案1】:

因此,为了解释您的演示示例,我创建了一个类似的示例,让您了解您的问题中发生了什么。

我们有一个不同状态的员工数据,所以当我想显示员工列表时,我只想显示“公平”和“好”,所以我编写了一个传递给 filter 指令的过滤函数来告诉哪些项目应该从列表中显示出来。

如果您观察该功能,它会从列表中获取每个项目并检查状态以匹配所需的显示状态。

查看演示,希望这能让您清楚地了解演示应用中发生的情况。

var app = angular.module('app',[]);
app.controller('getAllBenchersController',function($scope,$filter){
  
  $scope.data = {employee:[
    
    {'no':1,'name':'max','status':'bad'},
    {'no':2,'name':'fox','status':'good'},
    {'no':3,'name':'juno','status':'bad'},
    {'no':4,'name':'pet','status':'fair'},
    {'no':5,'name':'xyz','status':'good'},
    {'no':6,'name':'shit','status':'bad'},
    {'no':7,'name':'doggy','status':'fair'},
    {'no':8,'name':'hmmm','status':'bad'} 
  ]}
  
  //this test filter is a function which allows me to decide which data should be displayed in this case I've used 'fair' & 'good' status employees to be displayed...
  $scope.testFilter = function (item) {
            return (item.status.toLowerCase() === 'good' || item.status.toLowerCase() === 'fair');
        };
   
  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" class="widget-content" ng-controller="getAllBenchersController">

                <table>
          <tr ng-repeat="emp in data.employee | filter: testFilter">
                      <td>{{$index+1}} </td>
                      <td> {{emp.no}}</td>
                      <td>{{emp.name}} </td>
  </tr>
                </table>

            </div>

【讨论】:

  • 是的,我之前整理过。但你的榜样有助于获得知识。 Tnx :)
  • 干杯!任何时候快乐的编码伙伴:)
  • 嗨,伙计,如果你有时间,请检查这个问题:) stackoverflow.com/questions/40504608/…
猜你喜欢
  • 2019-03-31
  • 2020-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-04
  • 1970-01-01
  • 1970-01-01
  • 2019-10-13
相关资源
最近更新 更多