Skip to content

fix: adapt to tree children use splice to add row #3543

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion packages/vue/src/grid/src/composable/useData.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { hooks } from '@opentiny/vue-common'
import { getRowid } from '@opentiny/vue-renderless/grid/utils'

const isContented = (array) => Array.isArray(array) && array.length > 0
Expand Down Expand Up @@ -102,7 +103,6 @@ export const buildRenderGraph = ($table) => {
export const tileFullData = ($table) => {
const { treeConfig, rowGroup, groupFullData, afterFullData } = $table

// @ts-expect-error
const tileInfo = makeTile({
list: !treeConfig && rowGroup?.field ? groupFullData : afterFullData,
getID: (row) => getRowid($table, row),
Expand All @@ -124,3 +124,37 @@ export const graphFullData = ($table) => {
$table._graphInfo = graphInfo
}
}

const getTreeLength = (tree, childrenKey = 'children') => {
let length = tree.length

for (const node of tree) {
const children = node[childrenKey]

if (children && children.length > 0) {
length += getTreeLength(children, childrenKey)
}
}

return length
}

const getTiledLength = (props) => {
const data = props.data || []
const { children: childrenKey } = props.treeConfig || {}

return props.treeConfig ? getTreeLength(data, childrenKey) : data.length
}

export const useData = (props) => {
const $table = hooks.getCurrentInstance()?.proxy
// 原始数据展开长度
const tiledLength = hooks.ref(0)

hooks.watch([() => props.data, () => getTiledLength(props)], ([_, length]) => {
tiledLength.value = length
$table?.handleDataChange()
})

return { tiledLength }
}
17 changes: 1 addition & 16 deletions packages/vue/src/grid/src/table/src/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ import {
onScrollXLoad
} from './utils/refreshColumn'
import { mapFetchColumnPromise } from './utils/handleResolveColumn'
import { hooks, isVue2 } from '@opentiny/vue-common'
import { hooks } from '@opentiny/vue-common'
import { computeScrollYLoad, computeScrollXLoad } from './utils/computeScrollLoad'
import { calcTableWidth, calcFixedDetails } from './utils/autoCellWidth'
import { funcs, headerProps, handleAllColumnPromises } from './funcs'
Expand Down Expand Up @@ -1983,21 +1983,6 @@ const Methods = {
this._isUpdateData = false
}
},
watchDataForVue3() {
if (isVue2) return

const stopWatch = hooks.watch(
[() => this.data, () => this.data && this.data.length],
([newData, newLength], [oldData, oldLength]) => {
// vue3下额外监控数组长度改变,解决push无响应等问题
if (Array.isArray(this.data) && newData === oldData && newLength !== oldLength) {
this.handleDataChange()
}
}
)

hooks.onBeforeUnmount(() => stopWatch())
},
getVm(name) {
return this.$grid.getVm(name)
},
Expand Down
14 changes: 5 additions & 9 deletions packages/vue/src/grid/src/table/src/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import methods from './methods'
import GlobalConfig from '../../config'
import { error } from '../../tools'
import MfTable from '../../mobile-first/index.vue'
import { useDrag, useRowGroup } from '../../composable'
import { useData, useDrag, useRowGroup } from '../../composable'
import { isServer } from '@opentiny/utils'

const { themes, viewConfig, columnLevelKey, defaultColumnName } = GlobalConfig
Expand Down Expand Up @@ -527,10 +527,6 @@ export default defineComponent({
!this.isUpdateCustoms && this.mergeCustomColumn(value)
this.isUpdateCustoms = false
},
data() {
// data的监控处理:a、在vue2中,数组对象替换、数组长度改变和数组项属性改变;b、在vue3中,数组对象替换
this.handleDataChange()
},
height() {
this.$nextTick(this.recalculate)
},
Expand Down Expand Up @@ -619,9 +615,6 @@ export default defineComponent({

bindEvent(this)

// vue3下额外监控数组长度改变,解决push无响应等问题
this.watchDataForVue3()

// 设置表格实例
this.$grid.connect({ name: 'table', vm: this })
},
Expand Down Expand Up @@ -735,6 +728,8 @@ export default defineComponent({

useRowGroup({ props, visibleColumn, tableFullColumn, tableColumn, columnStore })

const { tiledLength } = useData(props)

hooks.onMounted(() => {
$table.addIntersectionObserver()

Expand Down Expand Up @@ -823,7 +818,8 @@ export default defineComponent({
bodyWrapperMaxHeight,
bodyTableWidth,
scrollLoadScrollHeight,
columnStore
columnStore,
tiledLength
}
},
render() {
Expand Down
Loading