Skip to content

Commit 56f04a5

Browse files
authored
Merge pull request #90 from bgfxc4/main
added updateSort emit and its type declaration
2 parents 98c5fb3 + 66032a1 commit 56f04a5

File tree

7 files changed

+33
-5
lines changed

7 files changed

+33
-5
lines changed

src/components/DataTable.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ onMounted(() => {
314314
const emits = defineEmits([
315315
'clickRow',
316316
'expandRow',
317+
'updateSort',
317318
'update:itemsSelected',
318319
'update:serverOptions',
319320
]);
@@ -353,6 +354,7 @@ const {
353354
sortBy,
354355
sortType,
355356
updateServerOptionsSort,
357+
emits
356358
);
357359
358360
const {

src/hooks/useHeaders.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import {
22
ref, Ref, computed, ComputedRef, WritableComputedRef,
33
} from 'vue';
44
import type { Header, SortType } from '../types/main';
5-
import type { ServerOptionsComputed, HeaderForRender, ClientSortOptions } from '../types/internal';
5+
import type {
6+
ServerOptionsComputed, HeaderForRender, ClientSortOptions, EmitsEventName,
7+
} from '../types/internal';
68

79
export default function useHeaders(
810
checkboxColumnWidth: Ref<number>,
@@ -21,6 +23,7 @@ export default function useHeaders(
2123
sortBy: Ref<string>,
2224
sortType: Ref<SortType>,
2325
updateServerOptionsSort: (newSortBy: string, newSortType: SortType | null) => void,
26+
emits: (event: EmitsEventName, ...args: any[]) => void,
2427
) {
2528
const hasFixedColumnsFromUser = computed(() => headers.value.findIndex((header) => header.fixed) !== -1);
2629
const fixedHeadersFromUser = computed(() => {
@@ -113,6 +116,10 @@ export default function useHeaders(
113116
sortDesc: newSortType === 'desc',
114117
};
115118
}
119+
emits('updateSort', {
120+
sortType: newSortType,
121+
sortBy: newSortBy,
122+
});
116123
};
117124

118125
return {

src/modes/Client.vue

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
:body-row-class-name="bodyRowClassNameFunction"
3232
:header-item-class-name="headerItemClassNameFunction"
3333
:body-item-class-name="bodyItemClassNameFunction"
34-
must-sort
3534
@click-row="showItem"
35+
@update-sort="updateSort"
3636
>
3737
<template #expand="item">
3838
<div style="padding: 15px">
@@ -98,7 +98,7 @@ import {
9898
computed, ref, reactive, toRefs,
9999
} from 'vue';
100100
import type {
101-
Header, Item, FilterOption, ClickRowArgument, HeaderItemClassNameFunction, BodyItemClassNameFunction, BodyRowClassNameFunction,
101+
Header, Item, FilterOption, ClickRowArgument, UpdateSortArgument, HeaderItemClassNameFunction, BodyItemClassNameFunction, BodyRowClassNameFunction,
102102
} from '../types/main';
103103
import DataTable from '../components/DataTable.vue';
104104
import { mockClientNestedItems, mockClientItems, mockDuplicateClientNestedItems } from '../mock';
@@ -146,6 +146,10 @@ const showItem = (item: ClickRowArgument) => {
146146
console.log('item');
147147
console.log(JSON.stringify(item));
148148
};
149+
150+
const updateSort = (sortOption: UpdateSortArgument) => {
151+
console.log(sortOption);
152+
};
149153
// filtering
150154
151155
const ageCriteria = ref<[number, number]>([1, 15]);

src/modes/ServerSide.vue

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
table-class-name="hc-table"
2525
theme-color="#1d90ff"
2626
border-cell
27+
@update-sort="updateSort"
2728
>
2829
<template #expand="item">
2930
<div style="padding: 15px">
@@ -89,7 +90,7 @@ import {
8990
defineComponent, ref, computed, watch, onMounted, Ref,
9091
} from 'vue';
9192
import {
92-
Header, Item, ServerOptions, BodyItemClassNameFunction, BodyRowClassNameFunction, HeaderItemClassNameFunction,
93+
Header, Item, ServerOptions, UpdateSortArgument, BodyItemClassNameFunction, BodyRowClassNameFunction, HeaderItemClassNameFunction,
9394
} from '../types/main';
9495
import DataTable from '../components/DataTable.vue';
9596
import { mockClientItems, mockServerItems } from '../mock';
@@ -164,6 +165,9 @@ export default defineComponent({
164165
const currentPageLastIndex = computed(() => dataTable.value?.currentPageLastIndex);
165166
const clientItemsLength = computed(() => dataTable.value?.clientItemsLength);
166167
168+
const updateSort = (sortOption: UpdateSortArgument) => {
169+
console.log(sortOption);
170+
};
167171
console.log('dataTable');
168172
console.log(dataTable.value);
169173
@@ -206,6 +210,7 @@ export default defineComponent({
206210
prevPage,
207211
updatePage,
208212
bodyRowClassName,
213+
updateSort,
209214
};
210215
},
211216

src/types/internal.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ export type ClientSortOptions = {
2323

2424
export type MultipleSelectStatus = 'allSelected' | 'noneSelected' | 'partSelected'
2525

26-
export type EmitsEventName = 'clickRow' | 'expandRow' | 'update:itemsSelected' | 'update:serverOptions'
26+
export type EmitsEventName = 'clickRow' | 'expandRow' | 'updateSort' | 'update:itemsSelected' | 'update:serverOptions'

src/types/main.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ export type ClickRowArgument = Item & {
4242
indexInCurrentPage?: number
4343
}
4444

45+
export type UpdateSortArgument = {
46+
sortType: SortType | null
47+
sortBy: string
48+
}
49+
4550
export type HeaderItemClassNameFunction = (header: Header, index: number) => string
4651
export type BodyRowClassNameFunction = (item: Item, index: number) => string
4752
export type BodyItemClassNameFunction = (column: string, index: number) => string

types/main.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ export type ClickRowArgument = Item & {
4242
indexInCurrentPage?: number
4343
}
4444

45+
export type UpdateSortArgument = {
46+
sortType: SortType | null
47+
sortBy: string
48+
}
49+
4550
export type HeaderItemClassNameFunction = (header: Header, index: number) => string
4651
export type BodyRowClassNameFunction = (item: Item, index: number) => string
4752
export type BodyItemClassNameFunction = (column: string, index: number) => string

0 commit comments

Comments
 (0)