如何使用AngularJS中的select(下拉)筛选数据

How to filter data using select (dropdown) in AngularJS?

本文关键字:下拉 筛选 数据 select 何使用 AngularJS 中的      更新时间:2023-09-26

我有一个对象,其中包含一些节日优惠。我在下面给出了我的代码。我是这项技术的新手。在这里,我想使用下拉列表(select)过滤ng-repeat值。

示例:在选择中,如果我选择所有,它应该显示所有优惠券(可用)。这意味着,如果我选择了所有应该显示的内容,可以获得50%的午餐、晚餐大马士革、晚餐节。

示例2:在选择中,如果我选择获得50%的午餐,它应该只在这里获得50%的午饭。它应该显示两个数据:一个是storeid:"886745",storeid:"777755"。

根据选择,它应该显示我的代码,任何人都可以帮我。

angular.module('myApp', [])
    .controller("myCntrl", function ($scope) {
    
    $scope.isCouponActivated = function(storeId){
    var isCouponActivated = false;
        angular.forEach($scope.activatedcoupons, function(coupon){
        if(coupon.storeid == storeId)
        {
            isCouponActivated = true;
        }
        });
        return isCouponActivated;
    }
    $scope.coupons = [{
        id: "1",
        storeid: "986745",
        couponname: "healthy breakfast offer",
        offermessage: "50% offer for break fast",
        noofcoupon: "10"
    }, {
         id: "2",
        storeid: "886745",
        couponname: "get 50% lunch",
        offermessage: "50% offer for Lunch",
        noofcoupon: "10"
    }, {
         id: "3",
        storeid: "690745",
        couponname: "dinner damaka",
        offermessage: "50% offer for dinner",
        noofcoupon: "10"
    },
    {
         id: "4",
         storeid: "550745",
        couponname: "supper festiwal",
        offermessage: "80% offer for supper",
        noofcoupon: "10"
    },
    
    {
         id: "5",
         storeid: "690733",
        couponname: "eve damaka snaks",
        offermessage: "20% offer for snaks",
        noofcoupon: "10",
    },
    {
         id: "6",
        storeid: "777755",
        couponname: "get 50% lunch",
        offermessage: "50% offer for Lunch",
        noofcoupon: "50"
    }
    ]
    
     $scope.activatedcoupons = [{
       id: "1",
        storeid: "986745",
        couponname: "healthy breakfast offer",
        offermessage: "50% offer for break fast",
        noofcoupon: "10",
    }, {
         id: "2",
         storeid: "690733",
        couponname: "eve damaka snaks",
        offermessage: "20% offer for snaks",
        noofcoupon: "10"
    }
    ]
    
 $scope.activate = function(id) {
 console.log(id);
 }
    
})
<div ng-app="myApp">
    <div ng-controller="myCntrl">
        <label>List Of Coupons</label>
        <br>
        <label for="singleSelect"> select: </label><br>
    <select name="singleSelect" ng-model="data.singleSelect">
	<option value="0">all</option>
      <option value="healthy breakfast offer">healthy breakfast offer</option>
      <option value="get 50% lunch">get 50% lunch</option>
	  <option value="dinner damaka">dinner damaka</option>
	  <option value="supper festiwal">supper festiwal</option>
	  <option value="eve damaka snaks">eve damaka snaks</option>
    </select><br>
		<span ng-bind="searchsubject"></span>
        <br>
        <br>
        <div ng-repeat="coupon in coupons | filter:search" data-ng-hide="isCouponActivated(coupon.storeid)" style="border-radius:5px;background: #8AC007;padding: 20px;">
            <br>{{coupon.couponname}}
            <br>
            <br>{{coupon.offermessage}}
            
         <a class="tab-item" ng-click="activate(coupon.id)" id="appcolor">
      <i class="icon ion-checkmark-circled" ></i> 
      Activate
    </a>
        </div>
    </div>
    <BR>
    <BR>
</div>
  

我的小提琴http://jsfiddle.net/qwdvdv55/2/

我将您的选择模型更改为singleSelect,并将ng重复过滤器更新为基于singleSelect的过滤器。

<div ng-app="myApp">
<div ng-controller="myCntrl">
    <label>List Of Coupons</label>
    <br>
    <label for="singleSelect"> select: </label><br>
<select name="singleSelect" ng-model="singleSelect">
    <option value="">all</option>
    <option value="healthy breakfast offer">healthy breakfast offer</option>
    <option value="get 50% lunch">get 50% lunch</option>
    <option value="dinner damaka">dinner damaka</option>
    <option value="supper festiwal">supper festiwal</option>
    <option value="eve damaka snaks">eve damaka snaks</option>
</select>
        <br>
    <span ng-bind="searchsubject"></span>
    <br>
    <br>
    <div ng-repeat="coupon in coupons | filter:singleSelect" data-ng-hide="isCouponActivated(coupon.storeid)" style="border-radius:5px;background: #8AC007;padding: 20px;">
        <br>{{coupon.couponname}}
        <br>
        <br>{{coupon.offermessage}}
     <a class="tab-item" ng-click="activate(coupon.id)" id="appcolor">
  <i class="icon ion-checkmark-circled" ></i> 
  Activate
</a>
    </div>
</div>
<BR>
<BR>

更改过滤器:

<div ng-repeat="coupon in coupons | 
     filter:data.singleSelect" data-ng-hide="isCouponActivated(coupon.storeid)" 
     style="border-radius:5px;background: #8AC007;padding: 20px;">
</div>

jsfiddle

另一个不更改代码的选项是实现过滤器中引用的search()方法。实现搜索方法使您能够自定义搜索。你可以在这个jsfiddle中查看它。

搜索方法将在每张优惠券上调用。返回true以在筛选器中显示它,或返回false以不显示它。

$scope.search = function (coupon, index, coupons) {
    //if the search combobox value isn't defined, or if it's "all"
    if (!$scope.data || !$scope.data.singleSelect || $scope.data.singleSelect == "0")
        return true;
    if (coupon.couponname == $scope.data.singleSelect) {
        return true;
    } else {
        return false;
    }
}