ProCardList.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 { AdvancedForm, BasicForm, ICRUD } from '@/types/form'
  10. interface Props {
  11. crud: ICRUD
  12. pageSize?: number
  13. formConfig: BasicForm | AdvancedForm
  14. dialogConfig?: DialogProps
  15. height?: string
  16. cardHeight?: string
  17. showAdd?: boolean
  18. span?: number
  19. }
  20. const props = withDefaults(defineProps<Props>(), {
  21. pageSize: 12,
  22. showAdd: true,
  23. cardHeight: '226px',
  24. span: 4
  25. })
  26. const emits = defineEmits(['click-create', 'click-edit'])
  27. // ============== 查询部分开始 ===============
  28. const query = ref<any>({})
  29. const searchList = ref<any>([])
  30. watch(
  31. () => props.formConfig.formItems,
  32. val => {
  33. val.forEach((item: any) => {
  34. if (item.group) {
  35. searchList.value = searchList.value.concat(item.group.filter((item: any) => item.search))
  36. } else {
  37. item.search && searchList.value.push(item)
  38. }
  39. })
  40. },
  41. { immediate: true }
  42. )
  43. const handleQuery = () => {
  44. curPage.value = 1
  45. getTableData()
  46. }
  47. const handleReset = () => {
  48. query.value = {}
  49. handleQuery()
  50. }
  51. // ============== 查询部分结束 ===============
  52. // ============== 表格部分开始 ===============
  53. const tableData = ref<any>([])
  54. const total = ref(0)
  55. const curPage = ref(1)
  56. const loading = ref(false)
  57. const getTableData = () => {
  58. loading.value = true
  59. props.crud
  60. ?.getList({
  61. ...query.value,
  62. pageSize: props.pageSize,
  63. pageNo: curPage.value
  64. })
  65. .then((res: any) => {
  66. tableData.value = res.list || res.rows
  67. total.value = res.total
  68. })
  69. .finally(() => {
  70. loading.value = false
  71. })
  72. }
  73. watch(
  74. curPage,
  75. () => {
  76. getTableData()
  77. },
  78. {
  79. immediate: true
  80. }
  81. )
  82. const refresh = () => {
  83. curPage.value = 1
  84. getTableData()
  85. }
  86. // ============== 表格部分结束 ===============
  87. // ============== crud部分开始 ===============
  88. const formRoute = ref<any>(props.formConfig.route)
  89. const handleCreate = () => {
  90. emits('click-create')
  91. if (formRoute.value) {
  92. router.push(formRoute.value)
  93. } else {
  94. formData.value = {}
  95. dialogVisible.value = true
  96. }
  97. }
  98. const handleUpdate = (row: any) => {
  99. emits('click-edit', row)
  100. if (formRoute.value) {
  101. router.push(formRoute.value)
  102. } else {
  103. if (props.crud?.getRecord) {
  104. props.crud.getRecord({ id: row.id }).then((res: any) => {
  105. formData.value = res.data
  106. })
  107. } else {
  108. formData.value = { ...row }
  109. }
  110. dialogVisible.value = true
  111. }
  112. }
  113. const handleDelete = (id: string | number) => {
  114. ElMessageBox.confirm('您确定要删除该项吗', '提示', {
  115. type: 'warning'
  116. }).then(async () => {
  117. await props.crud?.delete({ id })
  118. getTableData()
  119. ElMessage({
  120. type: 'success',
  121. message: '删除成功'
  122. })
  123. })
  124. }
  125. // ============== crud部分结束 ===============
  126. // ============== 表单部分开始 ===============
  127. const formData = ref<any>({})
  128. const dialogVisible = ref(false)
  129. const handleFormSuccess = () => {
  130. getTableData()
  131. }
  132. // ============== 表单部分结束 ===============
  133. defineExpose({
  134. handleCreate,
  135. handleDelete,
  136. handleUpdate,
  137. refresh
  138. })
  139. </script>
  140. <template>
  141. <div class="flex flex-col" :style="{ height: height || 'calc(100vh - 101px - var(--main-padding) * 2)' }">
  142. <el-card class="mb-4" shadow="never" v-if="searchList.length">
  143. <el-form :inline="true">
  144. <el-form-item :label="item.label" v-for="(item, index) in searchList" :key="index">
  145. <form-comp :item="item" v-model="query[item.name]"></form-comp>
  146. </el-form-item>
  147. <slot name="query" :query="query"></slot>
  148. <el-form-item>
  149. <el-button type="primary" icon="Search" @click="handleQuery">查询</el-button>
  150. <el-button icon="Refresh" @click="handleReset">重置</el-button>
  151. </el-form-item>
  152. </el-form>
  153. </el-card>
  154. <el-card class="h-full flex-grow-1" :body-style="{ height: '100%' }" shadow="never">
  155. <div class="flex flex-col h-full">
  156. <slot name="header"></slot>
  157. <div class="h-full flex-grow">
  158. <el-row :gutter="10">
  159. <el-col :span="span" v-bind="$attrs" v-if="showAdd" class="mb-10">
  160. <div
  161. class="flex items-center justify-center border border-dashed w-full h-full card-list-item cursor-pointer"
  162. :style="{ height: cardHeight }"
  163. @click="handleCreate"
  164. >
  165. <el-icon :size="18" class="mr-2px"><plus></plus></el-icon>新增
  166. </div>
  167. </el-col>
  168. <el-col :span="span" v-bind="$attrs" v-for="(item, index) in tableData" :key="item.id" class="mb-10">
  169. <el-card :body-style="{ padding: '0px' }" shadow="hover">
  170. <slot :item="item" :index="index"></slot>
  171. </el-card>
  172. </el-col>
  173. </el-row>
  174. </div>
  175. <div class="flex justify-end shrink-0">
  176. <el-pagination
  177. background
  178. layout="prev, pager, next, jumper, total"
  179. v-model:current-page="curPage"
  180. :page-size="pageSize"
  181. :total="total"
  182. class="mt-16px"
  183. />
  184. </div>
  185. </div>
  186. </el-card>
  187. <dialog-form
  188. v-model="dialogVisible"
  189. :dialogConfig="dialogConfig"
  190. :formConfig="formConfig"
  191. :formData="formData"
  192. :create="crud.create"
  193. :update="crud.update"
  194. @success="handleFormSuccess"
  195. v-if="dialogVisible"
  196. />
  197. </div>
  198. </template>
  199. <style scoped lang="scss">
  200. .card-list-item {
  201. border-color: var(--el-border-color-light);
  202. border-radius: var(--el-card-border-radius);
  203. color: var(--el-text-color-regular);
  204. &:hover {
  205. color: var(--el-color-primary);
  206. border-color: currentColor;
  207. }
  208. }
  209. </style>