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 Aug 1, 2023
1 parent 437ce23 commit b53afec
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 26 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
- Fix `n-tree`'s `TreeOption`'s `checkboxDisabled` prop doesn't work when `check-on-click` is `true`.
- Fix rapid clicks on `n-date-input`'s buttons triggering a text select for the rest of the website.
- Fix `n-auto-complete`'s autocomplete menu's unexpected open when clicking the clear icon with the input not focused, closes [#4658](https://github.com/tusen-ai/naive-ui/issues/4658).
- Fix `n-data-table` incorrect position when the fixed columns can be draggable, closes [#5050](https://github.com/tusen-ai/naive-ui/issues/5050).

### Features

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
- 修复 `n-tree` `check-on-click``true` 时,`TreeOption` `checkboxDisabled` 不生效
- 修复 `n-date-input` 的按钮快速点击时网站其余文本会被选中
- 修复 `n-auto-complete` 在未聚焦状态下点击清除按钮时补全菜单意外打开的问题,关闭 [#4658](https://github.com/tusen-ai/naive-ui/issues/4658)
- 修复 `n-data-table` 在固定列设为可拖拽时定位不正确,关闭 [#5050](https://github.com/tusen-ai/naive-ui/issues/5050)

### Features

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"
: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"
: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 @@ -151,7 +151,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 b53afec

Please sign in to comment.