需求:
使用RestAPI调用填充唯一的cateID,categoryName,但是我正在获取整个记录。
表格
代码说明:
存储库:
@Repository
public interface CategoryRepository extends JpaRepository<xCategory,Integer>
{
// @Query("SELECT DISTINCT a.catID,a.categoryName FROM ccCategory a order by categoryName asc")
@Query("SELECT DISTINCT a.catID, a.categoryName FROM xCategory a order by categoryName asc")
List<ccCategory> getCategoryName();
}
Rest Controller:
@RestController
@CrossOrigin(origins = "http://localhost:4200", maxAge = 3600)
public class HomeResource {
private final Logger log = LoggerFactory.getLogger(HomeResource.class);
@Autowired
CategoryRepository categoryRepository;
@GetMapping("/getAllCategory")
public List<ccCategory> getAllCategory() {
// public List<String> getAllCategory() {
System.out.println("***** Call : API getAllCategory() ******");
List<ccCategory> cCategory = categoryRepository.findAll();
return cCategory;
}
角码:
<label class="control-label">Category: </label>
<select [(ngModel)]="listAllCategory" name="xxcategory" class="form-control" required>
<option *ngFor="let xxcategory of listAllCategory" [value]="xxcategory.catID">
{{xxcategory.categoryName}}
</option>
</select>
问题:下拉列表填充所有表值,但我只希望UNIQUE值,如一次catID,categoryName。
您必须添加变量以将所选元素保留在select
中,然后用[(ngModel)]="listAllCategory"
更改[(ngModel)]="selectedCategory"
在List<Object>
中没有映射。将List<Object>
转换为List<cCategory>
。您可以这样解析List<Object>
并创建List<cCategory>
for (Object cdata:cCategory) {
Object[] obj= (Object[]) cdata;
String catID = (String)obj[0];
String categoryName = (String)obj[1];
}