我已添加selectedOption
为您的 TreeList 组件的道具。
App
零件:
import * as React from "react";
import * as ReactDOM from "react-dom";
import MyTreeList from "./TreeList";
import { DropDownList } from "@progress/kendo-react-dropdowns";
import { Label } from '@progress/kendo-react-labels';
class App extends React.Component {
options = ["Option 1", "Option 2", "Option 3"];
state = { selectedOption: '' };
onChange = (e) => {
this.setState({selectedOption: e.value});
}
render() {
return (
<div>
<Label>Your option: </Label>
<DropDownList data={this.options} onChange={(e) => this.onChange(e)}/>
<br />
<br />
<MyTreeList selectedOption={this.state.selectedOption}/>
</div>
);
}
}
ReactDOM.render(<App />, document.querySelector("my-app"));
使用componentDidUpdate
钩子,我检查了您的选择并根据需要更新了列表。
TreeList
零件:
import * as React from "react";
import {
TreeList,
mapTree,
extendDataItem,
TreeListSelectionCell,
TreeListHeaderSelectionCell
} from "@progress/kendo-react-treelist";
import employees from "./data";
const subItemsField = "employees";
const expandField = "expanded";
const selectField = "selected";
export default class MyTreeList extends React.Component {
state = {
data: employees.slice(),
expanded: [1, 2, 32],
selected: []
};
handleRowClick = e => {
const selected = e.dataItem.selected
? this.state.selected.filter(x => x !== e.dataItem.id)
: [...this.state.selected, e.dataItem.id];
this.setState({ selected });
};
onExpandChange = e => {
this.setState({
expanded: e.value
? this.state.expanded.filter(id => id !== e.dataItem.id)
: [...this.state.expanded, e.dataItem.id]
});
};
updateFields = dataArr => {
const { expanded, selected } = this.state;
return mapTree(dataArr, subItemsField, item =>
extendDataItem(item, subItemsField, {
expanded: expanded.includes(item.id),
selected: selected.includes(item.id)
})
);
};
onSelectionChange = event => {
this.changeSelection(event.dataItem);
};
changeSelection = dataItem => {
const selected = dataItem.selected
? this.state.selected.filter(x => x !== dataItem.id)
: [...this.state.selected, dataItem.id];
this.setState({ selected });
};
onRowClick = event => {
this.changeSelection(event.dataItem);
};
onHeaderSelectionChange = event => {
const checked = event.syntheticEvent.target.checked;
const selected = [];
if (checked) {
mapTree(this.state.data, subItemsField, item => {
selected.push(item.id);
return item;
});
}
this.setState({ selected });
};
headerSelectionValue = () => {
let allSelected = true;
const selected = this.state.selected;
mapTree(this.state.data, subItemsField, item => {
allSelected = allSelected && selected.includes(item.id);
return item;
});
return allSelected;
};
clearSelection() {
this.setState({ selected: [] });
}
componentDidUpdate(prevProps) {
if (prevProps.selectedOption !== this.props.selectedOption) {
var selectedOption = this.props.selectedOption;
switch (selectedOption) {
case "Option 1":
this.setState({ selected: [1, 2, 3] });
break;
case "Option 2":
this.clearSelection();
break;
case "Option 3":
break;
}
}
}
render() {
return (
<div>
<TreeList
style={{ maxHeight: "510px", overflow: "auto" }}
data={this.updateFields(this.state.data)}
selectedField={selectField}
onSelectionChange={this.onSelectionChange}
onHeaderSelectionChange={this.onHeaderSelectionChange}
onRowClick={this.onRowClick}
expandField={expandField}
subItemsField={subItemsField}
onExpandChange={this.onExpandChange}
columns={[
{
field: "selected",
width: "7%",
headerSelectionValue: this.headerSelectionValue(),
cell: TreeListSelectionCell,
headerCell: TreeListHeaderSelectionCell
},
{ field: "name", title: "Name", expandable: true, width: "31%" },
{ field: "position", title: "Position", width: "31%" },
{
field: "hireDate",
title: "Hire Date",
format: "{0:d}",
width: "31%"
}
]}
/>
</div>
);
}
}
在StackBlitz工作演示。