简体   繁体   English

如何在 Fluent UI DetailsList 组件上获取选中的项目

[英]How to get selected items on Fluent UI DetailsList component

I want to use DetailsList component https://developer.microsoft.com/en-us/fluentui#/controls/web/detailslist and select items.我想使用 DetailsList 组件https://developer.microsoft.com/en-us/fluentui#/controls/web/detailslist和 select 项目。 As I understand, the component doesn't provide us a prop to get selected items.据我了解,该组件没有为我们提供获取所选项目的道具。 I only see onActiveItemChanged prop which is only giving me single item but not all selected items.我只看到onActiveItemChanged道具,它只给我一个项目,而不是所有选定的项目。

  • I checked the question below, but it seems the answer doesn't work我检查了下面的问题,但答案似乎不起作用

Getting selected items in Fluent UI DetailsList 在 Fluent UI DetailsList 中获取选定的项目

  • I wanted to create a state with an empty array and push or filter selected items into it, but there is also another problem.我想创建一个带有空数组的 state 并将选定的项目推送或过滤到其中,但还有另一个问题。 Scenario:设想:
  1. Click checkbox on header to select all items单击 header 到 select 所有项目上的复选框
  2. If I click an item's checkbox, then it becomes unselected and other items still selected如果我单击一个项目的复选框,则它变为未选中状态,而其他项目仍处于选中状态
  3. If I click an item's any field instead of checkbox, then other items become unselected, and the one I clicked become selected.如果我单击一个项目的任何字段而不是复选框,那么其他项目将变为未选中状态,而我单击的项目将变为选中状态。

Because of this reason -maybe I can also have other problem in the future-, I couldn't use state, as well.由于这个原因——也许我以后还会有其他问题——我也不能使用 state。

I will be very glad if I can get current selected checkboxes on DetailsList component.如果我能在 DetailsList 组件上获得当前选中的复选框,我将非常高兴。

I found the answer.我找到了答案。

  • import Selection from @fluentui/react@fluentui/react导入Selection

  • Add selection={selection} to DetailsList propsselection={selection}添加到 DetailsList 道具

  • and then do this before returning the component:然后在返回组件之前执行此操作:

    const selection = new Selection({ onSelectionChanged: () => { const selectedItems = selection.getSelection(); // Do something with the selected items' console.log(selectedItems, 'ewgergre'); }, }); const selection = new Selection({ onSelectionChanged: () => { const selectedItems = selection.getSelection(); // 对选中的项目做一些事情 console.log(selectedItems, 'ewgergre'); }, });

I didn't understand why it doesn't format my code.我不明白为什么它不格式化我的代码。

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

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