在 Fluent UI DetailsList 中获取所选项目

Getting selected items in Fluent UI DetailsList

我正在使用 Fluent UI DetailsList。在示例中,该组件被实现为 class 组件,但我使用的是功能组件。

我在获取所选项目时遇到困难,我假设并认为我的实施不正确。问题是我没有得到任何选定的项目。

export const JobDetails = () =>  {
    const { actions, dispatch, isLoaded, currentTabJobs, activeTabItemKey } = useJobDetailsState()

    let history = useHistory();

    useEffect(() => {
        if (actions && dispatch) {
            actions.getJobListDetails()
        }
    }, [actions, dispatch])

    const getSelectionDetails = (): string =>  {
        let selectionCount = selection.getSelectedCount();

        switch (selectionCount) {
            case 0:
                return 'No items selected';
            case 1:
                return '1 item selected: ' + (selection.getSelection()[0] as any).name;
            default:
                return `${selectionCount} items selected`;
        }
    }

    const [selectionDetails, setSelectionDetails] = useState({})
    const [selection, setSelection] = useState(new Selection({
        onSelectionChanged: () => setSelectionDetails(getSelectionDetails())
    }))

    useEffect(() => {
        setSelection(new Selection({
            onSelectionChanged: () => setSelectionDetails(getSelectionDetails())
        }))
    },[selectionDetails])

    return (
        <div>
            <MarqueeSelection selection={selection}>
                <DetailsList
                    items={currentTabJobs}
                    groups={getGroups()}
                    columns={_columns}
                    selection={selection}
                    selectionPreservedOnEmptyClick={true}
                    groupProps={{
                        onRenderHeader: props => {
                            return (
                                <GroupHeader 
                                    {...props} 
                                    selectedItems={selection}
                                />
                            )
                        },
                        showEmptyGroups: true
                    }}
                />
            </MarqueeSelection>
        </div>
    )
}

export default JobDetails;

我找到了我遇到的问题的解决方案,我不得不记住详细信息列表

我做了什么:

const [selectedItems, setSelectedItems] = useState<IObjectWithKey[]>();
const selection = useMemo(
    () =>
      new Selection({
          onSelectionChanged: () => {
          //console.log('handle selection change',selection.getSelection())
          setSelectedItems(selection.getSelection());
      },
    selectionMode: SelectionMode.multiple,
  }),
[]);

const detailsList = useMemo(
    () => (
        <MarqueeSelection selection={selection}>
            <DetailsList
                items={currentTabJobs}
                groups={getGroups()}
                columns={columns}
                ariaLabelForSelectAllCheckbox="Toggle selection for all items"
                ariaLabelForSelectionColumn="Toggle selection"
                checkButtonAriaLabel="Row checkbox"
                selection={selection}
                selectionPreservedOnEmptyClick={true}
                groupProps={{
                    onRenderHeader: (props) => {
                        return <GroupHeader {...props} selectedItems={selection} />;
                    },
                    showEmptyGroups: true,
                 }}
                 onRenderItemColumn={(item, index, column) =>
                     renderItemColumn(item, index!, column!)
                 }
             />
         </MarqueeSelection>
     ),
     [selection, columns, currentTabJobs, activeTabItemKey]
 );

 return (
     <div>
         {detailsList}
     </div>
 )

我可能有一个更简单的答案,这个例子是针对 'SelectionMode.single' 激活的列表,但我认为获取所选项目的原理保持不变

const [selectedItem, setSelectedItem] = useState<Object | undefined>(undefined)
const selection = new Selection({
    onSelectionChanged: () => {
        setSelectedItem(selection.getSelection()[0])
    }
})

useEffect(() => {
    // Do something with the selected item
    console.log(selectedItem)
}, [selectedItem])

<DetailsList
    columns={columns}
    items={items}
    selection={selection}
    selectionMode={SelectionMode.single}
    selectionPreservedOnEmptyClick={true}
    setKey="exampleList"
/>

选择对象置于状态。 示例:

...

export const Table: FunctionComponent<TableProps> = props => {
  const { items, columns } = props

  const { setCopyEnabled } = useCommandCopy()
  const { setDeleteEnabled } = useCommandDelete()

  const onSelectionChanged = () => {
    if (selection.getSelectedCount() === 0) {
      setCopyEnabled(false)
      setDeleteEnabled(false)
    }
    else if (selection.getSelectedCount() === 1) {
      setCopyEnabled(true)
      setDeleteEnabled(true)
    }
    else {
      setCopyEnabled(false)
      setDeleteEnabled(true)
    }
  }

...

  const [selection] = useState(new Selection({ onSelectionChanged: onSelectionChanged }))

  useEffect(() => {
    selection.setAllSelected(false)
  }, [selection])

...

  return (
    <ScrollablePane styles={{
      root: {
        position: 'fixed',
        top: 105, left: 285, right: 20, bottom: 20
      },
    }}>
      <DetailsList
        items={items}
        columns={columns}

        selection={selection}
        selectionMode={SelectionMode.multiple}

        layoutMode={DetailsListLayoutMode.justified}
        constrainMode={ConstrainMode.horizontalConstrained}

...

      />
    </ScrollablePane>
  )
}