【问题标题】:A specific sort of Objects according to a parameter within *ngFor根据 *ngFor 中的参数的特定类型的对象
【发布时间】:2017-04-30 22:09:19
【问题描述】:

我的问题是由于缺乏使用 angular2 和 HTML5 的经验造成的,所以请不要怪我 ;) 我有一些这样的食物类别:

这是他们的 HTML:

<div
  *ngFor="let item of items; let i = index"
  class="col-lg-4 col-md-6 lc-reset-padding-v"
>
  <md-card>
    <md-card-title-group>
      <md-card-title layout="row" layout-align="end center">
        <div class="md-card-title-media inline">
          <img
            alt="{{item.picture.alt}}"
            title="{{item.picture.title}}"
            src="{{page_items_url + item.picture.public_id}}"
          >
        </div>
        <div class="md-card-title-text inline">
          <span class="md-headline inline">{{item.name}}</span>
          <span *ngIf="item?.internal_reference">
          <p class="md-headline-reference">
           {{'ref' | translate}}: {{item.internal_reference | truncate : 22}}
          </p>
          </span>
          <span *ngIf="item?.description">
          <p class="md-subhead">{{item.description | truncate : 100}}</p>
          </span>
          <span
            *ngIf="page?.advanced"
            class="label label-pill item-type text-capitalize">
            {{item.item_type || 'no type for this '}} item
          </span>
        </div>
        <div *ngIf="item.out_of_stock" class="ribbon">
          <span>{{'notAvailable' | translate}}</span>
        </div>
        <span class="price">
          {{item.price | currency: item.official_currency:true:'1.2-3'}}
        </span>
      </md-card-title>
    </md-card-title-group>
  </md-card>
</div>

这些食物类型中的每一种都属于category(在每个食物对象中显示为对象参数),所以我希望食物的视图将按如下类别排序:

我在*ngFor 循环中添加了这些行:

<h3 class="text-muted text-uppercase title-container">
                  {{item.category.name}}
        </h3>

但是,从逻辑上讲,它看起来像这样:

请提供任何帮助或指导或想法?

【问题讨论】:

  • 你有什么问题/问题。
  • So I want that the View of foods will be ordered by categories like this one: .. 第二张图片..
  • 这与 Angular 和 HTML 无关,它是纯算法。

标签: html algorithm loops angular ngfor


【解决方案1】:

就像@Sakuto 在 cmets 中所说,这与 HTML 或 Angular 无关,这是您的逻辑问题。

您正试图通过使用*ngFor 遍历所有项目来创建一个列表。

您想要实现的是按类别创建不同的列表。

您有 2 个解决方案,或者您将数据更改为类别驱动而不是项目驱动,或者您按类别对项目进行分组。

因此,如果您更改数据,您的对象将如下所示:

interface FoodList {
  categories: Category[];
}
interface Category {
  name: string;
  items: FoodItem[];
}
interface FoodItem {
  //your item
}

然后在列表中执行如下操作:

<div *ngFor="let category of yourCategories">
   <h5>{{category.name}}</h5>
   <div *ngFor="let item of category.items">
      {{item.name}}
      <!-- your item template --> 
   </div>
</div>

如果您无法更改数据模型,只需在“打印”之前按类别对项目进行分组。

【讨论】:

    【解决方案2】:

    您的问题与 Angular 无关,但与算法问题直接相关,您想要的可以通过两种不同的方式完成:

    1. 根据类别名称检索订购的产品,然后检查之前的类别名称是否与当前的不同,如果是,则打印类别名称,否则只打印产品。这是伪代码的解释。

      var currentCategoryName = "";
      FOR product in products
          IF product.categoryName != currentCategoryName
              WRITE product.categoryName
          ENDIF
      
          WRITE product
      ENDFOR
      
    2. 第二种方式,如果您不能订购或不想订购,您可以创建一个新集合,其中每个类别都有其元素。

      var yourList = [];
      
      FOR product in products
          yourList[product.categoryName][] = product
      ENDFOR
      
      // Display
      FOR category in yourList
          WRITE category[0].categoryName
      
          FOR product in category
              WRITE product
          ENDFOR
      ENDFOR
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-31
      • 1970-01-01
      • 1970-01-01
      • 2015-10-31
      • 2019-07-21
      • 2021-10-19
      相关资源
      最近更新 更多