Skip to content

useTable(推荐使用)

常规实例

TIP

若使用自动按需导入则直接使用useEyTable,无需导入

ts
import {useTable} from 'ey-admin'
const { list, listLoading, page, getTableList, tableChange } = useTable({
  api:promiseApi,// 传入对应的promise接口 页面变化会调用promiseApi并默认传入page、page_size
});
// 手动请求
getTableList()

api默认处理入参为:page、page_size。可按需修改 返回值默认如下,也可按需修改

ts
  data: {
    list: [...],
    otherData: {
      total: 4,
    }
  }
ts
import {useTable} from 'ey-admin'
const { list, listLoading, page, getTableList, tableChange } = useTable({
  api:promiseApi,
  formatHandle(res){
    return res.data.youSelfList//返回值即为list最终值
  },
  totalKey:'data.pageData.total',// 修改为后端对应返回值的total取值
  immediately:true
});

泛型

userTable<T,K>T:list的类型将为list: Ref<T[]> K:searchForm即搜索字段

API

Attributes

成员说明类型是否必传默认值
api传入的 api 方法(searchForm: K) => Promise<AxiosResponse<any> | any>-
pagination分页信息PaginationProps 同ant-design-vue-
searchForm搜索参数K-
pageKey搜索时页码字段名string"page"
pageSizeKey搜索时分页大小字段名string"page_size"
totalKey返回数据的总数字段路径string"data.otherData.total"
formatHandle自定义返回数据处理函数(res: AxiosResponse<any>) => T[] | Promise<T[]>-
setSortFielda-table @change 时设置 sorter(sorter: unknown) => void-
formatSearchValue自定义调整 searchForm 入参(searchForm?: K) => K-
formatTableSearchValue自定义修改 page,返回值覆盖 page、page_size 入参(page: PaginationProps) => Record<string, any>-
immediate是否立即搜索boolean-

返回值

成员说明类型
list列表数据Ref<T[]>
listLoadingloading 状态Ref<boolean>
page用于 ant-design-vue 的 paginationPaginationProps
getTableList手动发起请求 —— search 即点击搜索,page 重置(type?: 'search') => any
tableChange同 ant-design-vue 的 change 事件(pagination: PaginationProps, _?: any, sorter?: unknown, source?: any, immediately?: boolean) => void
resetPagination页码重置() => void

一个让开发效率增倍的组件库