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 Mar 7, 2024
1 parent 2f4cd55 commit c2deaeb
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 26 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.38.1

`2024-02-26`
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.38.1

`2024-02-26`
Expand Down
52 changes: 42 additions & 10 deletions src/data-table/demos/enUS/column-draggable.demo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ Only support leaf nodes.

<template>
<n-data-table
:row-key="(d) => d.no"

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

View workflow job for this annotation

GitHub Actions / lint (20)

Parameter 'd' implicitly has an 'any' type.
:columns="columns"
:data="data"
:pagination="pagination"
:bordered="false"
:scroll-x="1800"
/>
</template>

Expand All @@ -21,35 +23,50 @@ import type { DataTableColumns } from 'naive-ui'
type Song = {
no: number
title: string
band: string
length: string
}
const createColumns = ({
play
}: {
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 All @@ -67,9 +84,24 @@ const createColumns = ({
}
const data: Song[] = [
{ no: 3, title: 'Wonderwall', length: '4:18' },
{ no: 4, title: "Don't Look Back in Anger", length: '4:48' },
{ no: 12, title: 'Champagne Supernova', length: '7:27' }
{
no: 3,
title: 'Wonderwall',
band: 'Oasis',
length: '4:18'
},
{
no: 4,
title: "Don't Look Back in Anger",
band: 'Oasis',
length: '4:48'
},
{
no: 12,
title: 'Champagne Supernova',
band: 'Oasis',
length: '7:27'
}
]
export default defineComponent({
Expand Down
51 changes: 41 additions & 10 deletions src/data-table/demos/zhCN/column-draggable.demo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@

<template>
<n-data-table
:row-key="(d) => d.no"

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

View workflow job for this annotation

GitHub Actions / lint (20)

Parameter 'd' implicitly has an 'any' type.
:columns="columns"
:data="data"
:pagination="pagination"
:bordered="false"
:scroll-x="1800"
/>
</template>

Expand All @@ -21,35 +22,50 @@ import type { DataTableColumns } from 'naive-ui'
type Song = {
no: number
title: string
band: string
length: string
}
const createColumns = ({
play
}: {
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',
fixed: 'right',
width: 100,
resizable: true,
render (row) {
return h(
NButton,
Expand All @@ -67,9 +83,24 @@ const createColumns = ({
}
const data: Song[] = [
{ no: 3, title: 'Wonderwall', length: '4:18' },
{ no: 4, title: "Don't Look Back in Anger", length: '4:48' },
{ no: 12, title: 'Champagne Supernova', length: '7:27' }
{
no: 3,
title: 'Wonderwall',
band: 'Oasis',
length: '4:18'
},
{
no: 4,
title: "Don't Look Back in Anger",
band: 'Oasis',
length: '4:48'
},
{
no: 12,
title: 'Champagne Supernova',
band: 'Oasis',
length: '7:27'
}
]
export default defineComponent({
Expand Down
3 changes: 2 additions & 1 deletion src/data-table/src/DataTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ export default defineComponent({
} = useScroll(props, {
bodyWidthRef,
mainTableInstRef,
mergedCurrentPageRef
mergedCurrentPageRef,
getResizableWidth
})
const { localeRef } = useLocale('DataTable')
const mergedTableLayoutRef = computed(() => {
Expand Down
24 changes: 19 additions & 5 deletions src/data-table/src/use-scroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,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 @@ -46,12 +48,18 @@ 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,12 +77,18 @@ 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 c2deaeb

Please sign in to comment.