繁体   English   中英

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

[英]how to filter data using controller angularjs?

我有3页第一页三星主页,其中包含带有移动服务的复选框列表。第二页诺基亚主页,其中包含具有诺基亚服务的复选框列表。三星和诺基亚商店的第三页列表。如果用户来自三星主页并选择了一些我需要显示的服务仅具有该特定服务的三星商店。如果用户来自nokiahome并选择了某些服务,我只需要显示具有该特定服务的诺基亚商店。我也具有搜索框,当我来自第一和第二页时,列表页应显示具有选定的services.i做了一些逻辑,但它的列表控制器不起作用,我需要检查选定的商店和选定的服务,有人可以帮助我

 .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" 
 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> 

我已附上我的代码,我想过滤S_Store和S_Services

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

$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]);                   
        }
    }
}

请注意,您在scope.check之前忘记了“ $”

暂无
暂无

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

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