简体   繁体   English

ngRepeat按数组名称过滤?

[英]ngRepeat Filter by Array name?

I have the following JSON structure in my Angular app: 我的Angular应用中具有以下JSON结构:

JSON结构

Sample: http://pastie.org/pastes/9476207/text?key=u6mobe15chwyiz1jakn0w 范例: http//pastie.org/pastes/9476207/text?key = u6mobe15chwyiz1jakn0w

And the following HTML which is displaying every product in the parent array: 下面的HTML在父数组中显示每个产品:

<div class="bb-product-grid" ng-repeat="brand in notebooks">
    <ul ng-repeat="products in brand">
        <li ng-repeat="Notebook in products">
            <span class="product-title">{{Notebook.Model}}</span>
            <span class="product-description"><p>{{Notebook.Description}}</p></span>
            <span class="product-image">image</span>
            <span class="product-add-to-bundle"><button>Add to bundle</button></span>
            <span class="product-price">{{Notebook.Price}}<sub>pcm</sub></span>
        </li>
    </ul>
</div>

I want to be able to filter the products by the brand names (array names, Acer, Apple etc.). 我希望能够按品牌名称(阵列名称,Acer,Apple等)过滤产品。

By default, only the 'Apple' products will be visible. 默认情况下,只有“ Apple”产品可见。 You will then click a button to change the visible results to reflect what brand you've selected. 然后,您将单击一个按钮来更改可见结果,以反映您选择的品牌。 So if you then select HP, you'll only see 'HP' products. 因此,如果您选择HP,则只会看到“ HP”产品。

I'm a little stuck on this because the JSON structure is pretty nested, if it was 1/2 levels I'd be able to cope but I can't figure this out, I seem to only show all the products or break the app. 我对此有点坚持,因为JSON结构是非常嵌套的,如果它是1/2级,我将能够应付,但我无法弄清楚,我似乎只展示所有产品或破坏产品应用程式。 Any advice on the best-practise approach to this (I'm guessing I might have to create a custom filter?) will be greatly appreciated! 对此有任何最佳实践方法的建议(我想我可能必须创建一个自定义过滤器?),将不胜感激!

If you can get your data to be in this format: 如果您可以使数据采用以下格式:

$scope.data = [
  {
    brand: 'Acer',
    laptops: ['acer_1', 'acer_2', 'acer_3', 'acer_4', 'acer_5']
  },
  {
    brand: 'Apple',
    laptops: ['apple_1', 'apple_2', 'apple_3', 'apple_4', 'apple_5']
  },
  {
    brand: 'Asus',
    laptops: ['asus_1', 'asus_2', 'asus_3', 'asus_4', 'asus_5']
  },
  {
    brand: 'HP',
    laptops: ['hp_1', 'hp_2', 'hp_3', 'hp_4', 'hp_5']
  },
  {
    brand: 'Lenovo',
    laptops: ['lenovo_1', 'lenovo_2', 'lenovo_3', 'lenovo_4', 'lenovo_5']
  },
  {
    brand: 'Toshiba',
    laptops: ['toshiba_1', 'toshiba_2', 'toshiba_3', 'toshiba_4', 'toshiba_5']
  }
];

Then you can use have a filter like so: 然后,您可以使用类似的过滤器:

$scope.search = {
  brand: 'HP'
};

With HTML: 使用HTML:

<select ng-model="search.brand">
  <option ng-repeat="company in data">{{company.brand}}</option>
</select>
<ul ng-repeat="company in data | filter:search">
  <li><b ng-bind="company.brand"></b></li>
  <ul ng-repeat="laptop in company.laptops">
    <li ng-bind="laptop"></li>
  </ul>
</ul>

Here is a jsfiddle . 这是一个jsfiddle

brands is an array of objects with the brand names as keys. brands是一个以品牌名称为键的对象数组。 It doesn't make sense to use filters to solve this problem ... however it's easy to get the products if you have the key name. 使用过滤器解决此问题没有任何意义……但是,如果您有密钥名称,则很容易获得产品。 Imagine you have searchName = 'Apple' and obj = [{Apple: []}, {Acer: []}] . 假设您有searchName = 'Apple'obj = [{Apple: []}, {Acer: []}] You could just use obj[0].Apple . 您可以只使用obj[0].Apple

this.filterItem = "Apple";
this.productsToShow = productObject[0][this.filterItem];

<select ng-model=ctrl.filterItem><!-- brand names go here --></select>
<ul ng-repeat="product in ctrl.productsToShow">

Don't use filter, when only ever one is supposed to be visisble. 如果只有一个是可见的,请不要使用过滤器。 Instead, skip the iteration over the brands, and instead just iterate over the selected brand . 取而代之的是,跳过对品牌的迭代,而仅对选定的品牌进行迭代。 All you need to do is then make sure you set selectedBrand to the correct subset of your data structure. 然后,您要做的就是确保将selectedBrand设置为数据结构的正确子集。

Fiddle: http://jsfiddle.net/5wwboysu/2/ 小提琴: http : //jsfiddle.net/5wwboysu/2/

Show brand:
<span ng-repeat="(name,brand) in notebooks.Notebooks[0]" ng-click="selected(brand)">
   {{name}} -  <br>
</span>

<ul ng-repeat="products in selectedBrand">
    <!-- snip -->
</ul>

$scope.selected = function(brand) {
      $scope.selectedBrand = brand;  
};

However, if you have control over the JSON, I'd try to return it in a format better suited for what you're trying to do. 但是,如果您可以控制JSON,我将尝试以更适合您尝试执行的格式返回它。

Add this filter which will take a brand: 添加此过滤器将采用一个品牌:

 <ul ng-repeat="brand in notebooks| filter:filterBrand">

in controller 在控制器中

$scope.SelectedBrandName= "Apple";

$scope.filterBrand = function(brand){
if(brand.Name == $scope.SelectedBrandName)
    return true;

 return false;
};

This will look at each brand and only allow brands that return true. 这将查看每个品牌,仅允许返回true的品牌。 If you have a button that changes the $scope.SelectedBrandName value to a different name, it will automatically filter through the ng-repeat to only brands that match. 如果您有一个将$ scope.SelectedBrandName值更改为其他名称的按钮,它将自动通过ng-repeat筛选仅匹配的品牌。

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

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