antv X6 Cell 父子节点问题

本文详细介绍了图形库中关于节点和边的操作,包括判断父子关系、遍历与过滤子节点、添加移除节点、属性操作等。核心方法如isParentOf、isChildOf用于检查节点间的包含关系,eachChild、filterChild则用于遍历和筛选子节点。此外,还涵盖了设置和获取属性、变更节点层级结构的相关函数,如getProp、setProp、removeProp等,以及如何获取和修改属性路径的值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

sParentOf(...)

isParentOf(child: Cell | null): boolean

返回当前节点是否是指定 Cell 的父节点。

参数

名称类型必选默认值描述
childCell | null

isChildOf(...)

isChildOf(parent: Cell | null): boolean

返回当前节点/边是否是指定节点的的子节点/边。

参数

名称类型必选默认值描述
parentCell | null

eachChild(...)

eachChild(iterator: (child: Cell, index: number, children: Cell[]) => void, context?: any): this

参数

名称类型必选默认值描述
iterator(child: Cell, index: number, children: Cell[]) => void迭代器函数。
contextany迭代器函数的执行上下文。

遍历子节点。

filterChild(...)

filterChild(iterator: (child: Cell, index: number, children: Cell[]) => boolean, context?: any): Cell[]

过滤子节点。

参数

名称类型必选默认值描述
iterator(child: Cell, index: number, children: Cell[]) => boolean过滤器函数。
contextany过滤器函数的执行上下文。

返回值

返回满足过滤条件的子节点/边,或者空数组。

getChildCount()

getChildCount(): number

获取子节点/边的数量。

返回值

返回子节点/边的数量,当没有子节点/边时返回 0

getChildIndex(...)

getChildIndex(child: Cell): number

获取子节点/边的索引。

参数

名称类型必选默认值描述
childCell

返回值

返回子节点/边的索引,当子节点不存在时返回 -1

getChildAt(...)

getChildAt(index: number): Cell | null

获取指定索引位置的子节点/边。

参数

名称类型必选默认值描述
indexnumber索引位置。

返回值

返回指定索引位置的子节点,当该节点不存在子节点/边、索引越界、子节点不存在时返回 null

getAncestors(...)

getAncestors(options?: { deep?: boolean }): Cell[]

获取所有祖先节点。

参数

名称类型必选默认值描述
options.deepbooleantrue默认递归获取所有祖先节点,设置为 false 时只返回当前节点/边的父节点。

返回值

返回祖先节点数组。

getDescendants(...)

getDescendants(options?: Cell.GetDescendantsOptions): Cell[]

获取所有子孙节点。

参数

名称类型必选默认值描述
options.deepbooleantrue默认递归获取所有子孙节点,设置为 false 时只返回当前节点孩子节点/边。
options.breadthFirstbooleanfalse默认使用深度优先算法,设置为 true 时使用广度优先搜索算法。

返回值

返回子孙节点/边的数组。

isDescendantOf(...)

isDescendantOf(ancestor: Cell | null, options?: { deep?: boolean }): boolean

返回当前节点/边是否是指定节点的子孙节点/边。

参数

名称类型必选默认值描述
ancestorCell | null指定节点。
options.deepbooleantrue默认递归判断指定节点的所有子孙节点/边,设置为 false 时只判断孩子节点/边。

isAncestorOf(...)

isAncestorOf(descendant: Cell | null, options?: { deep?: boolean }): boolean

返回当前节点是否是指定节点/边的祖先节点。

参数

名称类型必选默认值描述
descendantCell | null指定节点/边。
options.deepbooleantrue默认递归判断指定节点的所有子孙节点/边,设置为 false 时只判断孩子节点/边。

getCommonAncestor(...)

getCommonAncestor(...cells: (Cell | null | undefined)[]): Cell | null

参数

名称类型必选默认值描述
...cells(Cell | null | undefined)[]指定节点/边。

获取给定节点/边的共同祖先节点。

返回值

返回找到的共同祖先节点,否则没有共同祖先节点时返回 null

addTo(...)

addTo(model: Model, options?: Cell.SetOptions): this
addTo(graph: Graph, options?: Cell.SetOptions): this
addTo(parent: Cell, options?: Cell.SetOptions): this

将当前节点/边添加到画布或指定的父节点。

insertTo(...)

insertTo(parent: Cell, index?: number, options?: Cell.SetOptions): this

将当前节点/边插入到指定父节点的指定位置。

参数

名称类型必选默认值描述
parentCell父节点。
indexnumber要插入的索引位置,缺省时插入到末尾。
options.silentbooleanfalse为 true 时不触发 'change:parent' 事件和画布重绘。
options...othersobject其他自定义键值对,可以在事件回调中使用。

addChild(...)

addChild(child: Cell, options?: Cell.SetOptions): this

将指定的节点/边添加到当前节点的子节点的末尾。

参数

名称类型必选默认值描述
childCell指定的节点/边。
options.silentbooleanfalse为 true 时不触发 'change:children' 事件和画布重绘。
options...othersobject其他自定义键值对,可以在事件回调中使用。

insertChild(...)

insertChild(child: Cell, index?: number, options?: Cell.SetOptions): this

将指定的节点/边添加到当前节点的子节点的指定位置。

参数

名称类型必选默认值描述
childCell指定的节点/边。
indexnumber要插入的索引位置,缺省时插入到末尾。
options.silentbooleanfalse为 true 时不触发 'change:children' 事件和画布重绘。
options...othersobject其他自定义键值对,可以在事件回调中使用。

embed(...)

embed(child: Cell, options?: Cell.SetOptions): this

将指定的节点/边嵌入到当前节点,即将指定的节点/边插入到当前节点的末尾。

参数

名称类型必选默认值描述
childCell指定的节点/边。
options.silentbooleanfalse为 true 时不触发 'change:children' 事件和画布重绘。
options...othersobject其他自定义键值对,可以在事件回调中使用。

unembed(...)

unembed(child: Cell, options?: Cell.SetOptions): this

将指定的节点/边从当前节点中移除。

参数

名称类型必选默认值描述
childCell指定的节点/边。
options.silentbooleanfalse为 true 时不触发 'change:children' 事件和画布重绘。
options...othersobject其他自定义键值对,可以在事件回调中使用。

removeFromParent(...)

removeFromParent(options?: Cell.RemoveOptions): this

将当前节点从父节点中移除。

参数

名称类型必选默认值描述
options.deepbooleantrue默认递归移除所有子节点/边,设置为 false 时只移除当前节点/边。
options.silentbooleanfalse为 true 时不触发 'change:children' 事件和画布重绘。
options...othersobject其他自定义键值对,可以在事件回调中使用。

removeChild(...)

removeChild(child: Cell, options?: Cell.RemoveOptions): Cell | null

移除指定的子节点/边。

参数

名称类型必选默认值描述
childCell指定的节点/边。
options.deepbooleantrue默认递归移除所有子节点/边,设置为 false 时只移除当前节点/边。
options.silentbooleanfalse为 true 时不触发 'change:children' 事件和画布重绘。
options...othersobject其他自定义键值对,可以在事件回调中使用。

返回值

返回被移除的节点/边。

removeChildAt(...)

removeChildAt(index: number, options?: Cell.RemoveOptions): Cell | null

移除指定索引位置的子节点/边。

名称类型必选默认值描述
indexnumber指定的索引位置。
options.deepbooleantrue默认递归移除所有子节点/边,设置为 false 时只移除当前节点/边。
options.silentbooleanfalse为 true 时不触发 'change:children' 事件和画布重绘。
options...othersobject其他自定义键值对,可以在事件回调中使用。

返回值

返回被移除的节点/边。

remove(...)

remove(options?: Cell.RemoveOptions): this

先将当前节点/边从父节点,然后将其从画布中移除。

节点和边的属性 Properties

上面介绍的 markupattrszIndexdata 等基础选项,以及节点的 size、 positionangleports 等选项,还有边的 sourcetargetlabels 等选项,还有创建节点/边时提供的那些额外的键值对,我们都称为属性(Property)。

const rect = new Shape.Rect({
  x: 30,
  y: 30,
  width: 100,
  height: 40,
  attrs: {...},
  data: {...},
  zIndex: 10,
  sale: {...},
  product: {
    id: '1234',
    name: 'apple',
    price: 3.99,
  },
})

例如,上面代码中的 attrsdatazIndex 都是标准的属性,其中 x 和 y 是一对自定义选项,节点初始化时被转换为了 position 属性,同样 width 和 height 也是一对自定义选项,节点初始化时被转换为了 size 属性,最后剩余的 sale 和 product 两个对象是非标准的属性。

上面介绍了一些标准属性以及操作(get/set)这些标准属性的方法,下面再介绍几个比较通用的方法,这些方法对标准和非标准属性都适用。

getProp(...)

getProp<T>(key: string, defaultValue?: T): T

获取指定的属性值。

参数

名称类型必选默认值描述
keystring属性名称。
defaultValueTundefined默认值,当指定的属性不存在时返回该默认值。

用法

// 获取标准属性
const zIndex = rect.getProp<number>('zIndex')
const position = rect.getProp<{x: number; y: number}>('position')

// 获取非标准属性
const product = rect.getProp('product')

setProp(...)

/**
 * 设置指定的属性
 */
setProp(key: string, value: any, options?: Cell.SetOptions): this
/**
 * 批量设置属性,提供的属性与原属性进行深度 merge
 */
setProp(props: Partial<Properties>, options?: Cell.SetOptions): this

设置属性。

默认情况触发对应的 'change:xxx' 事件和画布重绘,当 options.silent 为 true 时不触发 'change:xxx' 事件和画布重绘。

参数

名称类型必选默认值描述
keystring属性名称。
valueany属性值。
propsPartial属性键值对,将与现有属性进行深度 merge
options.silentbooleanfalse为 true 时不触发 'change:markup' 事件和画布重绘。
options...othersobject其他自定义键值对,可以在事件回调中使用。

用法

设置单个属性:

rect.setProp('size', { width: 100, height: 30 })
rect.setProp('zIndex', 10)

通过键值对设置多个属性,提供的键值对将与现有属性进行深度 merge

rect.setProp({
  size: {
    width: 100, 
    height: 30, 
  },
  zIndex: 10,
})

当 options.silent 为 true 时,不触发对应的 'change:xxx' 事件和画布重绘:

rect.setProp('zIndex', 10, { silent: true })

在选项中支持其他自定义键值对,可以在事件回调用使用:

rect.setProp('zIndex', 10, { otherKey: 'otherValue', ... })

removeProp(...)

removeProp(path: string | string[], options?: Cell.SetOptions): this

删除指定路径的属性。

默认情况触发对应的 'change:xxx' 事件和画布重绘,当 options.silent 为 true 时不触发 'change:xxx' 事件和画布重绘。

参数

名称类型必选默认值描述
pathstring | string[]属性路径。
options.silentbooleanfalse为 true 时不触发 'change:markup' 事件和画布重绘。
options...othersobject其他自定义键值对,可以在事件回调中使用。

用法

删除单个属性:

rect.removeProp('zIndex')

删除指定路径的属性:

rect.removeProp('product/id')
// 或
rect.removeProp(['product', 'id'])

当 options.silent 为 true 时,不触发对应的 'change:xxx' 事件和画布重绘:

rect.removeProp('zIndex', { silent: true })

在选项中支持其他自定义键值对,可以在事件回调用使用:

rect.removeProp('zIndex', { otherKey: 'otherValue', ... })

getPropByPath(...)

getPropByPath<T>(path: string | string[]): T

获取指定路径的属性值。

参数

名称类型必选默认值描述
pathstring | string[]属性路径。
当 path 为 string 类型时,路径是以 '\' 分割的字符串。
当 path 为 string[] 类型时,路径是属性对象路径上的 Key 构成的数组。

用法

rect.getPropByPath('zIndex')
// 或
rect.getPropByPath(['zIndex'])

rect.getPropByPath('product/id')
// 或
rect.getPropByPath(['product', 'id'])

setPropByPath(...)

setPropByPath(path: string | string[], value: any, options?: Cell.SetByPathOptions): this

根据属性路径设置属性值。

默认情况触发对应的 'change:xxx' 事件和画布重绘,当 options.silent 为 true 时不触发 'change:xxx' 事件和画布重绘。

名称类型必选默认值描述
pathstring | string[]属性路径。
当 path 为 string 类型时,路径是以 '\' 分割的字符串。
当 path 为 string[] 类型时,路径是属性对象路径上的 Key 构成的数组。
valueany属性值。
options.rewritebooleanfalse默认与现路径上的现有属性进行深度 merge,设置为 true 时直接替换路径上的属性值。
options.silentbooleanfalse为 true 时不触发 'change:attrs' 事件和画布重绘。
options...othersobject其他自定义键值对,可以在事件回调中使用。

用法

默认情况触发对应的 'change:xxx' 事件和画布重绘:

rect.setPropByPath('zIndex', 10)
// 或
rect.setPropByPath(['zIndex'], 10)

rect.setPropByPath('product/price', 5.99)
// 或
rect.setPropByPath(['product', 'price'], 5.99)

当 options.silent 为 true 时不触发 'change:xxx' 事件和画布重绘:

rect.setPropByPath('zIndex', 10, { silent: true })
// 或
rect.setPropByPath(['zIndex'], 10, { silent: true })

rect.setPropByPath('product/price', 5.99, { silent: true })
// 或
rect.setPropByPath(['product', 'price'], 5.99, { silent: true })

默认与现路径上的现有属性进行深度 merge,当 options.rewrite 设置为 true 时直接替换路径上的属性值:

rect.setPropByPath(
  ['product'], 
  { id: '234', name: 'banana', price: 2.99 }, 
  { rewrite: true },
)

在选项中支持其他自定义键值对,可以在事件回调用使用:

rect.setPropByPath('zIndex', 10, { otherKey: 'otherValue', ... })

removePropByPath(...)

removePropByPath(path: string | string[], options?: Cell.SetOptions): this

删除指定路径的属性值。

参数

名称类型必选默认值描述
pathstring | string[]属性路径。
当 path 为 string 类型时,路径是以 '\' 分割的字符串。
当 path 为 string[] 类型时,路径是属性对象路径上的 Key 构成的数组。
options.silentbooleanfalse为 true 时不触发 'change:attrs' 事件和画布重绘。
options...othersobject其他自定义键值对,可以在事件回调中使用。

用法

默认情况触发对应的 'change:xxx' 事件和画布重绘:

rect.removePropByPath('zIndex')
// 或
rect.removePropByPath(['zIndex'])

rect.removePropByPath('product/price')
// 或
rect.removePropByPath(['product', 'price'])

当 options.silent 为 true 时不触发 'change:xxx' 事件和画布重绘:

rect.removePropByPath('zIndex', { silent: true })
// 或
rect.removePropByPath(['zIndex'], { silent: true })

rect.removePropByPath('product/price', { silent: true })
// 或
rect.removePropByPath(['product', 'price'], { silent: true })

在选项中支持其他自定义键值对,可以在事件回调用使用:

rect.removePropByPath('zIndex', { otherKey: 'otherValue', ... })

prop(...)

/**
 * 获取所有属性。
 */
prop(): Properties

/**
 * 获取指定路径的属性值。
 */
prop<T>(path: string | string[]): T

/**
 * 设置指定路径的属性值,与路径上的现有属性进行深度 merge。
 */
prop(path: string | string[], value: any, options?: Cell.SetOptions): this


/**
 * 设置属性,与现有属性进行深度 merge。
 */
prop(props: Partial<Properties>, options?: Cell.SetOptions): this

该方法是 getPropsetPropgetPropByPathsetPropByPath 和 removePropByPath 几个方法的整合,提供了上面四种函数签名,是一个非常实用的方法。

用法

获取属性:

rect.prop()
rect.prop('zIndex')
rect.prop('product/price')

设置属性:

rect.prop('zIndex', 10)
rect.prop('product/price', 5.99)
rect.prop({
  product: {
    id: '234',
    name: 'banana',
    price: 3.99,
  },
})

当 options.silent 为 true 时不触发 'change:xxx' 事件和画布重绘:

rect.prop('zIndex', 10, { silent: true })
// 或
rect.prop(['zIndex'], 10, { silent: true })

rect.prop('product/price', 5.99, { silent: true })
// 或
rect.prop(['product', 'price'], 5.99, { silent: true })

在选项中支持其他自定义键值对,可以在事件回调用使用:

rect.prop('zIndex', 10, { otherKey: 'otherValue', ... })

hasChanged(...)

hasChanged(key: string | undefined | null): boolean

返回指定的属性或所有属性是否已经改变。

参数

名称类型必选默认值描述
keystring | undefined | null属性名。缺省时表示检查所有属性。

用法

if (rect.hasChanged()) {
  // do something
}

if (rect.hasChanged('zIndex')) {
  // do something
}

previous(...)

previous<T>(name: string): T | undefined

当指定的属性发生改变后,获取改变前的属性值。

名称类型必选默认值描述
keystring属性名。

用法

if (rect.hasChanged('zIndex')) {
  const old = rect.previous('zIndex')
  // do something
}


 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值