Template
1
0
mirror of https://github.com/un-pany/v3-admin-vite.git synced 2025-04-21 11:29:20 +08:00

perf: 代码优化 views/table

This commit is contained in:
pany 2023-06-20 09:13:47 +08:00
parent d06985b8e6
commit d1c7480a4f
3 changed files with 63 additions and 88 deletions

View File

@ -7,6 +7,7 @@ import { Search, Refresh, CirclePlus, Delete, Download, RefreshRight } from "@el
import { usePagination } from "@/hooks/usePagination"
defineOptions({
//
name: "ElementPlus"
})
@ -25,29 +26,32 @@ const formRules: FormRules = reactive({
password: [{ required: true, trigger: "blur", message: "请输入密码" }]
})
const handleCreate = () => {
formRef.value?.validate((valid: boolean) => {
formRef.value?.validate((valid: boolean, fields) => {
if (valid) {
if (currentUpdateId.value === undefined) {
createTableDataApi({
username: formData.username,
password: formData.password
}).then(() => {
ElMessage.success("新增成功")
dialogVisible.value = false
getTableData()
})
createTableDataApi(formData)
.then(() => {
ElMessage.success("新增成功")
getTableData()
})
.finally(() => {
dialogVisible.value = false
})
} else {
updateTableDataApi({
id: currentUpdateId.value,
username: formData.username
}).then(() => {
ElMessage.success("修改成功")
dialogVisible.value = false
getTableData()
})
.then(() => {
ElMessage.success("修改成功")
getTableData()
})
.finally(() => {
dialogVisible.value = false
})
}
} else {
return false
console.error("表单校验不通过", fields)
}
})
}
@ -109,20 +113,11 @@ const getTableData = () => {
})
}
const handleSearch = () => {
if (paginationData.currentPage === 1) {
getTableData()
}
paginationData.currentPage = 1
paginationData.currentPage === 1 ? getTableData() : (paginationData.currentPage = 1)
}
const resetSearch = () => {
searchFormRef.value?.resetFields()
if (paginationData.currentPage === 1) {
getTableData()
}
paginationData.currentPage = 1
}
const handleRefresh = () => {
getTableData()
handleSearch()
}
//#endregion
@ -156,8 +151,8 @@ watch([() => paginationData.currentPage, () => paginationData.pageSize], getTabl
<el-tooltip content="下载">
<el-button type="primary" :icon="Download" circle />
</el-tooltip>
<el-tooltip content="刷新表格">
<el-button type="primary" :icon="RefreshRight" circle @click="handleRefresh" />
<el-tooltip content="刷新当前页">
<el-button type="primary" :icon="RefreshRight" circle @click="getTableData" />
</el-tooltip>
</div>
</div>

View File

@ -11,12 +11,11 @@ import {
type VxeModalInstance,
type VxeModalProps,
type VxeFormInstance,
type VxeFormProps,
type VxeGridPropTypes,
type VxeFormDefines
type VxeFormProps
} from "vxe-table"
defineOptions({
//
name: "VxeTable"
})
@ -136,26 +135,22 @@ const xGridOpt: VxeGridProps = reactive({
total: "total"
},
ajax: {
query: ({ page, form }: VxeGridPropTypes.ProxyAjaxQueryParams) => {
query: ({ page, form }) => {
xGridOpt.loading = true
crudStore.clearTable()
return new Promise<any>((resolve: Function) => {
return new Promise((resolve) => {
let total = 0
let result: RowMeta[] = []
/** 加载数据 */
const callback = (res: GetTableResponseData) => {
if (res && res.data) {
const resData = res.data
if (res?.data) {
//
if (Number.isInteger(resData.total)) {
total = resData.total
}
//
if (Array.isArray(resData.list)) {
result = resData.list
}
total = res.data.total
//
result = res.data.list
}
xGridOpt.loading = false
// vxe-table
resolve({ total, result })
}
@ -191,7 +186,7 @@ const xModalOpt: VxeModalProps = reactive({
//#region vxe-form
const xFormDom = ref<VxeFormInstance>()
const xFormOpt = reactive<VxeFormProps>({
const xFormOpt: VxeFormProps = reactive({
span: 24,
titleWidth: "100px",
loading: false,
@ -234,11 +229,11 @@ const xFormOpt = reactive<VxeFormProps>({
{
required: true,
validator: ({ itemValue }) => {
if (!itemValue) {
return new Error("请输入")
}
if (!itemValue.trim()) {
return new Error("空格无效")
switch (true) {
case !itemValue:
return new Error("请输入")
case !itemValue.trim():
return new Error("空格无效")
}
}
}
@ -247,11 +242,11 @@ const xFormOpt = reactive<VxeFormProps>({
{
required: true,
validator: ({ itemValue }) => {
if (!itemValue) {
return new Error("请输入")
}
if (!itemValue.trim()) {
return new Error("空格无效")
switch (true) {
case !itemValue:
return new Error("请输入")
case !itemValue.trim():
return new Error("空格无效")
}
}
}
@ -260,9 +255,9 @@ const xFormOpt = reactive<VxeFormProps>({
})
//#endregion
//#region CRUD
//#region
const crudStore = reactive({
/** 表单类型修改true 新增false */
/** 表单类型true 表示修改false 表示新增 */
isUpdate: true,
/** 加载表格数据 */
commitQuery: () => xGridDom.value?.commitProxy("query"),
@ -280,11 +275,8 @@ const crudStore = reactive({
xModalOpt.title = "新增用户"
}
//
if (xFormOpt.items) {
if (xFormOpt.items[0]?.itemRender?.props) {
xFormOpt.items[0].itemRender.props.disabled = crudStore.isUpdate
}
}
const props = xFormOpt.items?.[0]?.itemRender?.props
props && (props.disabled = crudStore.isUpdate)
xModalDom.value?.open()
nextTick(() => {
!crudStore.isUpdate && xFormDom.value?.reset()
@ -294,39 +286,38 @@ const crudStore = reactive({
/** 确定并保存 */
onSubmitForm: () => {
if (xFormOpt.loading) return
xFormDom.value?.validate((errMap?: VxeFormDefines.ValidateErrorMapParams) => {
xFormDom.value?.validate((errMap) => {
if (errMap) return
xFormOpt.loading = true
const callback = (err?: any) => {
const callback = () => {
xFormOpt.loading = false
if (err) return
xModalDom.value?.close()
ElMessage.success("操作成功")
!crudStore.isUpdate && crudStore.afterInsert()
crudStore.commitQuery()
}
if (crudStore.isUpdate) {
//
//
setTimeout(() => callback(), 1000)
} else {
//
//
setTimeout(() => callback(), 1000)
}
})
},
/** 新增后是否跳入最后一页 */
afterInsert: () => {
const pager: VxeGridPropTypes.ProxyAjaxQueryPageParams | undefined = xGridDom.value?.getProxyInfo()?.pager
const pager = xGridDom.value?.getProxyInfo()?.pager
if (pager) {
const currTotal: number = pager.currentPage * pager.pageSize
if (currTotal === pager.total) {
const currentTotal = pager.currentPage * pager.pageSize
if (currentTotal === pager.total) {
++pager.currentPage
}
}
},
/** 删除 */
onDelete: (row: RowMeta) => {
const tip = `确定 <strong style='color:red;'>删除</strong> 用户 <strong style='color:#409eff;'>${row.username}</strong> `
const tip = `确定 <strong style="color: red"> 删除 </strong> 用户 <strong style="color: #409eff"> ${row.username} </strong> `
const config: ElMessageBoxOptions = {
type: "warning",
showClose: true,
@ -336,28 +327,24 @@ const crudStore = reactive({
confirmButtonText: "确定",
dangerouslyUseHTMLString: true
}
ElMessageBox.confirm(tip, "提示", config)
.then(() => {
deleteTableDataApi(row.id)
.then(() => {
ElMessage.success("删除成功")
crudStore.afterDelete()
crudStore.commitQuery()
})
.catch(() => 1)
ElMessageBox.confirm(tip, "提示", config).then(() => {
deleteTableDataApi(row.id).then(() => {
ElMessage.success("删除成功")
crudStore.afterDelete()
crudStore.commitQuery()
})
.catch(() => 1)
})
},
/** 删除后是否返回上一页 */
afterDelete: () => {
const tableData: RowMeta[] = xGridDom.value!.getData()
const pager: VxeGridPropTypes.ProxyAjaxQueryPageParams | undefined = xGridDom.value?.getProxyInfo()?.pager
const pager = xGridDom.value?.getProxyInfo()?.pager
if (pager && pager.currentPage > 1 && tableData.length === 1) {
--pager.currentPage
}
},
/** 更多自定义方法 */
moreFunc: () => {}
moreFn: () => {}
})
//#endregion
</script>
@ -384,5 +371,3 @@ const crudStore = reactive({
</vxe-modal>
</div>
</template>
<style lang="scss" scoped></style>

View File

@ -3,12 +3,7 @@ import { type VxeColumnPropTypes } from "vxe-table/types/column"
const solts: VxeColumnPropTypes.Slots = {
default: ({ row, column }) => {
const cellValue = row[column.field]
let type = "danger"
let value = "禁用"
if (cellValue) {
type = "success"
value = "启用"
}
const [type, value] = cellValue ? ["success", "启用"] : ["danger", "禁用"]
return [<span class={`el-tag el-tag--${type} el-tag--plain`}>{value}</span>]
}
}