【发布时间】:2018-03-19 15:40:52
【问题描述】:
我正在使用 Kendo UI 库中的 TreeView 组件,我想知道如何更新树中的数据。可能吗?
这是我的代码:
class TreeViewTest extends React.Component {
dataSource = [
{
id: 123,
text: "aaaa",
expanded: true,
spriteCssClass: "rootfolder",
items: [
{
id: 2,
text: "aaa1",
expanded: true,
spriteCssClass: "folder",
items: [
{
id: 3,
text: "aaa2",
spriteCssClass: "html"
},
{
id: 4,
text: "aaa3",
spriteCssClass: "html"
},
{
id: 5,
text: "aaa4",
spriteCssClass: "image"
}
]
}
]
}
];
async componentDidUpdate(prevProps) {
if (this.props.endpoint !== prevProps.endpoint) {
init(this.props, this);
}
}
render() {
return (
<div>
<TreeView dataSource={this.dataSource} />
</div>
);
}
}
const init = async (props, _this) => {
const { endpoint, selectContent, setModal } = props;
const actions = props;
const response = await fetch(`${endpoint}/my/api/here`);
const body = await response.json();
/* some work on body here....*/
const data = [
{
id: 345,
text: "bbbb",
expanded: true,
spriteCssClass: "rootfolder",
items: [
{
id: 2,
text: "bbb1",
expanded: true,
spriteCssClass: "folder",
items: [
{
id: 3,
text: "bbb2",
spriteCssClass: "html"
},
{
id: 4,
text: "bbb3",
spriteCssClass: "html"
},
{
id: 5,
text: "bbb4",
spriteCssClass: "image"
}
]
}
]
}
];
_this.dataSource.push(data);
};
如何在远程调用后使用新数据更新数据源?
在安装 react 包之前,我添加了一个带有 Kendo UI jquery 版本的工作解决方案,它适用于所有更新。
使用 jquery 版本,我必须设置选项,并且我有函数 setDataSource(data) 并且它工作得很好,现在我必须做什么?
编辑 我使用 componentDidUpdate 和网络调用改进了代码。
【问题讨论】:
-
远程呼叫在哪里?
-
我不确定我是否理解您的意思,但我认为如果您的 this.dataSource 更新,TreeView 中的数据源也会更新。
-
我用你问我的东西改进了这个问题。