angularjs - 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>
解决方案
您可以在渲染到 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>
这是工作示例: 演示
推荐阅读
- vba - 再问:通过 JavaScript API for Excel 获取工作簿的所有 VBA 宏
- javascript - AngularJS ng-repeat 使用过滤器在数组上重复
- c# - SQL查询格式化日期数据类型以排除时间
- python - 从另一个类的另一个@classmethod 调用@classmethod
- hdfs - 如何将hdfs权限还给超级组?
- rust - 什么优化让我的 rust 程序运行得这么快?
- javascript - 在查看时如何为 CSS 动画制作动画?
- wordpress - 警告:输入中的意外字符:'' (ASCII=127) state=0
- java - 尝试运行 derby 时在 Eclipse 上“找不到或加载主类 org.apache.derby.tools.ij”
- c# - Atlassian.Net SDK 获取所有项目和冲刺