首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在下拉列表中对选定项进行筛选

在下拉列表中对选定项进行筛选
EN

Stack Overflow用户
提问于 2017-02-10 13:42:55
回答 1查看 944关注 0票数 1

我正在尝试使我的{client.tenant}成为以后过滤掉我的表的源。该表只应根据我的下拉列表中的选择来查看customer 1或customer 2。我感觉我的下拉列表中的输入没有存储在任何地方。你有什么建议吗我做错了什么?下面是我的代码中的一个示例。我测试了很多代码,非常抱歉。)

代码语言:javascript
复制
    <body>
    <div class="container" ng-controller="menuController">
        <select>
  <option ng-model="dropdownString" ng-repeat="client in products | unique:'tenant'">{{client.tenant}}</option>
</select>
        <div class="tab-content">
            <ul class="media-list tab-pane fade in active">
                <div class="row row-content">
                    <div class="col-xs-12">
                        <ul class="media-list">
                            <li class="media">
                                <div class="media-left media-middle">
                                </div>
                                <div class="media-body">
                                    <table>
                                        <tr>
                                            <th class="table-1">Product description</th>
                                            <th>SKU</th>
                                            <th>Tenant</th>
                                            <th>Select</th>
                                        </tr>
                                        <tr ng-repeat="product in products | searchFor:searchString">
                                            <td>{{product.description}}</td>
                                            <td>{{product.sku}}</td>
                                            <td>{{product.tenant}}</td>
                                            <td><input type="checkbox"></td>
                                        </tr>
                                    </table>
                                </div>
                            </li>
                        </ul>
            </ul>
            </div>
            </div>
        </div>
    </div>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script>
    <script>
        var app = angular.module('productBackend', []);
        app.controller('menuController', function ($scope, $http) {
            $scope.search=[];
            $http.get('./scripts/products.json')
                .then(function (res) {
                    $scope.products = res.data;
                });
        });

        app.filter('searchFor', function () {
            return function (arr, searchString) {
                if (!searchString) {
                    return arr;
                }
                var result = [];
                searchString = searchString.toLowerCase();
                angular.forEach(arr, function (item) {
                    if (item.tenant.toLowerCase().indexOf(searchString) !== -1) {
                        result.push(item);
                    }
                });
                return result;
            };
        });

app.filter('unique', function() {
   return function(collection, keyname) {
      var output = [], 
          keys = [];
      angular.forEach(collection, function(item) {
          var key = item[keyname];
          if(keys.indexOf(key) === -1) {
              keys.push(key); 
              output.push(item);
          }
      });
      return output;
   };
});

app.controller('ExampleController', ['$scope', function($scope) {
   $scope.data = {
    singleSelect: null,
    multipleSelect: [],
    option1: 'option-1'
   };

   $scope.forceUnknownOption = function() {
     $scope.data.singleSelect = 'nonsense';
   };
}]);
    </script>
</body>

Json档案:

代码语言:javascript
复制
[{
    "sku": "S-MLX-SEC-002",
    "description": "Intrusion Prevention",
    "tenant": "Customer 1"
}, {
    "sku": "S-MLX-MCA-007",
    "description": "Microsoft Lync Enterprise - Private Cloud",
    "tenant": "Customer 1"
},{
    "sku": "S-MLX-SEC-002",
    "description": "Intrusion Prevention",
    "tenant": "Customer 1"
}, {
    "sku": "S-MLX-SEC-004",
    "description": "Local privacy compliance",
    "tenant": "Customer 1"
}, {
    "sku": "S-MLX-S02-100",
    "description": "Location based Access",
    "tenant": "Customer 1"
},{
    "sku": "S-MLX-SEC-002",
    "description": "Intrusion Prevention",
    "tenant": "Customer 1"
}, {
    "sku": "S-MLX-S02-510",
    "description": "Two factor authentication - Hard Token Security",
    "tenant": "Customer 1"
}, {
    "sku": "S-MLX-MCA-007",
    "description": "Microsoft Lync Enterprise - Private Cloud",
    "tenant": "Customer 1"
},{
    "sku": "S-MLX-CHG-001",
    "description": "Changes level Business - for Global Desktop Bundel",
    "tenant": "Customer 1"
}, {
    "sku": "S-MLX-GLD-003",
    "description": "Global Desktop Business Bundel - Performance - Private Cloud",
    "tenant": "Customer 1"
}, {
    "sku": "S-MLX-GLD-012",
    "description": "Global Desktop Foundation - Standard - Private Cloud",
    "tenant": "Customer 1"
},{
    "sku": "S-MLX-OFF-001",
    "description": "Microsoft Office Standard - Private Cloud",
    "tenant": "Customer 1"
}, {
    "sku": "S-MLX-LOB-824",
    "description": "Exact Financials (CL) - Private Cloud",
    "tenant": "Customer 1"
}, {
    "sku": "S-MLX-MCA-007",
    "description": "Microsoft Lync Enterprise - Private Cloud",
    "tenant": "Customer 2"
},{
    "sku": "S-MLX-SEC-002",
    "description": "Intrusion Prevention",
    "tenant": "Customer 2"
}, {
    "sku": "S-MLX-SEC-004",
    "description": "Local privacy compliance",
    "tenant": "Customer 2"
}, {
    "sku": "S-MLX-S02-100",
    "description": "Location based Access",
    "tenant": "Customer 2"
},{
    "sku": "S-MLX-SEC-002",
    "description": "Intrusion Prevention",
    "tenant": "Customer 2"
}, {
    "sku": "S-MLX-S02-510",
    "description": "Two factor authentication - Hard Token Security",
    "tenant": "Customer 2"
}, {
    "sku": "S-MLX-MCA-007",
    "description": "Microsoft Lync Enterprise - Private Cloud",
    "tenant": "Customer 2"
},{
    "sku": "S-MLX-CHG-001",
    "description": "Changes level Business - for Global Desktop Bundel",
    "tenant": "Customer 2"
}, {
    "sku": "S-MLX-GLD-003",
    "description": "Global Desktop Business Bundel - Performance - Private Cloud",
    "tenant": "Customer 2"
}, {
    "sku": "S-MLX-GLD-012",
    "description": "Global Desktop Foundation - Standard - Private Cloud",
    "tenant": "Customer 2"
},{
    "sku": "S-MLX-OFF-001",
    "description": "Microsoft Office Standard - Private Cloud",
    "tenant": "Customer 2"
}, {
    "sku": "S-MLX-LOB-824",
    "description": "Exact Financials (CL) - Private Cloud",
    "tenant": "Customer 2"
}]

Plunkr https://embed.plnkr.co/hfB75e6w9sYZzDkwyh24/

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-10 15:19:30

您只需更改以下代码

代码语言:javascript
复制
 <select>
   <option ng-model="dropdownString" ng-repeat="client in products | unique:'tenant'">{{client.tenant}}</option>
  </select>

代码语言:javascript
复制
 <select ng-model="dropdownString" >
     <option ng-repeat="client in products | unique:'tenant'">{{client.tenant}}</option>
  </select>

我不确定它会不会起作用。我就是这样解决你的问题的。我的代码如下:

代码语言:javascript
复制
<input type="text" class="search" ng-model="searchx.sku" placeholder="Enter your search terms" />


<select ng-model="searchx.tenant">
    <option ng-repeat="client in products | unique:'tenant'">{{client.tenant}}</option>
</select>

<div class="tab-content">
    <ul class="media-list tab-pane fade in active">
        <div class="row row-content">
            <div class="col-xs-12">
               <ul class="media-list">
                        <li class="media">
                            <div class="media-left media-middle">
                            </div>
                            <div class="media-body">
                                <table>
                                    <tr>
                                        <th class="table-1">Product description</th>
                                        <th>SKU</th>
                                        <th>Tenant</th>
                                        <th>Select</th>
                                    </tr>
                                    <tr ng-repeat="product in products | filter: searchx">
                                        <td>{{product.description}}</td>
                                        <td>{{product.sku}}</td>
                                        <td>{{product.tenant}}</td>
                                        <td><input type="checkbox"></td>
                                    </tr>
                                </table>
                            </div>
                        </li>
                    </ul>
        </ul>
        </div>

Angular.js

代码语言:javascript
复制
 var app = angular.module('productBackend', []);

 app.controller('menuController', function ($scope, $http) {

       $scope.searchx = {};   

        $scope.search=[];
        $http.get('./products.json')
            .then(function (res) {
                $scope.products = res.data;
            });  
 });


 app.filter('unique', function() {
        // same code
 });

 app.controller('ExampleController', ['$scope', function($scope) {
     // same code
 }]);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42160843

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档