1

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

<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

4

3 回答 3

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

于 2015-09-01T12:59:34.907 回答
0

为了过滤,lastName您可以简单地更新过滤器调用以反映它

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

这将过滤rowCollectionlastName

但是,如果您想要对姓氏进行更严格的过滤,我相信您会寻求为您的应用程序定义更精确的过滤器。之类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;
  }
})

希望对您有所帮助!

于 2015-09-01T12:59:52.370 回答
0

只要有,

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

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

柱塞

于 2015-09-01T12:54:50.203 回答