【问题标题】:AngularJS filter search not working at allAngularJS过滤器搜索根本不起作用
【发布时间】:2015-10-02 09:35:00
【问题描述】:

我正在前端使用 AngularJS 编写应用程序。我想通过任何单词/字段搜索表格;一个搜索框搜索所有内容。我试图遵循这个 plunker 的工作示例:http://plnkr.co/edit/aIuSDYlFbC4doW6pfsC9?p=preview

这是我在前端的代码:

<div class = "row">
   <label>Search: <input ng-model="query"></label>
</div>

<div class = "row">
  <table ng-repeat="post in posts | orderBy: sort | filter: search">
    <tr>
       <td> {{index(post)}} </td>

       <td> {{post.title}} </td>

       <td> {{post.author}} </td>

       <td> {{post.content}} </td>

       <td> {{post.date | date: "d/M/yyyy"}} </td>
    </tr>
  </table>
</div>

这是在我的主控制器内部:

'use strict';
 $scope.posts = posts.posts;

 $scope.search = function (row) {
    return (angular.lowercase(row.author).indexOf($scope.query || '') !== -1 ||
    angular.lowercase(row.title).indexOf($scope.query || '') !== -1 ||
    angular.lowercase(row.content).indexOf($scope.query || '') !== -1 ||
    angular.lowercase(row.date).indexOf($scope.query || '') !== -1 ||
 };

我该怎么办?谢谢

【问题讨论】:

  • filter: query。我还会在orderBy 之前通过filter 传递数组
  • 您发布的示例似乎使用 angular 1.0.3;我怀疑任何新项目都在使用这个版本。过滤器的角度发生了巨大变化,这看起来不像是我期望在现代角度版本中工作的功能。
  • 另外,您似乎在该过滤器代码中有一些语法错误,即缺少关闭 ),以及后面没有选项的尾随 ||
  • @Phil:我知道 orderBy 与 filter 上的顺序无关紧要,它会按照现在的方式工作。
  • @VSO 它可能不会产生功能上的差异,但给orderBy 更少的项目排序会使其更快

标签: angularjs filter angularjs-scope angularjs-ng-repeat angularjs-filter


【解决方案1】:
<div class = "row">
   <label>Search: <input ng-model="query"></label>
</div>

<div class = "row">
    <table ng-repeat="post in posts | orderBy: sort | filter: query">
        <tr>
            <td> {{index(post)}} </td>
            <td> {{post.title}} </td>
            <td> {{post.author}} </td>
            <td> {{post.content}} </td>
            <td> {{post.date | date: "d/M/yyyy"}} </td>
        </tr>
    </table>
</div>

【讨论】:

  • 请说明您所做的更改以及错误的原因。这将有助于未来可能遇到相同问题的访问者了解该怎么做。
【解决方案2】:

试试这个。

HTML:

<label>Search:
  <input ng-model="searchKeyword" ng-model-options="{debounce:1000}">
</label>

<table ng-repeat="post in posts | orderBy: sort | filter: search">
  <tr>
    <td> {{post.title}} </td>
    <td> {{post.author}} </td>
    <td> {{post.content}} </td>
    <td> {{post.date}} </td>
  </tr>
</table>

控制器:

$scope.posts = [{
  title: 'Title one ',
  author: 'Author one ',
  content: 'Content one ',
  date: new Date('10.10.2010')
}, {
  title: 'Title two ',
  author: 'Author two ',
  content: 'Content two ',
  date: new Date('11.11.2011')
}, {
  title: 'Title three ',
  author: 'Author three ',
  content: 'Content three ',
  date: new Date('12.12.2012')
}];

$scope.searchKeyword = '';
$scope.sort = 'title';

var authorMatch, titleMatch, contentMatch, dateMatch;

$scope.search = function(row) {
  authorMatch = row.author.toLowerCase().includes($scope.searchKeyword);
  titleMatch = row.title.toLowerCase().includes($scope.searchKeyword);
  contentMatch = row.content.toLowerCase().includes($scope.searchKeyword);
  dateMatch = row.date.toString().toLowerCase().includes($scope.searchKeyword);

  return authorMatch || titleMatch || contentMatch || dateMatch;
};

去抖动选项在here 中进行了解释。包含函数是 JavaScript 中的新功能,并在 here 进行了解释。

【讨论】:

  • 谢谢。这种方法是否必须使用 Angular Strap?因为我收到“TypeError:无法读取未定义的属性'toLowerCase'”错误。 @Sofre Garevski
  • 还有这个错误:“TypeError: Cannot read property 'includes' of undefined”@Sofre Garevski
  • "TypeError: Cannot read property 'toLowerCase' of undefined" - 表示作者或标题或内容或日期未定义。从这里出现第二个错误“TypeError:无法读取未定义的属性'includes'”。确保你的数据是一致的,或者改进方法,以便它首先检查数据是否存在。
  • 我不明白为什么它们可能是未定义的。因为我仔细检查了所有数据名称。 @Sofre Garevski
  • 但我绝对明白为什么这会起作用@Sofre Garevski
【解决方案3】:

像这样在你的范围内创建一个过滤器变量:

$scope.myFilter = "defaultFilterValue";

将您的过滤器绑定到这样的搜索字段:

    <input type="text" ng-model="myFilter"></input>

像这样将过滤器放在你的 html 中:

  <table ng-repeat="post in posts | orderBy: sort | filter: myFilter">
    <tr>
       <td> {{index(post)}} </td>

       <td> {{post.title}} </td>

       <td> {{post.author}} </td>

       <td> {{post.content}} </td>

       <td> {{post.date | date: "d/M/yyyy"}} </td>
    </tr>
  </table>

然后……哦等等,就是这样。如果您有任何问题,请告诉我。 Tutorial here 正是您的问题。


编辑:此代码已经过测试,因此请按原样使用它并从那里开始。 html:

<div class = "row">
    <label>Search: <input type="text" ng-model="myFilter"></input></label>
</div>

<div class = "row">
    <table ng-repeat="post in posts | orderBy: sort | filter: myFilter">
        <tr>
            <td> {{index.post}} </td> //you had a weird formatting mistake here

            <td> {{post.title}} </td>

            <td> {{post.author}} </td>

            <td> {{post.content}} </td>

            <td> {{post.date | date: "d/M/yyyy"}} </td>
        </tr>
    </table>
</div>

JS:

$scope.myFilter = "";

$scope.posts = [
        {index: 1, author: "Mark Twain", title: "Tom Sawyer", description: "And den Jim said some racist stuff", date:"02/05/1870"},
        {index: 2, author: "Eirch Remarque", title: "Western Front", description: "Our lost generation, bawww", date: "03/17/1955"}
];

注意:我真的推荐我之前链接的教程。如果您不阅读它,那么至少使用表头。

【讨论】:

  • 如果默认为no filter,则不需要默认过滤器值
  • 所以我把之前在控制器中的所有东西都拿出来了,然后把 $scope.myFilter = "";并将过滤器绑定到搜索字段并将过滤器放在html中。但它不工作? @VSO
  • 我还有什么遗漏吗? @菲尔
  • @VSO 谢谢你。我已阅读教程并多次查看代码。这个解决方案对我来说不是 100% 有效。我不知道为什么。有时搜索有效,它会按预期提取结果。有时它不会。比如我的post.title是B,搜索就能找到。当它是 A 或 C 或 D 时,它不会找到它们。会不会是 post.date 以某种方式干扰它?顺便说一句,我正在使用 MongoDB 来存储结果
  • 你熟悉
    {{yourPostData | json}}>
    标签约定来测试你的帖子数据?您可以使用它来查看返回的数据。如果每个字段都返回了一些数据,那么您的 ng-repeat 应该可以工作。如果您的 ng-repeat 有效,则过滤器应该可以工作。基本上,我要说的是,只要您获取数据,我认为日期不是问题。这不像您将 BSON 带回给客户。附言我不知道问题是什么,这些只是建议。我可以相当肯定地说,它与日期无关,只要它在那里。
猜你喜欢
  • 2015-03-12
  • 2018-08-23
  • 2018-11-28
  • 2016-08-04
  • 2013-09-24
  • 1970-01-01
  • 2018-12-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多