Browse Source

列表排序实现

master
陈薪名 1 year ago
parent
commit
7111970105
  1. 17
      src/components/Table/src/Table.vue
  2. 19
      src/hooks/web/useTable.ts

17
src/components/Table/src/Table.vue

@ -49,7 +49,7 @@ export default defineComponent({
default: () => [] default: () => []
} }
}, },
emits: ['update:pageSize', 'update:currentPage', 'register'], emits: ['update:pageSize', 'update:currentPage', 'register', 'update:sort'],
setup(props, { attrs, slots, emit, expose }) { setup(props, { attrs, slots, emit, expose }) {
const elTableRef = ref<ComponentRef<typeof ElTable>>() const elTableRef = ref<ComponentRef<typeof ElTable>>()
@ -93,11 +93,17 @@ export default defineComponent({
} }
const selections = ref<Recordable[]>([]) const selections = ref<Recordable[]>([])
const sortRef = ref()
const selectionChange = (selection: Recordable[]) => { const selectionChange = (selection: Recordable[]) => {
selections.value = selection selections.value = selection
} }
//
const sortChange = (sortVal: Recordable) => {
sortRef.value = sortVal
}
expose({ expose({
setProps, setProps,
setColumn, setColumn,
@ -121,6 +127,13 @@ export default defineComponent({
) )
}) })
watch(
() => sortRef.value,
(val: any) => {
emit('update:sort', val)
}
)
watch( watch(
() => unref(getProps).pageSize, () => unref(getProps).pageSize,
(val: number) => { (val: number) => {
@ -250,6 +263,7 @@ export default defineComponent({
headerAlign={headerAlign} headerAlign={headerAlign}
{...props} {...props}
prop={v.field} prop={v.field}
sortable={v.sort}
> >
{{ {{
default: (data: TableSlotDefault) => default: (data: TableSlotDefault) =>
@ -276,6 +290,7 @@ export default defineComponent({
ref={elTableRef} ref={elTableRef}
data={unref(getProps).data} data={unref(getProps).data}
onSelection-change={selectionChange} onSelection-change={selectionChange}
onSort-change={sortChange}
{...unref(getBindValue)} {...unref(getBindValue)}
> >
{{ {{

19
src/hooks/web/useTable.ts

@ -32,6 +32,7 @@ interface TableObject<T = any> {
loading: boolean loading: boolean
exportLoading: boolean exportLoading: boolean
currentRow: Nullable<T> currentRow: Nullable<T>
sort: any
} }
export const useTable = <T = any>(config?: UseTableConfig<T>) => { export const useTable = <T = any>(config?: UseTableConfig<T>) => {
@ -53,14 +54,21 @@ export const useTable = <T = any>(config?: UseTableConfig<T>) => {
// 导出加载中 // 导出加载中
exportLoading: false, exportLoading: false,
// 当前行的数据 // 当前行的数据
currentRow: null currentRow: null,
// 排序
sort: {
order: '', // 排序规则
prop: '' // 排序字段
},
}) })
const paramsObj = computed(() => { const paramsObj = computed(() => {
return { return {
...tableObject.params, ...tableObject.params,
pageSize: tableObject.pageSize, pageSize: tableObject.pageSize,
pageNo: tableObject.currentPage pageNo: tableObject.currentPage,
sort: tableObject.sort.prop,
by: tableObject.sort.order == 'descending'?'DESC':'ASC'
} }
}) })
@ -84,6 +92,13 @@ export const useTable = <T = any>(config?: UseTableConfig<T>) => {
} }
) )
watch(
() => tableObject.sort,
() => {
methods.getList()
}
)
// Table实例 // Table实例
const tableRef = ref<typeof Table & TableExpose>() const tableRef = ref<typeof Table & TableExpose>()

Loading…
Cancel
Save