目录
1、useTableData
2、util
3、使用
开箱即用,理解容易。
1、useTableData
import { reactive } from 'vue'
import { getType } from '@/utils/utils'
export const useTableData = (api) => {
if (typeof api !== 'function' && getType(api) !== 'Promise') {
throw new Error('请传入Promise类型的api请求')
}
// 分页器每页个数配置组
const PAGE_SIZES = reactive([10, 20, 50])
// 初始化数据
const DEFAULT_TABLE_DATA = reactive({
list: [],
totalCount: 0,
isLoading: false,
currentPage: 1,
pageSize: PAGE_SIZES[0]
})
// 数据表格
const tableData = reactive({
list: DEFAULT_TABLE_DATA.list,
totalCount: DEFAULT_TABLE_DATA.totalCount,
isLoading: DEFAULT_TABLE_DATA.isLoading
})
// 数据表格基本请求参数
let tableParams = reactive({
currentPage: DEFAULT_TABLE_DATA.currentPage,
pageSize: DEFAULT_TABLE_DATA.pageSize
})
// 获取数据
const fetchData = async () => {
tableData.isLoading = true
const res = await api(tableParams).catch(() => {
tableData.isLoading = false
})
if (res) {
console.log(api.toString() + '请求成功\n', res)
tableData.isLoading = false
tableData.list = res.list
tableData.totalCount = res.totalCount
} else {
ElMessage.error('数据获取失败!')
}
}
// 重置请求
const reset = () => {
tableData.list = DEFAULT_TABLE_DATA.list
tableData.totalCount = DEFAULT_TABLE_DATA.totalCount
tableData.isLoading = DEFAULT_TABLE_DATA.isLoading
tableParams = {
currentPage: DEFAULT_TABLE_DATA.currentPage,
pageSize: DEFAULT_TABLE_DATA.pageSize
}
fetchData()
}
// 刷新
const refresh = () => {
fetchData()
}
// 设置查询参数
const setParams = (params = {}) => {
tableParams = { ...tableParams, ...params }
fetchData()
}
return {
PAGE_SIZES,
tableData,
tableParams,
setParams,
reset,
refresh
}
}
2、util
export const getType = (value) => {
let type = Object.prototype.toString.call(value)
type = type.replace(/^\[object\s/, '')
type = type.replace(/$/, '')
return type
}
3、使用
const roleManagementTable =
useTableData(http.roleManagement.list)
<el-table
:data="roleManagementTable.tableData.list"
tooltip-effect="dark"
:height="windowRect.clientHeight - 250"
:row-style="{ height: '65px' }"
@selection-change="onTableSelectionChange"
>
list即为一个axios请求;
然后将分页工具和分页参数进行绑定即可。
<el-pagination
@size-change="onSizeChange"
@current-change="onCurrentChange"
v-model:page-size="roleManagementTable.tableParams.pageSize"
v-model:current-page="roleManagementTable.tableParams.currentPage"
:page-sizes="roleManagementTable.PAGE_SIZES"
:layout="`total,prev,pager,next,${device === 'mobile' ? '' : 'sizes'}`"
:total="roleManagementTable.tableData.totalCount"
:small="device === 'mobile'"
>
</el-pagination>
如果对你有帮助,点赞、收藏、关注是我更新的动力!
#14vue3生成表单并跳转到外部地址的方式-CSDN博客文章浏览阅读2.3k次,点赞74次,收藏13次。vue3生成表单并跳转到外部地址的方式https://blog.csdn.net/weixin_42718399/article/details/136538137?spm=1001.2014.3001.5502#13Maven打包生成MD5校验文件的两种方式_mvn 打war包,显示md5和大小-CSDN博客文章浏览阅读2.1k次,点赞59次,收藏17次。Maven打包生成MD5校验文件的两种方式_mvn 打war包,显示md5和大小https://blog.csdn.net/weixin_42718399/article/details/136375811?spm=1001.2014.3001.5502#12解决request中getReader()和getInputStream()只能调用一次的问题_servletrequest.getreader只允许调一次-CSDN博客文章浏览阅读2.1k次,点赞64次,收藏30次。getInputStream() has already been called for this request,解决request中getReader()和getInputStream()只能调用一次的问题_servletrequest.getreader只允许调一次https://blog.csdn.net/weixin_42718399/article/details/136237084?spm=1001.2014.3001.5502#3Jenkins(Windows环境)版本升级、迁移、负载均衡、双机器同步与备份_windows下的jenkins的job备份-CSDN博客文章浏览阅读1.1k次,点赞28次,收藏17次。Jenkins(Windows环境)版本升级、迁移、负载均衡、双机器同步与备份_windows下的jenkins的job备份https://blog.csdn.net/weixin_42718399/article/details/135404525?spm=1001.2014.3001.5502#2Vite+Vue3+SpringMVC前后端分离 解决跨域问题和session每次请求不一致问题_vue3 每次请求sessionid都不一样-CSDN博客文章浏览阅读1.1k次,点赞37次,收藏16次。Vite+Vue3+SpringMVC前后端分离通过vite/nginx解决跨域问题和session一致性问题_vue3 每次请求sessionid都不一样https://blog.csdn.net/weixin_42718399/article/details/135388463?spm=1001.2014.3001.5502