ProTable.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. <script lang="ts">
  2. export default {
  3. inheritAttrs: false
  4. }
  5. </script>
  6. <script setup lang="ts">
  7. import router from '@/router'
  8. import { ElMessage, ElMessageBox } from 'element-plus'
  9. import type { DialogProps, DrawerProps } from 'element-plus'
  10. import type { BasicForm, BasicFormItem, ICRUD, FormSlot } from '@/types/form'
  11. import type { VXEComponent, VxeToolbarProps, VxeToolbarEventProps, VxeToolbarPropTypes } from 'vxe-table'
  12. import { buildFormSlots } from '@/utils/utils'
  13. interface CustomTable {
  14. showOperate?: boolean
  15. showEdit?: boolean
  16. showView?: boolean
  17. showDelete?: boolean
  18. operateWidth?: number
  19. }
  20. interface Props {
  21. crud: ICRUD
  22. pageSize?: number
  23. selection?: boolean
  24. formConfig: BasicForm
  25. dialogConfig?: Partial<DialogProps>
  26. drawerConfig?: Partial<DrawerProps>
  27. tableConfig?: CustomTable
  28. toolbarConfig?: VxeToolbarProps
  29. showToolbar?: boolean
  30. height?: string
  31. formMode?: 'dialog' | 'drawer'
  32. beforeCreate?: Function
  33. }
  34. const props = withDefaults(defineProps<Props>(), {
  35. pageSize: 10,
  36. selection: true,
  37. showToolbar: true,
  38. formMode: 'dialog'
  39. })
  40. const emits = defineEmits(['click-create', 'click-edit', 'click-view', 'click-reset', 'checkbox-change'])
  41. const slots = useSlots()
  42. // ============== 查询部分开始 ===============
  43. const query = ref<any>({})
  44. const defaultQuery = ref<any>({})
  45. const searchList = ref<any>([])
  46. // 构造搜索列表
  47. const buildSearchList = (item: BasicFormItem) => {
  48. if (item.search) {
  49. searchList.value.push(Object.assign({}, item, { props: { ...item.props, disabled: false } }))
  50. if (item.value) {
  51. query.value[item.name] = item.value
  52. defaultQuery.value[item.name] = item.value
  53. }
  54. }
  55. item.children && item.children.forEach(buildSearchList)
  56. }
  57. watch(
  58. () => props.formConfig.formItems,
  59. val => {
  60. searchList.value = []
  61. val.forEach((item: BasicFormItem) => {
  62. buildSearchList(item)
  63. })
  64. },
  65. { immediate: true }
  66. )
  67. const handleQuery = () => {
  68. curPage.value = 1
  69. getTableData()
  70. }
  71. const handleReset = () => {
  72. query.value = { ...defaultQuery.value }
  73. emits('click-reset', query.value)
  74. handleQuery()
  75. }
  76. // ============== 查询部分结束 ===============
  77. // ============== 表格部分开始 ===============
  78. const tableData = ref([])
  79. const total = ref(0)
  80. const curPage = ref(1)
  81. const loading = ref(false)
  82. const tableConfig = computed<CustomTable>(() => ({
  83. showOperate: true,
  84. showView: false,
  85. showEdit: true,
  86. showDelete: true,
  87. operateWidth: 140,
  88. ...props.tableConfig
  89. }))
  90. const xTable = ref<any>()
  91. const xToolbar = ref<any>()
  92. nextTick(() => {
  93. // 将表格和工具栏进行关联
  94. const $table = xTable.value
  95. const $toolbar = xToolbar.value
  96. $toolbar && $table.connect($toolbar)
  97. })
  98. const getTableData = () => {
  99. loading.value = true
  100. props.crud
  101. ?.getList({
  102. ...query.value,
  103. pageSize: props.pageSize,
  104. pageNo: curPage.value
  105. })
  106. .then((res: any) => {
  107. tableData.value = res.list || res.rows || res.infos
  108. total.value = res.total || res.totalCount
  109. })
  110. .finally(() => {
  111. loading.value = false
  112. })
  113. }
  114. watch(
  115. curPage,
  116. () => {
  117. getTableData()
  118. },
  119. {
  120. immediate: true
  121. }
  122. )
  123. const refresh = () => {
  124. curPage.value = 1
  125. getTableData()
  126. }
  127. const multipleSelection = ref<any[]>([])
  128. const handleSelectionChange = () => {
  129. multipleSelection.value = xTable.value.getCheckboxRecords()
  130. emits('checkbox-change', multipleSelection.value)
  131. }
  132. // ============== 表格部分结束 ===============
  133. // ============== crud部分开始 ===============
  134. const formRoute = ref<any>(props.formConfig.route)
  135. const doCreate = () => {
  136. emits('click-create')
  137. if (formRoute.value) {
  138. router.push(formRoute.value)
  139. } else {
  140. formData.value = {}
  141. props.formConfig.disabled = false
  142. formVisible.value = true
  143. }
  144. }
  145. const handleCreate = async () => {
  146. if (!props.beforeCreate) {
  147. return doCreate()
  148. }
  149. let result = true
  150. try {
  151. const beforeUploadPromise = props.beforeCreate(formData.value)
  152. result = await beforeUploadPromise
  153. } catch {
  154. result = false
  155. }
  156. if (result === false) {
  157. return
  158. }
  159. doCreate()
  160. }
  161. const handleUpdate = (row: any) => {
  162. emits('click-edit', row)
  163. if (formRoute.value) {
  164. router.push(formRoute.value)
  165. } else {
  166. if (props.crud?.getRecord) {
  167. props.crud.getRecord({ id: row.id }).then((res: any) => {
  168. formData.value = res.data
  169. })
  170. } else {
  171. formData.value = { ...row }
  172. }
  173. props.formConfig.disabled = false
  174. formVisible.value = true
  175. }
  176. }
  177. const handleView = (row: any) => {
  178. emits('click-view', row)
  179. if (formRoute.value) {
  180. router.push(formRoute.value)
  181. } else {
  182. if (props.crud?.getRecord) {
  183. props.crud.getRecord({ id: row.id }).then((res: any) => {
  184. formData.value = res.data
  185. })
  186. } else {
  187. formData.value = { ...row }
  188. }
  189. props.formConfig.disabled = true
  190. formVisible.value = true
  191. }
  192. }
  193. const handleDelete = (id: string | number) => {
  194. ElMessageBox.confirm('您确定要删除该项吗', '提示', {
  195. type: 'warning'
  196. }).then(async () => {
  197. const data = await props.crud?.delete({ id })
  198. if (data.success || data.code === 200) {
  199. getTableData()
  200. ElMessage({
  201. type: 'success',
  202. message: '删除成功'
  203. })
  204. } else {
  205. ElMessage.error(data.msg)
  206. }
  207. })
  208. }
  209. const handleBatchDelete = () => {
  210. ElMessageBox.confirm('您确定要删除吗', '提示', {
  211. type: 'warning'
  212. }).then(async () => {
  213. if (props.crud.deleteBatch) {
  214. const data = await props.crud?.deleteBatch({
  215. ids: multipleSelection.value.map(item => item.id).join(',')
  216. })
  217. if (data.success || data.code === 200) {
  218. getTableData()
  219. ElMessage({
  220. type: 'success',
  221. message: '删除成功'
  222. })
  223. } else {
  224. ElMessage.error(data.msg)
  225. }
  226. } else {
  227. ElMessage({
  228. type: 'error',
  229. message: '未提供deleteBatch方法'
  230. })
  231. }
  232. })
  233. }
  234. // ============== crud部分结束 ===============
  235. // ============== 表单部分开始 ===============
  236. const formData = ref<any>({})
  237. const formVisible = ref(false)
  238. const handleFormSuccess = () => {
  239. getTableData()
  240. }
  241. // 构造表单插槽
  242. const formSlots = ref<FormSlot[]>([])
  243. buildFormSlots(props.formConfig.formItems, formSlots.value)
  244. // ============== 表单部分结束 ===============
  245. defineExpose({
  246. handleCreate,
  247. handleDelete,
  248. handleUpdate,
  249. handleView,
  250. handleQuery,
  251. handleReset,
  252. refresh,
  253. formData,
  254. query,
  255. table: xTable
  256. })
  257. </script>
  258. <template>
  259. <div class="flex flex-col" style="max-height: 100vh" :style="{ height: height || '100%' }">
  260. <el-card class="mb-4" shadow="never" v-if="searchList.length || slots.query">
  261. <el-form :inline="true">
  262. <el-form-item :label="item.label" v-for="(item, index) in searchList" :key="index">
  263. <form-comp :item="item" v-model="query[item.name]">
  264. <template #[slot.alias] v-for="slot in item.slots" :key="slot.alias">
  265. <slot :name="slot.alias"></slot>
  266. </template>
  267. </form-comp>
  268. </el-form-item>
  269. <slot name="query" :query="query"></slot>
  270. <el-form-item>
  271. <el-button type="primary" icon="Search" @click="handleQuery">查询</el-button>
  272. <el-button icon="Refresh" @click="handleReset">重置</el-button>
  273. </el-form-item>
  274. </el-form>
  275. </el-card>
  276. <el-card class="h-full flex-grow-1" :body-style="{ height: '100%' }" shadow="never">
  277. <div class="flex flex-col h-full">
  278. <slot name="header"></slot>
  279. <vxe-toolbar ref="xToolbar" v-bind="toolbarConfig" v-if="showToolbar">
  280. <template #buttons>
  281. <el-button type="primary" icon="Plus" @click="handleCreate">新增</el-button>
  282. <el-button
  283. type="danger"
  284. icon="Delete"
  285. @click="handleBatchDelete"
  286. :disabled="!multipleSelection.length"
  287. v-if="selection"
  288. >
  289. 删除
  290. </el-button>
  291. <slot name="toolbar" :selection="multipleSelection"></slot>
  292. </template>
  293. </vxe-toolbar>
  294. <div class="h-full flex-grow">
  295. <vxe-table
  296. ref="xTable"
  297. id="xProTable"
  298. size="medium"
  299. height="100%"
  300. :data="tableData"
  301. :row-config="{ isHover: true }"
  302. v-loading="loading"
  303. v-bind="$attrs"
  304. @checkbox-change="handleSelectionChange"
  305. @checkbox-all="handleSelectionChange"
  306. >
  307. <vxe-column type="checkbox" width="50" v-if="selection"></vxe-column>
  308. <slot></slot>
  309. <vxe-column fixed="right" title="操作" :width="tableConfig.operateWidth" v-if="tableConfig.showOperate">
  310. <template #default="{ row }">
  311. <slot name="operateBefore" :row="row"></slot>
  312. <el-button type="success" size="small" @click="handleView(row)" v-if="tableConfig.showView">
  313. 查看
  314. </el-button>
  315. <el-button type="primary" size="small" @click="handleUpdate(row)" v-if="tableConfig.showEdit">
  316. 编辑
  317. </el-button>
  318. <el-button type="danger" size="small" @click="handleDelete(row.id)" v-if="tableConfig.showDelete">
  319. 删除
  320. </el-button>
  321. <slot name="operateAfter" :row="row"></slot>
  322. </template>
  323. </vxe-column>
  324. </vxe-table>
  325. </div>
  326. <div class="flex justify-end shrink-0">
  327. <el-pagination
  328. background
  329. layout="prev, pager, next, jumper, total"
  330. v-model:current-page="curPage"
  331. :page-size="pageSize"
  332. :total="total"
  333. class="mt-16px"
  334. />
  335. </div>
  336. </div>
  337. </el-card>
  338. <template v-if="formMode === 'dialog'">
  339. <dialog-form
  340. v-model="formVisible"
  341. v-if="formVisible"
  342. :dialogConfig="dialogConfig"
  343. :formConfig="formConfig"
  344. :formData="formData"
  345. :formSlots="formSlots"
  346. :create="crud.create"
  347. :update="crud.update"
  348. @success="handleFormSuccess"
  349. >
  350. <template #[slot.alias]="slotProps" v-for="slot in formSlots" :key="slot.alias">
  351. <slot :name="slot.alias" v-bind="slotProps"></slot>
  352. </template>
  353. </dialog-form>
  354. </template>
  355. <template v-else>
  356. <drawer-form
  357. v-model="formVisible"
  358. v-if="formVisible"
  359. :drawerConfig="drawerConfig"
  360. :formConfig="formConfig"
  361. :formData="formData"
  362. :formSlots="formSlots"
  363. :create="crud.create"
  364. :update="crud.update"
  365. @success="handleFormSuccess"
  366. >
  367. <template #[slot.alias]="slotProps" v-for="slot in formSlots" :key="slot.alias">
  368. <slot :name="slot.alias" v-bind="slotProps"></slot>
  369. </template>
  370. </drawer-form>
  371. </template>
  372. </div>
  373. </template>
  374. <style scoped>
  375. .vxe-toolbar {
  376. padding-top: 0;
  377. }
  378. :deep(.el-card) {
  379. --el-card-padding: 16px;
  380. border: none !important;
  381. }
  382. </style>