antvx6綁定子節(jié)點(diǎn)后子節(jié)點(diǎn)getParent()為null?

問(wèn)題原因:

antvx6中的setChildren()、setParent()都是單向綁定,如果使用setChildren()或設(shè)置父節(jié)點(diǎn)的children屬性,只能讓父認(rèn)子,而子不認(rèn)父,父節(jié)點(diǎn)調(diào)getChildren()有值,而子節(jié)點(diǎn)調(diào)getParent()為null。

解決方案:

使用addChild()可以雙向綁定。如果是通過(guò)預(yù)設(shè)數(shù)據(jù)的方式,可以在父節(jié)點(diǎn)設(shè)置children綁定子節(jié)點(diǎn),同時(shí)在子節(jié)點(diǎn)設(shè)置parent綁定父節(jié)點(diǎn),實(shí)現(xiàn)雙向綁定。

預(yù)設(shè)數(shù)據(jù):

const children = {
  id: 'children',
  shape: 'custom-node',
  label: 'childrenNode',
  ...
  parent: 'parent' // 綁定父節(jié)點(diǎn),getParent()有值,否則無(wú)值
}

const parent = {
  id: 'parent',
  shape: 'custom-node',
  label: 'parentNode',
  ...
  children: ['children'] // 綁定子節(jié)點(diǎn)
}

Graph.fromJSON([children, parent])

調(diào)用API:

const children = graph.addNode({
  shape: 'custom-node',
  label: 'childrenNode'
})

const parent = graph.addNode({
  shape: 'custom-node',
  label: 'parentNode'
})

parent.setChildren([children]) // 單向綁定,入?yún)閿?shù)組,子項(xiàng)getParent()為null
parent.addChild(children) // 雙向綁定,入?yún)閏ell對(duì)象
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容