简体   繁体   English

AngularJS-如何处理多个动态下拉列表以筛选列表?

[英]AngularJS - How should I handle multiple dynamic dropdowns to filter a list?

I'm very new to AngularJS (and somewhat new to Javascript in general) so I'm not sure if I'm even looking at this issue the right way. 我是AngularJS的新手(一般来说对Javascript还是有些新手),所以我不确定我是否正在以正确的方式看待这个问题。

I'm getting data from Amazon's Product Advertising API. 我正在从Amazon的Product Advertising API获取数据。 I'm trying to account for different variations of an item and allow the user to select their preferred variation with dropdowns for the "variation dimensions" which are basically just ways in which the variations differ (size, color, etc) 我正在尝试说明商品的不同变体,并允许用户通过“变体尺寸”下拉菜单选择自己喜欢的变体,这基本上就是变体不同(大小,颜色等)的方式

I don't know in advance what these dimensions will be, so in my JSON data that I get from the server I have an array of all possible items (an array of all valid combinations of these dimensions), an array of dimension names and an array of dimension values. 我事先不知道这些尺寸是什么,因此在从服务器获取的JSON数据中,我有一个所有可能项目的数组(这些尺寸的所有有效组合的数组),一个尺寸名称和尺寸值数组。 Example: 例:

possible_items = [{'size': 5, 'color': 'red'}, {'size': 5, 'color': 'blue'}];
variation_dimensions = ['size', 'color'];
variation_values['size'] = [5];
variation_values['color'] = ['red', 'blue'];

Currently I'm doing this: 目前,我正在这样做:

<div class="detailsSelect" ng-repeat="dim in variation_dimensions">
  <select class="form-control variation-dropdown">
    <option ng-repeat="value in item.variation_values[dim]">{{ value }}</option>
  </select>
</div>

This creates the correct number of selects with the correct values. 这将创建具有正确值的正确选择数。

How can I use Angular to keep track of what selects were created and use their values to filter the list of possible items? 如何使用Angular跟踪创建了哪些选择并使用它们的值来过滤可能的项目列表?

First of all, in selects you can use ng-options instead of ng-repeat : 首先,在selects中可以使用ng-options代替ng-repeat

<select class="form-control variation-dropdown" 
        ng-options="value for value in item.variation_values[dim]"></select>

Then you need to add ng-model to store what's been selected, and ng-change to apply the selection to your filters: 然后,您需要添加ng-model来存储选择的内容,并添加ng-change来将选择内容应用于过滤器:

<select class="form-control variation-dropdown" 
        ng-options="value for value in item.variation_values[dim]" 
        ng-model="selection[dim]" 
        ng-change="updateFilters()"></select>

In your controller: 在您的控制器中:

$scope.selection = {};  // At the top, just to initialise $scope.selection as a key/value store
$scope.filteredItems= possible_items; // Initially just display everything

$scope.updateFilters = function() {
  for (var dim in $scope.selection) {
    $scope.filteredItems= $scope.filteredItems.filter(function(item) {
      return item[dim]==$scope.selection[dim];
    });
  }
}

Then you just need to add another section in your html to display the contents of filteredItems . 然后你只需要添加另一部分在HTML中显示的内容filteredItems

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

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