首页 > 解决方案 > Angular8:如何过滤唯一记录

问题描述

根据查询获取记录某些值保留重复记录但我无法在 dropdowm 中显示唯一值。

寻找一种仅显示唯一记录的方法。

**

**

@Repository
public interface CategoryRepository extends JpaRepository<ccCategory,Integer>
{

    public static final String FIND_CATEGORYNAME = "SELECT DISTINCT * from xxCategory where active='1'";

    @Query(value = FIND_CATEGORYNAME, nativeQuery = true)

    List<xxCategory > getCategoryName();
}

**-

**

@GetMapping("/getAllCategory")
        public List<xxCategory> getAllCategory() {
        List<ccCategory> cCategory = categoryRepository.getCategoryName();
        return cCategory;
    }

- 角边:

<label class="control-label">Category: </label>
                <select  [(ngModel)]="listAllCategory" name="enquirycategory" class="form-control" required>


                    <option *ngFor="let enquirycategory of listAllCategory" [value]="enquirycategory.ID">
                      {{enquirycategory.categoryName}}
                  </option>

标签: angularjsangularjs-directiveangular-material

解决方案


您可以在渲染到 DOM 之前过滤结果。

这是您可以过滤的方法:

this.uniqueCatogaries = this.categories.filter((item, index, self) =>
  index === self.findIndex((t) => (
    t.id === item.id
  ))

然后在component.html *ngFor数组uniqueCatogaries中:

<select  [(ngModel)]="listAllCategory" name="enquirycategory"     class="form-control" required>
<option *ngFor="let enquirycategory of uniqueCatogaries" [value]="enquirycategory.ID">
{{enquirycategory.name}}
</option>
</select>

这是工作示例: 演示


推荐阅读