如何过滤angularjs中的数组值?

时间:2015-08-07 05:16:33

标签: javascript jquery angularjs

我有两页主页和列表页面。主页包含带有复选框的服务列表用户一旦选择了一些服务并提交,页面重定向到列表页面。我已将选定的值存储在localstorage中并传递到列表页面,一切正常但过滤器不起作用。在这里,我正在过滤S_Services它无法正常工作。如果我只选择常规服务它的工作如何筛选,但如果我选择常规服务,软件错误一些像这样我选择它不工作。如何在S_Services中显示任何一个匹配?

有人可以帮助我吗?

.controller('ListingCtrl', [
 '$scope', '$http', '$location', '$window', '$filter','$ionicPopover','$ionicLoading',
   function($scope, $http, $location, $window, $filter, $ionicPopover, $ionicLoading) {
	$scope.$watch(function() {
		return window.localStorage.getItem("searchstore")
		},
		function(searchstore) {
					$scope.query = searchstore;
				console.log($scope.query);
		});

	$scope.clearSearch = function() {
			window.localStorage.setItem("searchstore", "");
		};
	
	$http.get('*****').success(function(data,dealers,response)
			{  
			$scope.dealers = data;
            console.log(dealers);
			
			});
}])
    //in console i am getting list of dealers
    console i am getting like this
    1: Object
    $$hashKey: "object:28"
    Dealer_id: "55b24172c7d354f30cda0e7f"
    Legal_Name: "Adtiya Samsung Store"
    S_Address: Object
    address_line1: "No.80, Marks Road"
    area: "madiwala"
    city: "Bangalore"
    state: "Karnataka"
    zipcode: "560068"
    S_Date_add: "2015-07-24T13:45:23.927Z"
    S_Email_id: "aditiya@gmail.com"
    S_Store: "samsung"
    Store_Name: "Adtiya Samsung Store"
    S_Services:"Regular Service,Software Faults,Hardware Faults"
    Store_long_description: "Undertake all kind of samsung mobiles"
    Store_short_description: "Undertake all kind of samsung mobiles"
    2: Object
    $$hashKey: "object:28"
    Dealer_id: "55b24172c7d354f30cda0e7g"
    Legal_Name: "sri shakthi mobile services"
    S_Address: Object
    address_line1: "3rd street"
    area: "madiwala"
    city: "Bangalore"
    state: "Karnataka"
    zipcode: "560068"
    S_Date_add: "2015-07-24T13:45:23.927Z"
    S_Email_id: "rajs@gmail.com"
    S_Store: "nokia"
    Store_Name: "sri shakthi mobile service"
    S_Services:"Settings Faults,Regular Service,Hardware Faults"
    Store_long_description: "Undertake all kind of nokia mobiles"
    Store_short_description: "Undertake all kind of nokia mobiles"
    3: Object
    $$hashKey: "object:28"
    Dealer_id: "55b24172c7d354f30cda0e7h"
    Legal_Name: "sun mobile service center"
    S_Address: Object
    address_line1: "23rd main ,2nd cross"
    area: "madiwala"
    city: "Bangalore"
    state: "Karnataka"
    zipcode: "560068"
    S_Date_add: "2015-07-24T13:45:23.927Z"
    S_Email_id: "sprtive23@gmail.com"
    S_Store: "nokia,samsung"
    Store_Name: "sun mobile service center"
    S_Services:"Regular Service"
    Store_long_description: "Undertake all kind of nokia,samsung mobiles"
    Store_short_description: "Undertake all kind of nokia,samsung mobiles
<div class="bar bar-header item-input-inset">
      <label class="item-input-wrapper" >
        <i class="icon ion-ios-search placeholder-icon"></i>
        <input type="text" placeholder="Search" ng-model="query" >
      </label>
    	<button class="button button-icon ion-ios-close-outline"     ng-click="clearSearch()" id="iconcolor"  >clear</button>
    </div>
    <div class="list card" data-ng-init="nearme()" data-ng-repeat="dealer in dealers |  filter:query ">
         <div class="item item-thumbnail-left item-icon-right" href="#">
          <h2>{{dealer.Store_Name}}</h2> 
          <p>{{dealer.S_Address.area}} {{dealer.S_Address.city}}</p> 
    	  <p>{{dealer.S_Services}}</p>
    	 </div>	
    	 
    </div>

1 个答案:

答案 0 :(得分:0)

您必须创建一个自定义过滤器,以根据dealer对象的属性进行过滤,如下所示。

     .filter('customFil', function () {

return function (dealers, query) {
    var obj = [];

    for (var i = 0; i < dealers.length; i++) {
        var tokenisedString = query.split(",");
        var isPresent = false;
        for (var j = 0; j < tokenisedString.length; j++) {
            if (dealers[i].S_Services.toUpperCase().includes(tokenisedString[j].toUpperCase())) {
                obj.push(dealers[i]);
                break;
            }
        }
    }
    return obj;
}
});

Here是一个有效的例子