简体   繁体   English

AngularJS ng通过对象和文本框中的一个属性重复过滤

[英]AngularJS ng repeat filter by one attribute in object and a text box

I have this issue and I would like to get ideas on how to solve it in the best approach. 我遇到了这个问题,我想获得有关如何以最佳方法解决问题的想法。 I need to filter ng-repeat results with a text box and when hashtag is being used as the first sign I need the filter to work only on one specific column. 我需要使用文本框过滤ng-repeat结果,当将hashtag用作第一个符号时,我需要过滤器仅在一个特定的列上工作。 for example: 例如:

  1. by default = filter only by text inserted in the search box - meaning the filter should work on all attributes of each user. 默认情况下=仅按在搜索框中插入的文字进行过滤-表示该过滤器应适用于每个用户的所有属性。
  2. Filter by only one attribute when user enter Hashtag: value. 当用户输入Hashtag:值时,仅按一个属性过滤。 For instance: (instead of regular value user insert in search box) 例如:(而不是用户在搜索框中插入常规值)

     #ID: 33 #ID:33 

    will filter only users who have id which contains 33. 将仅过滤ID包含33的用户。

     #date: 1/12/2014 #date:1/12/2014 

    wil filter only users who have the following date 仅过滤具有以下日期的用户

var users = [{
  "name": "Bruce",
  "city": "Thailand",
  "date": "1/12/2014",
  "id": 3376848
}, {
  "name": "Frank",
  "city": "Bangladesh",
  "date": "11/10/2014",
  "id": 4482771
}, {
  "name": "Judith",
  "city": "Philippines",
  "date": "13/9/2015",
  "id": 4981921,

}, {
  "name": "Earl",
  "city": "Ukraine",
  "date": "21/6/2015",
  "id": 4024523,
}];

What you need to do is watch a variable in scope (I use "search" below). 您需要做的是观察范围内的变量(我在下面使用“搜索”)。

When it starts with "#", search only on that property. 当它以“#”开头时,仅在该属性上搜索。 When it doesn't start with "#", search on all properties. 如果它不是以“#”开头,​​请搜索所有属性。

I've created a new array called "filteredUsers" because we don't want to modify the original "users" array. 我创建了一个名为“ filteredUsers”的新数组,因为我们不想修改原始的“ users”数组。

To make things easier, we're using Angular's filter service (remember to include $filter in your controller). 为了使事情变得简单,我们使用Angular的filter服务(请记住在控制器中包括$filter )。

 var app = angular.module("app", []) .controller("controller", function($scope, $filter) { var users = [{ "name": "Bruce", "city": "Thailand", "date": "1/12/2014", "id": 3376848 }, { "name": "Frank", "city": "Bangladesh", "date": "11/10/2014", "id": 4482771 }, { "name": "Judith", "city": "Philippines", "date": "13/9/2015", "id": 4981921, }, { "name": "Earl", "city": "Ukraine", "date": "21/6/2015", "id": 4024523, }]; $scope.search = ""; $scope.filteredUsers = users; $scope.$watch("search", function(newVal, oldVal) { // if the search value starts with '#' if (newVal.substring(0, 1) === "#") { var searchProperty = newVal.substring(1, newVal.indexOf(":")); // string between '#' and first ':' var searchValue = $scope.search.substring(newVal.indexOf(":") + 1).trim(); // string after first ':'. will chuck some errors if there is no ':' but you can check this yourself // if there is both a property and value do the search on a single property if (searchProperty && searchValue) { // Angular's filter service requires an object (eg {name: "Bruce"}) but if we try {searchProperty: searchValue} it will filter on a property called "searchProperty" not the value of the variable so we create a new object and use that var searchObject = {}; searchObject[searchProperty] = searchValue; $scope.filteredUsers = $filter('filter')(users, searchObject, false); } else { // Else show no users. It's up to you what you do here. You might want to show all users while the user is typing $scope.filteredUsers = []; } } else { // if the search value doesn't start with '#', use filter on the search value will look through all properties $scope.filteredUsers = $filter('filter')(users, newVal, false); } }); }); 
 pre { margin: 0; } 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.min.js"></script> <div ng-app="app" ng-controller="controller"> Search: <input type="text" ng-model="search" /> <h4>User</h4> <pre ng-repeat="user in filteredUsers"> {{user | json}} </pre> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM