如何使用控制器angularjs过滤数据?

时间:2015-08-05 13:14:39

标签: javascript angularjs

我有3页第1页samsunghome,其中包含与三星移动服务的复选框列表。第2页nokiahome,其中包含与诺基亚服务的复选框列表。三星和诺基亚商店的页面列表。如果用户来自三星家居并选择了一些服务我需要只显示具有该特定服务的三星商店。如果用户来自nokiahome并选择了一些服务我只需要显示具有该特定服务的诺基亚商店。我也有搜索框,当我来自第1和第2页列表页面时应该显示选定的服务选择商店。我已经做了一些逻辑,但它不工作列表控制器我需要检查选定的商店和选定的服务,一些人帮助我



.controller('SamsungServicesCtrl', function ($scope,$filter,$location) {
   var service = ["Regular Service", "Settings Faults", "Software Faults", "Hardware Faults"];
   var services = ["General services", "If a phone doesn’t switch-on, it is called a dead phone, SIM card does not get detected", "Hardware Faults"];
   var icons = ["images/arrow.svg","images/arrow.svg","images/arrow.svg"];
     $scope.items= [] ;
        for(var i=0;i<service.length;i++)
        	{
        		var modal = {
        			name:service[i],
        			subname:services[i],
        			icon:icons[i],
        			selected:false
        	      }; 
                   
                $scope.items.push(modal);        
        	}
            scope.check = function()
            {
                  var checkedItems = [];
                   for(var i=0;i<$scope.items.length;i++){
                      if($scope.items[i].selected){
                         checkedItems.push($scope.items[i].name);
                         }
                        }
               //$location.path('menulisting/listing');
                        var model ="samsung";
    //i have checked Regular Service, Settings Faults
    $location.path('menulisting/listing').search({filter: model +','+checkedItems});
             }
        	
})
Nokia service controller
.controller('NokiaServicesCtrl', function ($scope,$filter,$location) {
var service = ["Regular Service", "Settings Faults", "Software Faults", "Hardware Faults"];
var services = ["General services", "If a phone doesn’t switch-on, it is called a dead phone, SIM card does not get detected", "Hardware Faults"];
var icons = ["images/arrow.svg","images/arrow.svg","images/arrow.svg"];
     $scope.items= [] ;
        for(var i=0;i<service.length;i++)
        	{
				var modal = {
        		name:service[i],
        		subname:services[i],
        		icon:icons[i],
        		selected:false
        		}; 
                   
                $scope.items.push(modal);        
        	}
   scope.check = function()
            {
                var checkedItems = [];
                for(var i=0;i<$scope.items.length;i++){
                if($scope.items[i].selected){
                checkedItems.push($scope.items[i].name);
                }
              }
                $location.path('menulisting/listing');
            }
        	
})
listing controller
.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)
       {  
			var filter= $location.search().filter;
			console i am getting
			//samsung,Regular Service, Settings Faults
			$scope.dealers=[];
			for(var i=0;i<data.length;i++){
				var temp=data[i].S_Store.split(',');
				for(var j=0;j<temp.length:j++){
					if(temp[j]===filter)
					   $scope.dealers.push(data[i]);
					}
				}
        			
        });
}])
.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;
    }
});
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,Settings Faults,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:"Regular Service,Settings Faults,Software Faults,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,Settings Faults,Software Faults,Hardware Faults"
Store_long_description: "Undertake all kind of nokia,samsung mobiles"
Store_short_description: "Undertake all kind of nokia,samsung mobiles"
&#13;
samsung page hml
    <div  ng-controller="SamsungServicesCtrl">
     <ion-content> 

         <li class="item item-checkbox"  ng-repeat="item in items" >
    	  <img src="{{item.icon}}"  style="float:left;height:30px;width:30px;padding-right:5px;" > 
    	 <label  class="checkbox checkbox-energized"  >
           <input type="checkbox"  style="float:right;"   ng-model="item.selected"  >
         </label>
    	 {{item.name}}
    	  <p style="font-size:10px;  padding-left:0px;">  {{item.subname}}</p>
        </li>
       <div class="padding">
        <button class="button button-full button-energized"  value="submit" ng-click="check()">Apply
    	</button>
       </div>
      
     </ion-content>
     </div>

    Nokia Page html
    <div  ng-controller="NokiaServicesCtrl">
     <ion-content> 

         <li class="item item-checkbox"  ng-repeat="item in items" >
    	  <img src="{{item.icon}}"  style="float:left;height:30px;width:30px;padding-right:5px;" > 
    	 <label  class="checkbox checkbox-energized"  >
           <input type="checkbox"  style="float:right;"   ng-model="item.selected"  >
         </label>
    	 {{item.name}}
    	  <p style="font-size:10px;  padding-left:0px;">  {{item.subname}}</p>
        </li>
       <div class="padding">
        <button class="button button-full button-energized"  value="submit" ng-click="check()">Apply
    	</button>
       </div>
      
     </ion-content>
     </div>
    listing page
    <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>
    </div>
     <div class="list card"  data-ng-repeat="dealer in dealers || customFil: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>
&#13;
&#13;
&#13;

我附上了我想要过滤S_Store和S_Services的代码

1 个答案:

答案 0 :(得分:0)

您可以将过滤条件作为参数传递:

$location.path('menulisting/listing').search({model: 'nokia', items:checkedItems});

然后在控制器中获取这些参数:

var filter = $location.search().model;
var items = $location.search().items;

并过滤您的经销商:

$scope.dealers=[];
for(var i=0;i<data.length;i++){
    var temp=data[i].S_Store.split(',');
    for(var j=0;j<temp.length:j++){
        if(temp[j]===filter){
           data[i].S_Services=items;
           $scope.dealers.push(data[i]);                   
        }
    }
}

注意,你忘记了&#39; $&#39;在scope.check之前