Skip to content

Commit 658c990

Browse files
authored
fix: 修复列排序bug,d-column增加默认插槽 (#232)
1 parent 3a25cac commit 658c990

File tree

6 files changed

+412
-354
lines changed

6 files changed

+412
-354
lines changed

packages/devui-vue/devui/table/src/column/use-column.ts

Lines changed: 51 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { watch, reactive, onBeforeMount, ToRefs, Slots, h } from 'vue';
2-
import { Column, TableColumnPropsTypes } from './column.type'
2+
import { Column, TableColumnPropsTypes } from './column.type';
33
import { formatWidth, formatMinWidth } from '../utils';
44

5+
function defaultRenderHeader(this: Column) {
6+
return h('span', { class: 'title' }, this.header);
7+
}
58

6-
export function createColumn<T extends Record<string, unknown> = any>(
7-
props: ToRefs<TableColumnPropsTypes>,
8-
templates: Slots
9-
): Column<T> {
9+
export function createColumn<T extends Record<string, unknown> = any>(props: ToRefs<TableColumnPropsTypes>, templates: Slots): Column<T> {
1010
const {
1111
field,
1212
header,
@@ -20,43 +20,63 @@ export function createColumn<T extends Record<string, unknown> = any>(
2020
filterMultiple,
2121
order,
2222
fixedLeft,
23-
fixedRight
23+
fixedRight,
2424
} = props;
2525
const column: Column = reactive({});
2626

27-
watch([field, header, order], ([field, header, order]) => {
28-
column.field = field;
29-
column.header = header;
30-
column.order = order;
31-
}, { immediate: true });
27+
function defaultRenderCell<K extends Record<string, unknown>>(rowData: K, index: number) {
28+
const value = rowData[this.field];
29+
if (templates.default) {
30+
return templates.default(rowData);
31+
}
32+
if (this.formatter) {
33+
return this.formatter(rowData, value, index);
34+
}
35+
36+
return value?.toString?.() ?? '';
37+
}
38+
39+
watch(
40+
[field, header, order],
41+
([fieldVal, headerVal, orderVal]) => {
42+
column.field = fieldVal;
43+
column.header = headerVal;
44+
column.order = orderVal;
45+
},
46+
{ immediate: true }
47+
);
3248

3349
// 排序功能
34-
watch([sortable, compareFn], ([sortable, compareFn]) => {
35-
column.sortable = sortable;
36-
column.compareFn = compareFn;
37-
})
50+
watch([sortable, compareFn], ([sortableVal, compareFnVal]) => {
51+
column.sortable = sortableVal;
52+
column.compareFn = compareFnVal;
53+
});
3854

3955
// 过滤功能
40-
watch([
41-
filterable,
42-
filterList,
43-
filterMultiple,
44-
], ([filterable, filterList, filterMultiple]) => {
45-
column.filterable = filterable;
46-
column.filterMultiple = filterMultiple;
47-
column.filterList = filterList;
48-
}, { immediate: true })
56+
watch(
57+
[filterable, filterList, filterMultiple],
58+
([filterableVal, filterListVal, filterMultipleVal]) => {
59+
column.filterable = filterableVal;
60+
column.filterMultiple = filterMultipleVal;
61+
column.filterList = filterListVal;
62+
},
63+
{ immediate: true }
64+
);
4965

5066
// 固定左右功能
51-
watch([fixedLeft, fixedRight], ([left, right]) => {
52-
column.fixedLeft = left;
53-
column.fixedRight = right;
54-
}, { immediate: true });
67+
watch(
68+
[fixedLeft, fixedRight],
69+
([left, right]) => {
70+
column.fixedLeft = left;
71+
column.fixedRight = right;
72+
},
73+
{ immediate: true }
74+
);
5575

5676
// 宽度
57-
watch([width, minWidth], ([width, minWidth]) => {
58-
column.width = formatWidth(width);
59-
column.minWidth = formatMinWidth(minWidth);
77+
watch([width, minWidth], ([widthVal, minWidthVal]) => {
78+
column.width = formatWidth(widthVal);
79+
column.minWidth = formatMinWidth(minWidthVal);
6080
column.realWidth = column.width || column.minWidth;
6181
});
6282

@@ -71,15 +91,3 @@ export function createColumn<T extends Record<string, unknown> = any>(
7191

7292
return column;
7393
}
74-
75-
function defaultRenderHeader(this: Column) {
76-
return h('span', { class: 'title' }, this.header);
77-
}
78-
79-
function defaultRenderCell<T extends Record<string, unknown>>(this: Column, rowData: T, index: number) {
80-
const value = rowData[this.field];
81-
if (this.formatter) {
82-
return this.formatter(rowData, value, index);
83-
}
84-
return value?.toString?.() ?? '';
85-
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { defineComponent } from 'vue';
2+
import ColGroup from './colgroup/colgroup';
3+
import TableHeader from './header/header';
4+
import TableBody from './body/body';
5+
6+
export default defineComponent({
7+
props: {
8+
classes: {
9+
type: Object,
10+
default: () => ({}),
11+
},
12+
isEmpty: {
13+
type: Boolean,
14+
},
15+
},
16+
setup(props: { classes: Record<string, unknown>; isEmpty: boolean }) {
17+
return () => {
18+
return (
19+
<div class='devui-table-view'>
20+
<div style='overflow:hidden scroll;'>
21+
<table class={props.classes} cellpadding='0' cellspacing='0'>
22+
<ColGroup />
23+
<TableHeader />
24+
</table>
25+
</div>
26+
<div class='scroll-view'>
27+
<table class={props.classes} cellpadding='0' cellspacing='0'>
28+
<ColGroup />
29+
{!props.isEmpty && <TableBody style='flex: 1' />}
30+
</table>
31+
</div>
32+
</div>
33+
);
34+
};
35+
},
36+
});
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { defineComponent } from 'vue';
2+
import ColGroup from './colgroup/colgroup';
3+
import TableHeader from './header/header';
4+
import TableBody from './body/body';
5+
6+
export default defineComponent({
7+
props: {
8+
classes: {
9+
type: Object,
10+
default: () => ({}),
11+
},
12+
isEmpty: {
13+
type: Boolean,
14+
},
15+
},
16+
setup(props: { classes: Record<string, unknown>; isEmpty: boolean }) {
17+
return () => {
18+
return (
19+
<table class={props.classes} cellpadding='0' cellspacing='0'>
20+
<ColGroup />
21+
<TableHeader style='position:relative' />
22+
{!props.isEmpty && <TableBody />}
23+
</table>
24+
);
25+
};
26+
},
27+
});

0 commit comments

Comments
 (0)