我正在从表中选择列。我有20个元素的限制。我需要能够选择所有的项目,直到超过限制。
场景:18/20
- 下一列有10个元素
- 点击选择整列
- 应该只选择前两个元素
我试过了:
const myArray = selectedColumn.some(() => itemsOnArray.length <= 20)
? selectedLaborer.map((laborer) => laborer)
: [];
你能给你的问题提供更多的背景吗?
就我个人而言,当涉及到选择时,我通常会给我希望选择的对象一个id,如果他们没有一个(一个唯一标识符),并将这个值存储在一个数组中作为我选择的对象。
添加和删除值非常容易:
(这是为了更明确的阅读,但没有改变任何东西的打字稿)
let selectedItems: string[] = [];
const MAX_ALLOWER_SELECTED = 20;
const selectItem = (itemId: string): void => {
//You might want to check for duplicates before inserting
if (selectedItems.length > MAX_ALLOWED_SELECTED)
return;
selectedItems.push(itemId);
}
const unselectItem = (itemId: string): void => {
selectedItems = selectedItems.filter(id => itemId !== id);
}
const isItemSelected = (itemId: string): boolean => {
return selectedItems.includes(itemId);
}