我使用的是@pnp/spfx-controls-react库2.5.0版的ListView控件
有一个文档化的属性叫做defaultSelection
,它是:
默认选择项的索引
我试着这样使用它来自动选择表的第一行:
<ListView
items={this.state.items}
viewFields={this._viewFields}
selectionMode={SelectionMode.single}
selection={this._updateSelectedItems}
defaultSelection={[0]}
/>
但是当页面加载时,它从不选择任何东西。控制台也没有错误。
我也在这里使用了一个简单的例子:https://github.com/RaspeR87/sp-dev-fx-webparts/tree/master/spfx-react-controls/ListView在一个简单的项目上尝试它,但是它仍然没有选择任何东西。
有人设法利用这个属性吗?感谢您的每一个意见。
下面是解决这个问题的方法:
// Add a useRef to your ListView control
const refListView = useRef<any>(null);
// Set selection on load
useEffect(() => {
// Set mySelection as default selection
const mySelection = [1,4,6];
mySelection.forEach(i => {
setTimeout(() => {
refDocs.current._selection.setIndexSelected(i, true, false);
}, 0);
});
}, []);
// ListView control
<ListView ref={refListView }...
是的,我的测试结果和你的一样。您可以将此问题发布到PNP react控制Github库中。https://github.com/pnp/sp-dev-fx-controls-react/issues