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,11 +65,15 @@ export default [
const { title, pageNo, pageSize } = data.query
let pageData = []
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 (pageSize) {
while (pageData.length < pageSize) {
pageData.push(filterData[Math.round(Math.random() * (filterData.length - 1))])
}
} else {
pageData = filterData
}
pageData = pageData.map((item, index) => ({
id: pageSize * (pageNo - 1) + index + 1,
...item,

View File

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

View File

@ -8,15 +8,16 @@
<CrudTable
ref="$table"
v-model:query-form="queryForm"
v-model:query-items="queryItems"
:extra-params="extraParams"
:scroll-x="1600"
:columns="columns"
:get-data="getTableData"
:get-data="api.getPosts"
@on-checked="onChecked"
>
<template #queryBar>
<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>
</template>
</CrudTable>
@ -32,22 +33,11 @@ import { renderIcon } from '@/utils/icon'
import api from './api'
const $table = ref(null)
const queryForm = ref({
title: '',
})
/** queryBar参数 */
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) {
if (rowKeys.length) $message.info(`选中${rowKeys.join(' ')}`)