简体   繁体   English

NGRX 实体 - 如何正确处理 isSelected 属性?

[英]NGRX Entity - how to handle isSelected property properly?

I have a table where each row might be clicked and then it should change a color and later on be used to filter out selected items.我有一个表格,可以单击每一行,然后它应该更改颜色,然后用于过滤掉选定的项目。 In each row I have an item:在每一行我都有一个项目:

interface Item{
   id:string;
   name:string;
   price:number;
}

I am wondering what is a better approach regarding EntityStateAdapter:我想知道关于 EntityStateAdapter 的更好方法是什么:

  1. add isSelected property to Item and later on updating this part of state?isSelected属性添加到Item以及稍后更新 state 的这一部分?

OR或者

  1. extend Adapter state and store selected items in array.扩展适配器 state 并将所选项目存储在数组中。
export const adapter: EntityAdapter<Item> = createEntityAdapter<Item>();

export const initialState: EntityState<Item> = adapter.getInitialState({
        selectedItems:Array<string>
})

IMO 1st approach is better. IMO 第一种方法更好。 It is easier to just add property to Item, because later on I can easily reflect the state in table for each row just by item.isSelected .将属性添加到 Item 更容易,因为稍后我可以通过 item.isSelected 轻松地在表中为每一行反映item.isSelected I would like to hear about your experience.我想听听你的经历。

I prefer to use selectedItems , reasons:我更喜欢使用selectedItems ,原因:

  • it's "meta data" that is added on top of an entity它是添加在实体之上的“元数据”
  • easier to update, eg if you select or unselect you can simply update the array instead of looking the entity up before you can update it更容易更新,例如,如果您是 select 或取消选择,您可以简单地更新数组,而不是在更新之前查找实体

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

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