1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
|
import { ref } from 'vue';
import { Pagination, PaginationRes } from '@/types/global';
type GetListFunc = <T>(v: object) => Promise<T>; export default function useTbleProps(loadListFunc: GetListFunc) { const defaultProps = { 'row-key': 'id', 'bordered': { cell: true }, 'hover': true, 'border': true, 'stripe': true, 'size': 'small', 'column-resizable': true, // "scroll": { y: 550, x: '100%' }, 'expandable': false, 'loading': true, 'data': [] as any[], 'pagination': { current: 1, pageSize: 20, total: 0, showPageSize: true, } as Pagination, };
// 属性组 const propsRes = ref(defaultProps);
// 加载效果 const setLoading = (status: boolean) => { propsRes.value.loading = status; };
/** * 分页设置 * @param current //当前页数 * @param total //总页数默认是0条,可选 * @param fetchData 获取列表数据,可选 */ interface SetPaginationPrams { current: number; total?: number; }
const setPagination = ({ current, total }: SetPaginationPrams) => { propsRes.value.pagination.current = current; total && (propsRes.value.pagination.total = total); };
// 单独设置默认属性 const setProps = (params: object) => { for (const key in params) { defaultProps[key] = params[key]; } };
// 设置请求参数,如果出了分页参数还有搜索参数,在模板页面调用此方法,可以加入参数 const loadListParams = ref<object>({}); const setLoadListParams = (params?: object) => { loadListParams.value = params; };
// 加载分页列表数据 const loadList = async <T>() => { setLoading(true); let data = await loadListFunc<PaginationRes<T>>({ ...propsRes.value.pagination, ...loadListParams.value, }); propsRes.value.data = data.list as T[]; setPagination({ current: data.current, total: data.total }); setLoading(false); return data; };
// 事件触发组 const propsEvent = ref({ //排序触发 sorterChange: (dataIndex: string, direction: string) => { console.log(dataIndex, direction); }, //分页触发 pageChange: (current: number) => { setPagination({ current }); loadList(); }, // 修改每页显示条数 pageSizeChange:(pageSize:number)=>{ propsRes.value.pagination.pageSize = pageSize; loadList(); } });
return { propsRes, propsEvent, setProps, setLoading, loadList, setPagination, setLoadListParams, }; }
|