import { FieldProps } from '../../composites/Form'; import { TableColumnCtx as ElTableColumns, TableInstance, TableProps } from 'element-plus'; import { Component, VNode } from 'vue'; import { UseQueryOptions } from 'zan-mixin-query'; type ReqParams = { /** * 当前页 */ pageNo: number; /** * 页大小 */ pageSize: number; /** * 排序字段 */ sort?: { prop: string; order?: null | 'ascending' | 'ascending'; }[]; [key: string]: any; }; type PaginationRes = { records: T[]; total: number; }; type ResData = T[] | PaginationRes | ({ body: T[]; }) | { body: PaginationRes; } | ({ data: T[]; }) | { data: PaginationRes; }; /** * 单元格数据 */ export type CellScope = { row: T; $index: number; index: number; column?: TableV2Column; cellValue: any; }; /** * 列配置 */ export interface TableV2Column extends Omit>, 'renderCell' | 'sortable' | 'sort-method' | 'sort-by' | 'sort-orders'> { /** * 是否支持排序 */ sortable?: boolean; /** * header 提示 */ tooltip?: string; /** * 自定义渲染单元格 * @param scope */ renderCell?: (props: CellScope) => VNode; /** * 可编辑单元格 */ editable?: Editable; } type Editable = { /** * 实现了v-model受控属性的组件 */ component: string | Component; /** * 组件的props */ componentProps?: any; /** * 值变动是触发 * @param value */ onChange?: (value: any, cellScope: CellScope) => void; /** * component组件默认值(默认传递callValue) */ defaultValue?: any | ((cellScope: CellScope) => any); /** * 确定按钮回调 * @param value */ handleConfirm?: (value: any, cellScope: CellScope) => Promise | void; /** * 表单字段配置项目 */ fieldProps?: Omit; /** * popover 宽度 * @default {260} */ popoverWidth?: number; }; export type Sort = { prop: string; order?: null | 'ascending' | 'ascending'; }[]; export interface TableV2Props extends Omit, 'data' | 'pageSizes' | 'defaultSort'> { /** * 当前页 (受控属性) */ page?: number; /** * 页大小 (受控属性) */ pageSize?: number; /** * 排序 (受控属性) */ sort?: Sort; /** * 选中的项目 (受控属性属性) * @type {Key|Key[]} */ select?: number | string | (number | string)[]; /** * 表格前置插槽 class */ beforeWrapperClass?: any; /** * 数据源(数据展示模式),需要手动管理分页状态(page, pageSize,total|hasNextPage) */ data?: T[]; /** * 数据展示模式时, default分页的总页数 */ total?: number; /** * 数据展示模式时, infinite分页的是否到底 */ hasNextPage?: boolean; /** * 列配置 */ columns: TableV2Column[]; /** * loading 状态,service模式时(传入queryKey, queryFn), 会自动管理 loading 状态 */ loading?: boolean; /** * service模式时,queryFn的请求参数,变动时会重新请求,传递给ReqParams时会默认进行解构 */ filterParams?: any; /** * 表格选择模式, 必传rowKey,事件selectChange的返回值与rowKey挂钩 */ mode?: 'select' | 'radio'; /** * 默认选中项 */ defaultSelect?: (string | number) | (string | number)[]; /** * - false: 关闭分页功能, pagination-before也将失效 * - 'infinite': service模式,无限分页 * - 'default': service模式,普通分页, queryFn 需要返回总页数,或者手动管理 total * - 'interior': 数据模式,内部分页 */ paginationMode?: false | 'default' | 'infinite' | 'interior'; /** * 分页大小选项 */ pageSizes?: number[]; /** * service模式参数(也可以从queryFn.queryKey上取) */ queryKey?: string; /** * 是否启用查询 * @default {true} */ enabled?: boolean; /** * service模式参数, 返回数据体支持结构 * - T[] * - {body: T[]} * - {records: T[], total: number} * - {body: {records: T[], total: number}} * @param params {ReqParams} 分页数据,排序数据,过滤参数 * @param params.pageNo {number} 当前页 * @param params.pageSize {number} 当前页大小 * @param params.sort {{ prop: string, order?: null | 'ascending' | 'ascending' }[]} 排序字段,受querySortFormatter, spreadSortParam影响 * @return {ResData} */ queryFn?: ((params: ReqParams) => ResData | Promise>); /** * query 剩余配置项 */ queryOptions?: Omit; /** * 是否可以勾选, 也会取row._selectable属性 */ selectable?: (row: T, index: number, selectValue: any[]) => boolean; /** * 是否开启多字段排序功能, 单字段模式时回会默认解构,注意:解构出的字段被filterParams字段覆盖 */ multipleSort?: boolean; /** * 自定义query请求参数的sort字段转换规则 * @param sort */ querySortFormatter?: (sort: Sort) => any; /** * sort字段传递给ReqParams时会默认进行解构, multipleSort=false时默认解构(ReqParams没有sort字段),同时也会将querySortFormatter返回值进行解构,注意:解构出的字段被filterParams字段覆盖 */ spreadSortParam?: boolean | null; /** * 索引列的表头 */ indexLabel?: string; /** * 索引列 */ index?: boolean | number | ((index: number) => number); /** * 展开列 */ renderExpand?: (props: CellScope) => VNode; } export interface TableV2Emits { /** * 选中事件,mode?: 'select' | 'radio' * - mode='select': (value: key[],selectData: row[]) * - mode='radio': (value: key,selectData: row) */ (e: 'selectChange', value: any, selectData: any): void; /** * 单元格编辑完成事件 * @param e * @param value {any} 编辑的值 * @param cellScope {CellScope} 单元格数据 */ (e: 'tableEditable', value: any, cellScope: CellScope): void; } export interface TableV2Exposes extends Pick { /** * 获取表格数据 */ getData: () => T[] | undefined; /** * Server 模式时刷新表格 * @param resetPage {boolean = false} 是否将page重置为1 */ refresh: (resetPage?: boolean) => Promise; /** * 全选 */ selectionAll: () => void; /** * 多选模式切换全选全不选 */ toggleAllSelection: () => void; /** * 选择指定项 */ selection: (ids?: number | string | (number | string)[]) => void; /** * 获取选中的行 */ getSelectionRows: () => any[]; /** * 清空选中 */ clearSelection: () => void; } export type TableV2Slots = { /** * Table before */ 'before': (props: { select: any; tableInstance: TableV2Exposes; }) => VNode; /** * 自定义分页 */ 'pagination': () => VNode; /** * 分页前置插槽 一般放置批量操作按钮 */ 'pagination-before': (props: { select: any; tableInstance: TableV2Exposes; }) => VNode; /** * expand 展开列插槽 */ 'expand': (props: CellScope) => VNode; /** * 尾部插槽 * @param props */ 'append': (props: { select: any; tableInstance: TableV2Exposes; }) => VNode; /** * 空状态 */ 'empty': (props: { isLoading: boolean; emptyText?: string; }) => VNode; }; export {};