Node.js 反应可排序树-我可以给出外部按钮吗;添加节点";通过查找具有树数据的类型?

Node.js 反应可排序树-我可以给出外部按钮吗;添加节点";通过查找具有树数据的类型?,node.js,reactjs,Node.js,Reactjs,我想通过外部添加按钮将节点添加到父节点,方法是将所选类型与树匹配。但我没有在树外获取树索引/路径。所以我传递了硬编码的父密钥来添加子节点。但如果任何节点被删除,则将更改父密钥。所以我的代码不起作用 if (this.state.parentNodeValue === 'Position') { parentKey = 10; nodeClassName = 'fa-position div'; } let NEW_NODE = { title: this.state.engli

我想通过外部添加按钮将节点添加到父节点,方法是将所选类型与树匹配。但我没有在树外获取树索引/路径。所以我传递了硬编码的父密钥来添加子节点。但如果任何节点被删除,则将更改父密钥。所以我的代码不起作用

if (this.state.parentNodeValue === 'Position') {

  parentKey = 10;
  nodeClassName = 'fa-position div';

}

let NEW_NODE = {
  title: this.state.englishName,
  arabicTitle: this.state.arabicName,
  className: nodeClassName
};

let newTree = addNodeUnderParent({
  treeData: this.state.treeData,
  newNode: NEW_NODE,
  expandParent: true,
  parentKey: parentKey,
  getNodeKey: ({
    treeIndex
  }) => treeIndex
});
this.setState({
  treeData: newTree.treeData,
  modalOuterAddNode: !this.state.modalOuterAddNode,
}, () => {
  // console.log('treeData : ' + JSON.stringify(this.state.treeData));
});

通过此代码将获得标题数组及其索引。然后我们可以将子节点添加到这些父索引中

设titleArray=[]

    var buildTree = function (tree) {
        _.each(tree, function (item) {
            let titleObj = {};
            // titleObj.id = item.id;
            titleObj.title = item.title;
            titleArray.push(titleObj);
            if (item.children) buildTree(item.children);
        });
    }
    buildTree(tree);

通过此代码将获得标题数组及其索引。然后我们可以将子节点添加到这些父索引中

设titleArray=[]

    var buildTree = function (tree) {
        _.each(tree, function (item) {
            let titleObj = {};
            // titleObj.id = item.id;
            titleObj.title = item.title;
            titleArray.push(titleObj);
            if (item.children) buildTree(item.children);
        });
    }
    buildTree(tree);

嗨,这是我自己解决的。在“外部添加”按钮上,打开带有现有树标题列表的表单。之后,我得到了点击标题的索引。然后我将子元素添加到父索引中。嗨,我自己解决了这个问题。在“外部添加”按钮上,打开带有现有树标题列表的表单。之后,我得到了点击标题的索引。然后,我将child添加到此父索引中。