wip: crud table

This commit is contained in:
张传龙 2022-09-03 17:33:20 +08:00
parent 661aed1a94
commit d1dd58215d
3 changed files with 54 additions and 39 deletions

View File

@ -65,10 +65,14 @@ export default [
const { title, pageNo, pageSize } = data.query const { title, pageNo, pageSize } = data.query
let pageData = [] let pageData = []
let total = 60 let total = 60
const filterData = posts.filter((item) => item.title.includes(title)) const filterData = posts.filter((item) => item.title.includes(title) || (!title && title !== 0))
if (filterData.length) { if (filterData.length) {
while (pageData.length < pageSize) { if (pageSize) {
pageData.push(filterData[Math.round(Math.random() * (filterData.length - 1))]) while (pageData.length < pageSize) {
pageData.push(filterData[Math.round(Math.random() * (filterData.length - 1))])
}
} else {
pageData = filterData
} }
pageData = pageData.map((item, index) => ({ pageData = pageData.map((item, index) => ({
id: pageSize * (pageNo - 1) + index + 1, id: pageSize * (pageNo - 1) + index + 1,

View File

@ -44,31 +44,52 @@ const props = defineProps({
type: Array, type: Array,
required: true, required: true,
}, },
queryForm: { /** queryBar中的参数 */
queryItems: {
type: Object, type: Object,
required: true, default() {
return {}
},
}, },
/** 补充参数(可选) */
extraParams: {
type: Object,
default() {
return {}
},
},
/**
* ! 约定接口入参出参
* * 分页模式需约定分页接口入参
* @pageSize 分页参数一页展示多少条默认10
* @pageNo 分页参数页码默认1
* * 需约定接口出参
* @pageData 分页模式必须,非分页模式如果没有pageData则取上一层data
* @total 分页模式必须非分页模式如果没有total则取上一层data.length
*/
getData: { getData: {
type: Function, type: Function,
required: true, required: true,
}, },
}) })
const emit = defineEmits(['update:queryForm', 'onChecked']) const emit = defineEmits(['update:queryItems', 'onChecked'])
const loading = ref(false) const loading = ref(false)
const initQuery = { ...props.queryForm } const initQuery = { ...props.queryItems }
const tableData = ref([]) const tableData = ref([])
const pagination = reactive({ page: 1, pageSize: 10, itemCount: 100 }) const pagination = reactive({ page: 1, pageSize: 10 })
async function handleQuery(extraParams = {}) { async function handleQuery() {
try { try {
loading.value = true loading.value = true
const res = await props.getData( let paginationParams = {}
{ ...props.queryForm, ...extraParams }, // 使,
{ pageNo: pagination.page, pageSize: pagination.pageSize } if (props.isPagination && props.remote) {
) paginationParams = { pageNo: pagination.page, pageSize: pagination.pageSize }
tableData.value = res?.pageData || res }
pagination.itemCount = res.total ?? res.length const { data } = await props.getData({ ...props.queryItems, ...props.extraParams, ...paginationParams })
tableData.value = data?.pageData || data
pagination.itemCount = data.total ?? data.length
} catch (error) { } catch (error) {
tableData.value = [] tableData.value = []
pagination.itemCount = 0 pagination.itemCount = 0
@ -82,22 +103,22 @@ function handleSearch() {
handleQuery() handleQuery()
} }
async function handleReset() { async function handleReset() {
emit('update:queryForm', { ...initQuery }) emit('update:queryItems', { ...initQuery })
await nextTick() await nextTick()
pagination.page = 1 pagination.page = 1
handleQuery() handleQuery()
} }
function onChecked(rowKeys) {
if (props.columns.some((item) => item.type === 'selection')) {
emit('onChecked', rowKeys)
}
}
function onPageChange(currentPage) { function onPageChange(currentPage) {
pagination.page = currentPage pagination.page = currentPage
if (props.remote) { if (props.remote) {
handleQuery() handleQuery()
} }
} }
function onChecked(rowKeys) {
if (props.columns.some((item) => item.type === 'selection')) {
emit('onChecked', rowKeys)
}
}
defineExpose({ defineExpose({
handleSearch, handleSearch,

View File

@ -8,15 +8,16 @@
<CrudTable <CrudTable
ref="$table" ref="$table"
v-model:query-form="queryForm" v-model:query-items="queryItems"
:extra-params="extraParams"
:scroll-x="1600" :scroll-x="1600"
:columns="columns" :columns="columns"
:get-data="getTableData" :get-data="api.getPosts"
@on-checked="onChecked" @on-checked="onChecked"
> >
<template #queryBar> <template #queryBar>
<QueryBarItem label="标题" :label-width="50"> <QueryBarItem label="标题" :label-width="50">
<n-input v-model:value="queryForm.title" type="text" placeholder="请输入标题" /> <n-input v-model:value="queryItems.title" type="text" placeholder="请输入标题" />
</QueryBarItem> </QueryBarItem>
</template> </template>
</CrudTable> </CrudTable>
@ -32,22 +33,11 @@ import { renderIcon } from '@/utils/icon'
import api from './api' import api from './api'
const $table = ref(null) const $table = ref(null)
const queryForm = ref({ /** queryBar参数 */
title: '', const queryItems = ref({})
}) /** 可选,用于补充参数 */
const extraParams = ref({})
async function getTableData(query = {}, pagination = {}) {
const { pageSize = 10, pageNo = 1 } = pagination
try {
// * pagination
const res = await api.getPosts({ ...query, pageSize, pageNo })
if (res.code === 0) {
return Promise.resolve(res.data)
}
} catch (error) {
return Promise.reject(error)
}
}
// //
function onChecked(rowKeys) { function onChecked(rowKeys) {
if (rowKeys.length) $message.info(`选中${rowKeys.join(' ')}`) if (rowKeys.length) $message.info(`选中${rowKeys.join(' ')}`)