ProTable.vue 9.8 KB

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