yt-crud.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <template>
  2. <div>
  3. <yt-table-search v-bind="bind.searchBind" :data="data" :column="column" v-model:query="query" @handle-search="search">
  4. <template v-for="(item, index) in searchSlots" :key="index" #[item]="scope">
  5. <slot
  6. :name="item"
  7. v-bind="{
  8. ...scope,
  9. }"
  10. />
  11. </template>
  12. </yt-table-search>
  13. <yt-table-fun
  14. v-bind="bind.funBind"
  15. v-loading="loading"
  16. :addBtn="addBtn"
  17. :multipleSelection="multipleSelection"
  18. :limit="pageParams.pageSize"
  19. @change-all="changeAll"
  20. @handleDel="handleDel"
  21. @handle-export="emits('exportFun')"
  22. @handle-add="handleAdd()"
  23. >
  24. <template #rightToolbar>
  25. <slot name="rightToolbar" />
  26. </template>
  27. <yt-table
  28. v-bind="bind.tableBind"
  29. :data="data"
  30. :column="column"
  31. :total="total"
  32. v-model:page="pageParams"
  33. ref="ytTableRef"
  34. v-model:multiple-selection="multipleSelection"
  35. @handle-selection-change="handleSelectionChange"
  36. @handle-update="handleUpdate"
  37. @handle-delete="handleDel"
  38. @handle-view="handleView"
  39. @change-page="changePage"
  40. @row-click="rowClick"
  41. >
  42. <template #customTable>
  43. <slot name="customTable" :handle-update="handleUpdate" :handle-delete="handleDel" :handle-view="handleView" />
  44. </template>
  45. <template #menuSlot="scope">
  46. <slot
  47. name="menuSlot"
  48. v-bind="{
  49. ...scope,
  50. }"
  51. />
  52. </template>
  53. <template v-for="(item, index) in tableSlots" :key="index" #[item]="scope">
  54. <slot
  55. :name="item"
  56. v-bind="{
  57. ...scope,
  58. }"
  59. />
  60. </template>
  61. </yt-table>
  62. </yt-table-fun>
  63. <yt-table-form
  64. ref="tableFormRef"
  65. :loading="loading"
  66. v-bind="bind.formBind"
  67. :data="data"
  68. :column="column"
  69. @on-success="onSuccess"
  70. @open-before-fun="openBeforeFun"
  71. >
  72. <template v-for="(item, index) in formSlots" :key="index" #[item]="scope">
  73. <slot
  74. :name="item"
  75. v-bind="{
  76. ...scope,
  77. }"
  78. />
  79. </template>
  80. <template v-for="(item, index) in formItemSlots" :key="index" #[item]="scope">
  81. <slot
  82. :name="item"
  83. v-bind="{
  84. ...scope,
  85. }"
  86. />
  87. </template>
  88. </yt-table-form>
  89. </div>
  90. </template>
  91. <script lang="ts" setup>
  92. import { crudProps } from './props/crudProps'
  93. import YtTableSearch from '@/components/common/yt-table-search'
  94. import YtTableFun from '@/components/common/yt-table-fun.vue'
  95. import YtTable from '@/components/common/yt-table'
  96. import YtTableForm from '@/components/common/yt-table-form'
  97. const props = defineProps({
  98. ...crudProps,
  99. })
  100. let tableSlots = ref<string[]>([])
  101. let formSlots = ref<string[]>([])
  102. let formItemSlots = ref<string[]>([])
  103. let searchSlots = ref<string[]>([])
  104. watch(() => props.column, (newV) => {
  105. if (newV?.length > 0) {
  106. tableSlots.value = newV.filter(f => f.slot).map(m => m.key)
  107. formSlots.value = newV.filter(f => f.formSlot).map(m => m.key + 'Form')
  108. formItemSlots.value = newV.filter(f => f.formItemSlot).map(m => m.key + 'FormItem')
  109. searchSlots.value = newV.filter(f => f.searchSlot).map(m => m.key + 'Search')
  110. }
  111. }, {
  112. immediate: true,
  113. deep: true,
  114. })
  115. const emits = defineEmits(['change', 'onLoad', 'update:query', 'saveFun', 'rowClick', 'delFun', 'openBeforeFun', 'exportFun', 'update:page'])
  116. const ytTableRef = ref()
  117. const getTableRef = () => {
  118. return ytTableRef.value
  119. }
  120. const tableFormRef = ref()
  121. // 上传前
  122. const openBeforeFun = (data) => {
  123. emits('openBeforeFun', data)
  124. }
  125. const handleAdd = () => {
  126. tableFormRef.value?.openDialog('add')
  127. }
  128. const handleDel = (row: any) => {
  129. emits('delFun', row)
  130. }
  131. const handleUpdate = (row: any) => {
  132. tableFormRef.value?.openDialog('update', row)
  133. }
  134. const handleView = (row: any) => {
  135. tableFormRef.value?.openDialog('view', row)
  136. }
  137. const rowClick = (row: any) => {
  138. emits('rowClick', row)
  139. }
  140. // 多选
  141. const multipleSelection = ref([])
  142. const handleSelectionChange = (val) => {
  143. console.log('val', val)
  144. }
  145. const changeAll = (e) => {
  146. console.log()
  147. getTableRef().tableRef.toggleAllSelection()
  148. console.log('e', e)
  149. }
  150. // 表单保存
  151. const onSuccess = (obj: any) => {
  152. emits('saveFun', obj)
  153. }
  154. const addBtn = ref(props.addBtn)
  155. // 传给列表需要的参数
  156. const queryParams = ref(props.query)
  157. const search = (params: any) => {
  158. emits('update:query', params || {})
  159. queryParams.value = params
  160. onLoad()
  161. }
  162. // 分页
  163. const pageParams = ref(props.page)
  164. const changePage = (data: any) => {
  165. pageParams.value = toRaw(data)
  166. emits('update:page', pageParams.value)
  167. onLoad()
  168. }
  169. // 加载数据
  170. const onLoad = () => {
  171. emits('onLoad', queryParams.value, pageParams.value)
  172. }
  173. onLoad()
  174. const objBind = {}
  175. const bind = reactive({
  176. searchBind: objBind,
  177. tableBind: objBind,
  178. funBind: {},
  179. formBind: objBind,
  180. })
  181. watch(props, (newV) => {
  182. // 搜索组件绑定值
  183. if (props.searchProps) bind.searchBind = {
  184. ...bind.searchBind,
  185. ...props.searchProps,
  186. }
  187. // 表格组件绑定值
  188. if (props.tableProps) bind.tableBind = {
  189. ...bind.tableBind,
  190. ...props.tableProps,
  191. }
  192. // 功能区组件绑定值
  193. if (props.funProps) bind.funBind = {
  194. ...props.funProps,
  195. }
  196. // 表单组件绑定值
  197. if (props.formProps) bind.formBind = {
  198. ...bind.formBind,
  199. ...props.formProps,
  200. }
  201. }, {
  202. immediate: true,
  203. deep: true,
  204. })
  205. defineExpose({
  206. getTableRef,
  207. handleAdd,
  208. handleDel,
  209. handleUpdate,
  210. handleView,
  211. })
  212. </script>
  213. <!-- <style lang="scss" scoped>
  214. </style> -->