如何按列过滤表格?

时间:2015-09-01 12:45:31

标签: angularjs smart-table

我正在尝试按列过滤表,这是表:

<tr ng-repeat="row in rowCollection | filter: { person.lastName : lastName}">
        <td>{{row.person.firstName}}</td>
        <td>{{row.person.lastName}}</td>
        <td>{{row.person.birthDate | date:'shortDate'}}</td>
        <td>{{row.person.balance}}</td>
        <td>{{row.person.email}}</td>
</tr>

数据如下所示:

$scope.rowCollection = [
              {person:{firstName: 'Laurent', lastName: 'Renard', birthDate: new Date('1987-05-21'), balance: 102, email: 'whatever@gmail.com'}},
              {person:{firstName: 'Blandine', lastName: 'Faivre', birthDate: new Date('1987-04-25'), balance: -2323.22, email: 'oufblandou@gmail.com'}},
              {person:{firstName: 'Francoise', lastName: 'Frere', birthDate: new Date('1955-08-27'), balance: 42343, email: 'raymondef@gmail.com'}}
];

在这种情况下,我如何按列过滤姓氏?当json超过1级时,我无法使它工作。 plunkr:http://plnkr.co/edit/AFSoGw?p=preview

3 个答案:

答案 0 :(得分:2)

您应该将该级别添加到搜索查询的模型中,因此查询输入的ng-model应为person.lastName

filter on lastname: <input type="text" ng-model="person.lastName">
    <h3>Basic Smart-Table Starter</h3>
    <table st-table="rowCollection" class="table table-striped">
        <thead>
          <tr>
              <th>first name</th>
              <th>last name</th>
              <th>birth date</th>
            <th>balance</th>
            <th>email</th>
        </tr>
        </thead>
        <tbody>
        <tr ng-repeat="row in rowCollection | filter: {'person':person}">
            <td>{{row.person.firstName}}</td>
            <td>{{row.person.lastName}}</td>
            <td>{{row.person.birthDate | date:'shortDate'}}</td>
            <td>{{row.person.balance}}</td>
            <td>{{row.person.email}}</td>
        </tr>
        </tbody>
    </table>

查看此更新的plunker

答案 1 :(得分:0)

只是,

ng-repeat="row in rowCollection | filter: lastName"

它将根据用户提供的值过滤您的完整数组。

PLUNKER

答案 2 :(得分:0)

要按lastName进行过滤,您只需更新过滤器调用以反映

<tr ng-repeat="row in rowCollection | filter:lastName">

这将按rowCollection过滤lastName

但是如果你想要一个更严格的过滤器专门用于姓氏,我相信你会为你的应用程序定义一个更精确的过滤器。类似于filterByLastName的东西,它将遍历项目并检查哪些项目的姓氏与该名称匹配。

以下内容:

.filter('filterByLastName', function () {
  return function(items, lastName) {
    var filtered = [];
    if (lastName) {
      for (var i = 0; i < items.length; i++) {
        if (items[i].lastName === lastName) {
          filtered.push[items[i]];
        }
      }
    } else {
      filtered = items;
    }
    return filtered;
  }
})

希望能帮到你!