gpt4 book ai didi

reactjs - 获取 Fluent UI DetailsList 中的选定项目

转载 作者:行者123 更新时间:2023-12-04 12:38:56 25 4
gpt4 key购买 nike

我正在使用 Fluent UI DetailsList .在示例中,组件是作为类组件实现的,但我使用的是功能组件。

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

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;

最佳答案

我可能有一个更简单的答案,此示例适用于激活了 '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"
/>

关于reactjs - 获取 Fluent UI DetailsList 中的选定项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61476978/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com