index.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <template>
  2. <a-row>
  3. <a-col span="24" >
  4. <a-row justify="end">
  5. <a-col>
  6. <a-space>
  7. <a-button type="primary" @click="openModel('add')" >
  8. 新增
  9. <template #icon>
  10. <plus-outlined />
  11. </template>
  12. </a-button>
  13. <a-button type="primary" >
  14. 导出
  15. <template #icon>
  16. <download-outlined />
  17. </template>
  18. </a-button>
  19. <!-- 小组件 -->
  20. <a-tooltip>
  21. <template #title >刷新</template>
  22. <reload-outlined v-if="!state.loading" @click="reload"/>
  23. <loading-outlined v-else />
  24. </a-tooltip>
  25. </a-space>
  26. </a-col>
  27. </a-row>
  28. </a-col>
  29. <a-col span="24" style="overflow: hidden;overflow-x: scroll;">
  30. <a-table
  31. style="width: 100%;margin-top: 10px;"
  32. v-bind="{...props}"
  33. :data-source="state.data"
  34. :loading="state.loading"
  35. :pagination="state.pagination.total <= 10 ? false : state.pagination"
  36. @change="handleTableChange"
  37. >
  38. <template #bodyCell="{ column, record }">
  39. <template v-if="column.key === 'action'">
  40. <a-space>
  41. <span v-for="item in column.list" :key="item.name" >
  42. <a-popconfirm
  43. title="确实要删除吗?"
  44. ok-text="确定"
  45. cancel-text="取消"
  46. @confirm="confirmDel(item, record)"
  47. v-if="item.use == 'del'"
  48. >
  49. <a href="#">删除</a>
  50. </a-popconfirm>
  51. <a v-else @click="handleAction(item, record)" >{{item.name}}</a>
  52. </span>
  53. </a-space>
  54. </template>
  55. </template>
  56. </a-table>
  57. </a-col>
  58. </a-row>
  59. <!-- 弹窗 -->
  60. <modal-pro
  61. :label="props.modalTitle"
  62. :visible="state.modelVisible"
  63. @cancel="closeModel"
  64. @ok="ok"
  65. >
  66. <form-pro ref="formProRef" />
  67. </modal-pro>
  68. </template>
  69. <script lang="ts" setup >
  70. import { useCopy } from '@/hooks/dom'
  71. import { ColumnsType } from 'ant-design-vue/lib/vc-table/interface'
  72. import { TableProps } from 'ant-design-vue/lib/vc-table/Table'
  73. import { reactive, ref, watch } from 'vue'
  74. import { PlusOutlined, DownloadOutlined, ReloadOutlined, LoadingOutlined } from '@ant-design/icons-vue'
  75. /**
  76. * formItemProps
  77. * rules 校验
  78. * request 需要请求的表单项 分别创建id key value name
  79. * type: input select radio check datepick
  80. */
  81. /**
  82. * pro-able
  83. * 'export' | 'add' | search
  84. */
  85. export interface ColumnsChildren {
  86. name: '',
  87. use: string,
  88. icon: '',
  89. bindKey: string
  90. }
  91. export interface TablePropPorps extends TableProps {
  92. request: (params: any) => any
  93. del: (id: string) => void
  94. params: any,
  95. columns: ColumnsType & {children: ColumnsChildren},
  96. modalTitle: string
  97. }
  98. const props = defineProps<TablePropPorps>()
  99. const formProRef = ref()
  100. const state = reactive({
  101. loading: false,
  102. data: [],
  103. pagination: {
  104. current: 1,
  105. total: 0,
  106. pageSize: 10
  107. },
  108. modelVisible: false,
  109. opraState: 'add'
  110. })
  111. const ok = () => {
  112. formProRef.value.onSubmit()
  113. }
  114. const handleTableChange = (pag: { pageSize: number; current: number }) => {
  115. state.pagination = {
  116. ...state.pagination,
  117. pageSize: pag.pageSize,
  118. current: pag.current
  119. }
  120. }
  121. // 请求table 的data
  122. const request = async () => {
  123. state.loading = true
  124. const data = await props.request(props.params)
  125. state.loading = false
  126. console.log('table pro data:', data)
  127. state.data = data
  128. }
  129. // 删除对应的table选项
  130. const confirmDel = async (item: ColumnsChildren, record: any) => {
  131. await props.del(record[item.bindKey])
  132. request()
  133. }
  134. const handleAction = (column: ColumnsChildren, record: any) => {
  135. switch (column.use) {
  136. case 'copy':
  137. useCopy(record[column.bindKey])
  138. }
  139. }
  140. // `const reload = () => request()` 正在定义一个名为 `reload` 的函数,该函数调用 `request` 函数。该函数用于在用户点击“刷新”按钮时重新加载表格数据。
  141. const reload = () => request()
  142. const openModel = (type: 'add' | 'update') => {
  143. state.modelVisible = true
  144. state.opraState = type
  145. }
  146. const closeModel = () => {
  147. state.modelVisible = false
  148. }
  149. watch(
  150. () => props.request,
  151. () => request(),
  152. {
  153. deep: true,
  154. immediate: true
  155. }
  156. )
  157. </script>
  158. <style>
  159. </style>