[英]Is there a way to limit the number of selectable items in Selection component of Office UI Fabric React's Details list?
Selection Component of fluent UI has a property to let the user select either single or multiple items.流畅 UI 的选择组件具有让用户 select 单个或多个项目的属性。 However, I don't see a straight-forward way to limit the user to select certain maximum number of items.
但是,我看不到将用户限制为 select 某些最大项目数的直接方法。 Is there a way get this done?
有没有办法做到这一点?
I have tried to work around it by using the onSelectionChanged event of Selection component.我试图通过使用 Selection 组件的 onSelectionChanged 事件来解决它。 But solution and the behavior doesn't seem perfect.
但解决方案和行为似乎并不完美。
Codepen Link: https://codepen.io/sakamati/pen/poRryqa Codepen 链接: https://codepen.io/sakamati/pen/poRryqa
const { useRef } = React;
const { DetailsList, DetailsListLayoutMode, Selection, SelectionMode, IColumn, MarqueeSelection } = window.FluentUIReact;
interface IDetailsListDocumentsExampleState {
columns: IColumn[];
items: IDocument[];
selectionDetails: string;
isModalSelection: boolean;
isCompactMode: boolean;
announcedMessage?: string;
}
interface IDocument {
key: number;
value: string;
}
class DetailsListDocumentsExample extends React.Component<{}, IDetailsListDocumentsExampleState> {
private _selection: Selection;
private _allItems: IDocument[];
private readonly _maxSelectable = 10;
public constructor(props: {}) {
super(props);
let a = [];
for(let i=0;i<50; i++){
a.push({key:i, value:'Abc '+i});
}
this._allItems = a;
const columns: IColumn[] = [
{
key: 'column1',
name: 'Key',
fieldName: 'key',
minWidth: 160,
maxWidth: 160,
isRowHeader: true,
isResizable: true,
data: 'number',
},
{
key: 'column2',
name: 'value',
fieldName: 'value',
minWidth: 210,
maxWidth: 350,
isRowHeader: true,
isResizable: true,
data: 'string',
}
];
this.state = {
items: this._allItems,
columns: columns,
selectionDetails: this._getSelectionDetails(),
isModalSelection: false,
isCompactMode: false,
announcedMessage: undefined,
};
this._selection = new Selection({
onSelectionChanged: () => {
console.log("onSelectionChanged Invoked!");
const c = this._selection.getSelectedCount();
//alert(this._selection.getSelectedCount());
if(c>this._maxSelectable){
const indices = this._selection.getSelectedIndices();
this._selection.toggleRangeSelected(indices[this._maxSelectable],indices.length-this._maxSelectable);
}
console.log(c);
},
});
}
public render() {
const { columns, isCompactMode, items, selectionDetails, isModalSelection, announcedMessage } = this.state;
return (
<MarqueeSelection selection={this._selection}>
<DetailsList
items={items}
columns={columns}
selectionMode={SelectionMode.multiple}
layoutMode={DetailsListLayoutMode.justified}
isHeaderVisible={true}
selection={this._selection}
selectionPreservedOnEmptyClick={true}
onItemInvoked={this._onItemInvoked}
/>
</MarqueeSelection>
);
}
private _onItemInvoked(item: any): void {
alert('Item invoked: ' + item.value);
}
private _getSelectionDetails(): string {
/*const selectionCount = this._selection.getSelectedCount();
switch (selectionCount) {
case 0:
return 'No items selected';
case 1:
return '1 item selected: ' + (this._selection.getSelection()[0] as IDocument).key;
default:
return `${selectionCount} items selected`;
}*/
return "Hello";
}
}
ReactDOM.render(<DetailsListDocumentsExample />,
document.getElementById("root"))
You can clear selection when you have certain number of items:当您有一定数量的项目时,您可以清除选择:
const clearSelection = () => {
currentSelection.setAllSelected(false);
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.