mirror of
https://github.com/bufanyun/hotgo.git
synced 2025-01-23 10:50:24 +08:00
commit
905439f4ca
@ -3,5 +3,6 @@ import useBoolean from './useBoolean';
|
|||||||
import useLoading from './useLoading';
|
import useLoading from './useLoading';
|
||||||
import useLoadingEmpty from './useLoadingEmpty';
|
import useLoadingEmpty from './useLoadingEmpty';
|
||||||
import useSendCode from './useSendCode';
|
import useSendCode from './useSendCode';
|
||||||
|
import useSorter from './useSorter';
|
||||||
|
|
||||||
export { useContext, useBoolean, useLoading, useLoadingEmpty, useSendCode };
|
export { useContext, useBoolean, useLoading, useLoadingEmpty, useSendCode, useSorter };
|
||||||
|
40
web/src/hooks/common/useSorter.ts
Normal file
40
web/src/hooks/common/useSorter.ts
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
import { ref } from 'vue';
|
||||||
|
import { DataTableSortState } from 'naive-ui';
|
||||||
|
import { SorterMultiple } from 'naive-ui/es/data-table/src/interface';
|
||||||
|
|
||||||
|
export default function useSorter(callback: Function = function () {}) {
|
||||||
|
const sortStatesRef = ref<DataTableSortState[]>([]);
|
||||||
|
|
||||||
|
function updateSorter(sorter: DataTableSortState | DataTableSortState[] | null) {
|
||||||
|
if (sorter === null) {
|
||||||
|
// 默认排序
|
||||||
|
sortStatesRef.value = [];
|
||||||
|
} else if (Array.isArray(sorter)) {
|
||||||
|
// 多字段排序
|
||||||
|
sortStatesRef.value = filterMultipleSorters(sorter);
|
||||||
|
} else {
|
||||||
|
// 单字段排序
|
||||||
|
sortStatesRef.value = [];
|
||||||
|
sortStatesRef.value.push(sorter);
|
||||||
|
}
|
||||||
|
callback();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 多字段排序,将不参与排序的字段过滤掉,再根据设置的优先级对排序器重新排列
|
||||||
|
function filterMultipleSorters(sorters: DataTableSortState[]): DataTableSortState[] {
|
||||||
|
const filter = sorters.filter((item) => item.order !== false);
|
||||||
|
return filter.sort((a, b) => {
|
||||||
|
if (typeof a.sorter === 'object' && typeof b.sorter === 'object') {
|
||||||
|
const aSorter = a.sorter as SorterMultiple;
|
||||||
|
const bSorter = b.sorter as SorterMultiple;
|
||||||
|
return bSorter.multiple - aSorter.multiple;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
updateSorter,
|
||||||
|
sortStatesRef,
|
||||||
|
};
|
||||||
|
}
|
@ -78,13 +78,13 @@
|
|||||||
import { useDialog, useMessage } from 'naive-ui';
|
import { useDialog, useMessage } from 'naive-ui';
|
||||||
import { BasicTable, TableAction } from '@/components/Table';
|
import { BasicTable, TableAction } from '@/components/Table';
|
||||||
import { BasicForm, useForm } from '@/components/Form/index';
|
import { BasicForm, useForm } from '@/components/Form/index';
|
||||||
|
import { useSorter } from '@/hooks/common';
|
||||||
import { Delete, List, Status, Export } from '@/api/addons/hgexample/table';
|
import { Delete, List, Status, Export } from '@/api/addons/hgexample/table';
|
||||||
import { State, columns, schemas, options, newState } from './model';
|
import { State, columns, schemas, options, newState } from './model';
|
||||||
import { DeleteOutlined, PlusOutlined, ExportOutlined } from '@vicons/antd';
|
import { DeleteOutlined, PlusOutlined, ExportOutlined } from '@vicons/antd';
|
||||||
import { useRouter } from 'vue-router';
|
import { useRouter } from 'vue-router';
|
||||||
import { getOptionLabel } from '@/utils/hotgo';
|
import { getOptionLabel } from '@/utils/hotgo';
|
||||||
import Edit from './edit.vue';
|
import Edit from './edit.vue';
|
||||||
import { Sorter } from '/#/table';
|
|
||||||
|
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const dialog = useDialog();
|
const dialog = useDialog();
|
||||||
@ -95,7 +95,7 @@
|
|||||||
const showModal = ref(false);
|
const showModal = ref(false);
|
||||||
const formParams = ref<State>();
|
const formParams = ref<State>();
|
||||||
const actionRef = ref();
|
const actionRef = ref();
|
||||||
const sortStatesRef = ref<Sorter[]>([]);
|
const { updateSorter: handleUpdateSorter, sortStatesRef: sortStatesRef } = useSorter(reloadTable);
|
||||||
|
|
||||||
const actionColumn = reactive({
|
const actionColumn = reactive({
|
||||||
width: 300,
|
width: 300,
|
||||||
@ -250,19 +250,6 @@
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleUpdateSorter(sorter: Sorter) {
|
|
||||||
const index = sortStatesRef.value.findIndex((item) => item.columnKey === sorter.columnKey);
|
|
||||||
|
|
||||||
if (index !== -1) {
|
|
||||||
sortStatesRef.value[index].sorter = sorter.sorter;
|
|
||||||
sortStatesRef.value[index].order = sorter.order;
|
|
||||||
} else {
|
|
||||||
sortStatesRef.value.push(sorter);
|
|
||||||
}
|
|
||||||
|
|
||||||
reloadTable();
|
|
||||||
}
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style lang="less" scoped></style>
|
<style lang="less" scoped></style>
|
||||||
|
@ -260,7 +260,7 @@ export const columns = [
|
|||||||
{
|
{
|
||||||
title: 'ID',
|
title: 'ID',
|
||||||
key: 'id',
|
key: 'id',
|
||||||
sorter: true,
|
sorter: true, // 单列排序
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: '标题',
|
title: '标题',
|
||||||
@ -268,7 +268,9 @@ export const columns = [
|
|||||||
render(row) {
|
render(row) {
|
||||||
return row.title;
|
return row.title;
|
||||||
},
|
},
|
||||||
sorter: true,
|
sorter: {
|
||||||
|
multiple: 1, // 为多列排序的优先级,越高优先级越高
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: '标签',
|
title: '标签',
|
||||||
@ -396,7 +398,9 @@ export const columns = [
|
|||||||
{
|
{
|
||||||
title: '价格',
|
title: '价格',
|
||||||
key: 'price',
|
key: 'price',
|
||||||
sorter: true,
|
sorter: {
|
||||||
|
multiple: 2, // 为多列排序的优先级,越高优先级越高
|
||||||
|
},
|
||||||
render(row) {
|
render(row) {
|
||||||
return h(
|
return h(
|
||||||
NTag,
|
NTag,
|
||||||
@ -487,7 +491,9 @@ export const columns = [
|
|||||||
render(row) {
|
render(row) {
|
||||||
return formatToDate(row.activityAt);
|
return formatToDate(row.activityAt);
|
||||||
},
|
},
|
||||||
sorter: true,
|
sorter: {
|
||||||
|
multiple: 3, // 为多列排序的优先级,越高优先级越高
|
||||||
|
},
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
|
5
web/types/table.d.ts
vendored
5
web/types/table.d.ts
vendored
@ -1,5 +0,0 @@
|
|||||||
export interface Sorter {
|
|
||||||
columnKey: string;
|
|
||||||
sorter: string | boolean;
|
|
||||||
order: string | boolean;
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user