我正在前端使用AngularJS编写应用程序。我希望通过任何单词/字段搜索表格;一切都有一个搜索框。我试着按照这个傻瓜的工作示例:http://plnkr.co/edit/aIuSDYlFbC4doW6pfsC9?p=preview
这是我前端的代码:
angular.module("MesaViewer");
这是在我的主控制器内:
<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>
我该怎么办?谢谢
答案 0 :(得分:4)
在您的范围内创建一个过滤变量,如下所示:
$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"}
];
注意:我真的推荐我之前链接过的教程。如果你不读它,那么至少要使用表头。
答案 1 :(得分: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 :(得分:0)
试试这个。
<强> 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;
};