Skip to content

Commit

Permalink
fix(data-table): incorrect position when the fixed columns can be dra…
Browse files Browse the repository at this point in the history
…ggable, closes #5050
  • Loading branch information
jizai1125 committed Jan 12, 2025
1 parent c96427d commit 4550f94
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 19 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.en-US.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# CHANGELOG

## NEXT_VERSION

### Fixes

- Fix `n-data-table` incorrect position when the fixed columns can be draggable, closes [#5050](https://github.com/tusen-ai/naive-ui/issues/5050).

## 2.41.0

### Breaking Changes
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.zh-CN.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# CHANGELOG

## NEXT_VERSION

### Fixes

- 修复 `n-data-table` 在固定列设置可拖拽时定位不正确,关闭 [#5050](https://github.com/tusen-ai/naive-ui/issues/5050)

## 2.41.0

`2025-01-05`
Expand Down
30 changes: 24 additions & 6 deletions src/data-table/demos/enUS/column-draggable.demo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { defineComponent, h } from 'vue'
interface Song {
no: number
title: string
band: string
length: string
}
Expand All @@ -21,26 +22,41 @@ function createColumns({
play: (row: Song) => void
}): DataTableColumns<Song> {
return [
{
type: 'selection',
fixed: 'left'
},
{
title: 'No',
key: 'no',
width: 50
width: 100,
fixed: 'left',
resizable: true
},
{
title: 'Title',
key: 'title',
width: 200,
fixed: 'left',
resizable: true
},
{
title: 'Length',
key: 'length',
resizable: true,
minWidth: 50,
maxWidth: 100
title: 'Length (minWidth: 100, maxWidth: 500)',
key: 'length'
},
{
title: 'Band',
key: 'band',
width: 100,
fixed: 'right',
resizable: true
},
{
title: 'Action',
key: 'actions',
fixed: 'right',
width: 100,
resizable: true,
render(row) {
return h(
NButton,
Expand Down Expand Up @@ -81,9 +97,11 @@ export default defineComponent({

<template>
<n-data-table
:row-key="(row) => row.no"
:columns="columns"
:data="data"
:pagination="pagination"
:bordered="false"
:scroll-x="1800"
/>
</template>
31 changes: 24 additions & 7 deletions src/data-table/demos/zhCN/column-draggable.demo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { defineComponent, h } from 'vue'
interface Song {
no: number
title: string
band: string
length: string
}
Expand All @@ -21,27 +22,42 @@ function createColumns({
play: (row: Song) => void
}): DataTableColumns<Song> {
return [
{
type: 'selection',
fixed: 'left'
},
{
title: 'No',
key: 'no',
width: 50
width: 100,
fixed: 'left',
resizable: true
},
{
title: 'Title',
key: 'title',
width: 200,
fixed: 'left',
resizable: true
},
{
title: 'Length (minWidth: 100, maxWidth: 500)',
key: 'length',
resizable: true,
minWidth: 100,
maxWidth: 500
key: 'length'
},
{
title: 'Band',
key: 'band',
width: 100,
fixed: 'right',
resizable: true
},
{
title: 'Action',
key: 'actions',
render(row) {
fixed: 'right',
width: 100,
resizable: true,
render (row) {

Check failure on line 60 in src/data-table/demos/zhCN/column-draggable.demo.vue

View workflow job for this annotation

GitHub Actions / lint (22)

Unexpected space before function parentheses
return h(
NButton,
{
Expand Down Expand Up @@ -81,9 +97,10 @@ export default defineComponent({

<template>
<n-data-table
:row-key="(row) => row.no"
:columns="columns"
:data="data"
:pagination="pagination"
:bordered="false"
:scroll-x="1800"
/>
</template>
3 changes: 2 additions & 1 deletion src/data-table/src/DataTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ export default defineComponent({
} = useScroll(props, {
bodyWidthRef,
mainTableInstRef,
mergedCurrentPageRef
mergedCurrentPageRef,
getResizableWidth
})
const { localeRef } = useLocale('DataTable')
const mergedTableLayoutRef = computed(() => {
Expand Down
26 changes: 21 additions & 5 deletions src/data-table/src/use-scroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ export function useScroll(
{
mainTableInstRef,
mergedCurrentPageRef,
bodyWidthRef
bodyWidthRef,
getResizableWidth
}: {
bodyWidthRef: Ref<null | number>
mainTableInstRef: Ref<MainTableRef | null>
mergedCurrentPageRef: ComputedRef<number>
getResizableWidth: (key: ColumnKey) => number | undefined
}
) {
let lastScrollLeft = 0
Expand All @@ -45,13 +47,20 @@ export function useScroll(
function traverse(cols: TableColumn[]): void {
cols.forEach((col) => {
const positionInfo = { start: left, end: 0 }
columns[getColKey(col)] = positionInfo
const key = getColKey(col)
columns[key] = positionInfo
if ('children' in col) {
traverse(col.children)
positionInfo.end = left
}
else {
left += getNumberColWidth(col) || 0
const resizeWidth = getResizableWidth(key)
if (resizeWidth) {
left += resizeWidth
}
else {
left += getNumberColWidth(col) || 0
}
positionInfo.end = left
}
})
Expand All @@ -69,13 +78,20 @@ export function useScroll(
for (let i = cols.length - 1; i >= 0; --i) {
const col = cols[i]
const positionInfo = { start: right, end: 0 }
columns[getColKey(col)] = positionInfo
const key = getColKey(col)
columns[key] = positionInfo
if ('children' in col) {
traverse(col.children)
positionInfo.end = right
}
else {
right += getNumberColWidth(col) || 0
const resizeWidth = getResizableWidth(key)
if (resizeWidth) {
right += resizeWidth
}
else {
right += getNumberColWidth(col) || 0
}
positionInfo.end = right
}
}
Expand Down

0 comments on commit 4550f94

Please sign in to comment.