Skip to content

fix(grid): fix drag error when tbody not render #3538

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 26, 2025
Merged
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
28 changes: 13 additions & 15 deletions packages/vue/src/grid/src/body/src/body.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,19 @@ export default defineComponent({

hooks.watch(body, (body) => body && resizeObserver.observe(body))

hooks.watch(tbody, (tbody) => {
if (tbody && $table.dropConfig) {
// 初始化行列拖拽
const { plugin, row = true, column = true, scheme } = $table.dropConfig

plugin && row && (vm.rowSortable = $table.rowDrop(body.value))

if (scheme !== 'v2') {
plugin && column && (vm.columnSortable = $table.columnDrop(body.value))
}
}
})
Comment on lines +753 to +764
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add cleanup logic to prevent memory leaks when tbody changes.

The implementation correctly fixes the timing issue by using a reactive watcher instead of setTimeout. However, since watchers can trigger multiple times, you should destroy existing sortable instances before creating new ones to prevent memory leaks.

Apply this diff to add proper cleanup:

 hooks.watch(tbody, (tbody) => {
   if (tbody && $table.dropConfig) {
+    // 清理之前的拖拽实例
+    if (vm.rowSortable) {
+      vm.rowSortable.destroy()
+      vm.rowSortable = null
+    }
+    if (vm.columnSortable) {
+      vm.columnSortable.destroy()
+      vm.columnSortable = null
+    }
+
     // 初始化行列拖拽
     const { plugin, row = true, column = true, scheme } = $table.dropConfig

     plugin && row && (vm.rowSortable = $table.rowDrop(body.value))

     if (scheme !== 'v2') {
       plugin && column && (vm.columnSortable = $table.columnDrop(body.value))
     }
   }
 })
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
hooks.watch(tbody, (tbody) => {
if (tbody && $table.dropConfig) {
// 初始化行列拖拽
const { plugin, row = true, column = true, scheme } = $table.dropConfig
plugin && row && (vm.rowSortable = $table.rowDrop(body.value))
if (scheme !== 'v2') {
plugin && column && (vm.columnSortable = $table.columnDrop(body.value))
}
}
})
hooks.watch(tbody, (tbody) => {
if (tbody && $table.dropConfig) {
// 清理之前的拖拽实例
if (vm.rowSortable) {
vm.rowSortable.destroy()
vm.rowSortable = null
}
if (vm.columnSortable) {
vm.columnSortable.destroy()
vm.columnSortable = null
}
// 初始化行列拖拽
const { plugin, row = true, column = true, scheme } = $table.dropConfig
plugin && row && (vm.rowSortable = $table.rowDrop(body.value))
if (scheme !== 'v2') {
plugin && column && (vm.columnSortable = $table.columnDrop(body.value))
}
}
})
🤖 Prompt for AI Agents
In packages/vue/src/grid/src/body/src/body.tsx around lines 753 to 764, the
watcher on tbody creates new sortable instances each time it triggers without
cleaning up previous instances, causing potential memory leaks. Modify the
watcher to first check if vm.rowSortable and vm.columnSortable exist and call
their destroy methods to clean up before assigning new sortable instances. This
ensures old instances are properly disposed before creating new ones.


hooks.watch(customFooter, (customFooter) => customFooter && resizeObserver.observe(customFooter))

hooks.watchEffect(() => {
Expand All @@ -767,21 +780,6 @@ export default defineComponent({
vm._throttleScrollHandler = throttle($table.optimizeOpts.scrollDelay, vm.handleScroll)

body.value?.addEventListener('scroll', vm._throttleScrollHandler)

// 初始化行列拖拽
setTimeout(() => {
const { dropConfig } = $table

if (dropConfig) {
const { plugin, row = true, column = true, scheme } = dropConfig

plugin && row && (vm.rowSortable = $table.rowDrop(body.value))

if (scheme !== 'v2') {
plugin && column && (vm.columnSortable = $table.columnDrop(body.value))
}
}
}, 50)
})

hooks.onBeforeUnmount(() => {
Expand Down
Loading