| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- <script lang="ts">
- export default {
- inheritAttrs: false
- }
- </script>
- <script setup lang="ts">
- import router from '@/router'
- import { ElMessage, ElMessageBox, type DialogProps } from 'element-plus'
- import type { AdvancedForm, BasicForm } from '@/types/form'
- interface CRUD {
- create: Function
- update: Function
- delete: Function
- deleteBatch: Function
- getList: Function
- getRecord: Function
- }
- interface CustomTable {
- showOperate?: boolean
- operateWidth?: number
- }
- interface Props {
- crud: CRUD
- pageSize?: number
- selection?: boolean
- formConfig: BasicForm | AdvancedForm
- dialogConfig?: DialogProps
- tableConfig?: CustomTable
- showToolbar?: boolean
- }
- const props = withDefaults(defineProps<Props>(), {
- pageSize: 10,
- selection: true,
- showToolbar: true
- })
- // ============== 查询部分开始 ===============
- const query = ref<any>({})
- const searchList = ref<any>([])
- watch(
- () => props.formConfig.formItems,
- val => {
- val.forEach((item: any) => {
- if (item.group) {
- searchList.value = searchList.value.concat(item.group.filter((item: any) => item.search))
- } else {
- item.search && searchList.value.push(item)
- }
- })
- },
- { immediate: true }
- )
- const handleQuery = () => {
- curPage.value = 1
- getTableData()
- }
- const handleReset = () => {
- query.value = {}
- }
- // ============== 查询部分结束 ===============
- // ============== 表格部分开始 ===============
- const tableData = ref([])
- const total = ref(0)
- const curPage = ref(1)
- const loading = ref(false)
- const tableConfig = ref<CustomTable>({
- showOperate: true,
- operateWidth: 120,
- ...props.tableConfig
- })
- const getTableData = () => {
- loading.value = true
- props.crud
- ?.getList({
- ...query.value,
- pageSize: props.pageSize,
- pageNo: curPage.value
- })
- .then((res: any) => {
- tableData.value = res.data
- total.value = res.total
- })
- .finally(() => {
- loading.value = false
- })
- }
- getTableData()
- const pageChange = (val: number) => {
- curPage.value = val
- getTableData()
- }
- const refresh = () => {
- curPage.value = 1
- getTableData()
- }
- const multipleSelection = ref<any[]>([])
- const handleSelectionChange = (columns: any[]) => {
- multipleSelection.value = columns
- }
- // ============== 表格部分结束 ===============
- // ============== crud部分开始 ===============
- const formRoute = ref<any>(props.formConfig.route)
- const handleCreate = () => {
- if (formRoute.value) {
- router.push(formRoute.value)
- } else {
- formData.value = {}
- dialogVisible.value = true
- }
- }
- const handleUpdate = (row: any) => {
- if (formRoute.value) {
- router.push(formRoute.value)
- } else {
- if (props.crud?.getRecord) {
- props.crud.getRecord({ id: row.id }).then((res: any) => {
- formData.value = res.data
- })
- } else {
- formData.value = { ...row }
- }
- dialogVisible.value = true
- }
- }
- const handleDelete = (id: string | number) => {
- ElMessageBox.confirm('您确定要删除该项吗', '提示', {
- type: 'warning'
- }).then(async () => {
- await props.crud?.delete({ id })
- getTableData()
- ElMessage({
- type: 'success',
- message: '删除成功'
- })
- })
- }
- const handleBatchDelete = () => {
- ElMessageBox.confirm('您确定要删除吗', '提示', {
- type: 'warning'
- }).then(async () => {
- await props.crud?.deleteBatch({
- ids: multipleSelection.value.map(item => item.id).join(',')
- })
- getTableData()
- ElMessage({
- type: 'success',
- message: '删除成功'
- })
- })
- }
- // ============== crud部分结束 ===============
- // ============== 表单部分开始 ===============
- const formData = ref<any>({})
- const dialogVisible = ref(false)
- const handleFormSuccess = () => {
- getTableData()
- }
- // ============== 表单部分结束 ===============
- defineExpose({
- handleCreate,
- handleDelete,
- handleUpdate,
- refresh
- })
- </script>
- <template>
- <div class="flex flex-col" style="height: calc(100vh - 101px - var(--main-padding) * 2)">
- <el-card class="mb-4" shadow="never" v-if="searchList.length">
- <el-form :inline="true">
- <el-form-item :label="item.label" v-for="(item, index) in searchList" :key="index">
- <form-comp :item="item" v-model="query[item.name]"></form-comp>
- </el-form-item>
- <slot name="query" :query="query"></slot>
- <el-form-item>
- <el-button type="primary" icon="Search" @click="handleQuery">查询</el-button>
- <el-button icon="Refresh" @click="handleReset">重置</el-button>
- </el-form-item>
- </el-form>
- </el-card>
- <el-card class="h-full flex-grow-1" :body-style="{ height: '100%' }" shadow="never">
- <div class="flex flex-col h-full">
- <slot name="header"></slot>
- <div class="flex justify-between mb-20px" v-if="showToolbar">
- <div>
- <el-button type="primary" icon="Plus" @click="handleCreate">新增</el-button>
- <el-button
- type="danger"
- icon="Delete"
- @click="handleBatchDelete"
- :disabled="!multipleSelection.length"
- v-if="selection"
- >
- 删除
- </el-button>
- <slot name="toolbar"></slot>
- </div>
- </div>
- <el-table
- class="flex-grow-1 h-full"
- :data="tableData"
- v-loading="loading"
- v-bind="$attrs"
- @selection-change="handleSelectionChange"
- >
- <el-table-column type="selection" width="50" v-if="selection"></el-table-column>
- <slot></slot>
- <el-table-column fixed="right" label="操作" :width="tableConfig.operateWidth" v-if="tableConfig.showOperate">
- <template #default="{ row }">
- <slot name="operateBefore" :row="row"></slot>
- <el-button link type="primary" size="small" @click="handleUpdate(row)">编辑</el-button>
- <el-button link type="danger" size="small" @click="handleDelete(row.id)">删除</el-button>
- <slot name="operateAfter" :row="row"></slot>
- </template>
- </el-table-column>
- </el-table>
- <div class="flex justify-end shrink-0">
- <el-pagination
- background
- layout="prev, pager, next, jumper, total"
- :page-size="pageSize"
- :total="total"
- @current-change="pageChange"
- class="mt-16px"
- />
- </div>
- </div>
- </el-card>
- <dialog-form
- v-model="dialogVisible"
- :dialogConfig="dialogConfig"
- :formConfig="formConfig"
- :formData="formData"
- :create="crud.create"
- :update="crud.update"
- @success="handleFormSuccess"
- v-if="dialogVisible"
- />
- </div>
- </template>
- <style scoped>
- :deep(.el-table th.el-table__cell) {
- background-color: #f5f7fa;
- color: rgb(31, 34, 37);
- }
- </style>
|