ExampleDialog.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <script setup lang="ts">
  2. import { ContentWrap } from '@/components/ContentWrap'
  3. import { Search } from '@/components/Search'
  4. import { Dialog } from '@/components/Dialog'
  5. import { useI18n } from '@/hooks/web/useI18n'
  6. import { ElButton, ElTag } from 'element-plus'
  7. import { Table } from '@/components/Table'
  8. import { getTableListApi, saveTableApi, delTableListApi } from '@/api/table'
  9. import { useTable } from '@/hooks/web/useTable'
  10. import { TableData } from '@/api/table/types'
  11. import { h, reactive, ref, unref } from 'vue'
  12. import Write from './components/Write.vue'
  13. import Detail from './components/Detail.vue'
  14. const { register, tableObject, methods } = useTable<
  15. {
  16. total: number
  17. list: TableData[]
  18. },
  19. TableData
  20. >({
  21. getListApi: getTableListApi,
  22. delListApi: delTableListApi,
  23. response: {
  24. list: 'list',
  25. total: 'total'
  26. }
  27. })
  28. const { getList, setSearchParams } = methods
  29. getList()
  30. const { t } = useI18n()
  31. const searchData: FormSchema[] = [
  32. {
  33. label: t('exampleDemo.title'),
  34. value: '',
  35. component: 'Input',
  36. field: 'title'
  37. }
  38. ]
  39. const columns = reactive<TableColumn[]>([
  40. {
  41. field: 'index',
  42. label: t('tableDemo.index'),
  43. type: 'index'
  44. },
  45. {
  46. field: 'title',
  47. label: t('tableDemo.title')
  48. },
  49. {
  50. field: 'author',
  51. label: t('tableDemo.author')
  52. },
  53. {
  54. field: 'display_time',
  55. label: t('tableDemo.displayTime')
  56. },
  57. {
  58. field: 'importance',
  59. label: t('tableDemo.importance'),
  60. formatter: (_: Recordable, __: TableColumn, cellValue: number) => {
  61. return h(
  62. ElTag,
  63. {
  64. type: cellValue === 1 ? 'success' : cellValue === 2 ? 'warning' : 'danger'
  65. },
  66. () =>
  67. cellValue === 1
  68. ? t('tableDemo.important')
  69. : cellValue === 2
  70. ? t('tableDemo.good')
  71. : t('tableDemo.commonly')
  72. )
  73. }
  74. },
  75. {
  76. field: 'pageviews',
  77. label: t('tableDemo.pageviews')
  78. },
  79. {
  80. field: 'action',
  81. width: '260px',
  82. label: t('tableDemo.action')
  83. }
  84. ])
  85. const dialogVisible = ref(false)
  86. const dialogTitle = ref('')
  87. const AddAction = () => {
  88. dialogTitle.value = t('exampleDemo.add')
  89. tableObject.currentRow = null
  90. dialogVisible.value = true
  91. }
  92. const delLoading = ref(false)
  93. const delData = async (row: TableData | null, multiple: boolean) => {
  94. tableObject.currentRow = row
  95. const { delList, getSelections } = methods
  96. const selections = await getSelections()
  97. delLoading.value = true
  98. await delList(
  99. multiple ? selections.map((v) => v.id) : [tableObject.currentRow?.id as string],
  100. multiple
  101. ).finally(() => {
  102. delLoading.value = false
  103. })
  104. }
  105. const actionType = ref('')
  106. const action = (row: TableData, type: string) => {
  107. dialogTitle.value = t(type === 'edit' ? 'exampleDemo.edit' : 'exampleDemo.detail')
  108. actionType.value = type
  109. tableObject.currentRow = row
  110. dialogVisible.value = true
  111. }
  112. const writeRef = ref<ComponentRef<typeof Write>>()
  113. const loading = ref(false)
  114. const save = async () => {
  115. const write = unref(writeRef)
  116. await write?.elFormRef?.validate(async (isValid) => {
  117. if (isValid) {
  118. loading.value = true
  119. const data = (await write?.getFormData()) as TableData
  120. const res = await saveTableApi({
  121. data
  122. })
  123. .catch(() => {})
  124. .finally(() => {
  125. loading.value = false
  126. })
  127. if (res) {
  128. dialogVisible.value = false
  129. tableObject.currentPage = 1
  130. getList()
  131. }
  132. }
  133. })
  134. }
  135. </script>
  136. <template>
  137. <ContentWrap>
  138. <Search :schema="searchData" @search="setSearchParams" @reset="setSearchParams" />
  139. <div class="mb-10px">
  140. <ElButton type="primary" @click="AddAction">{{ t('exampleDemo.add') }}</ElButton>
  141. <ElButton :loading="delLoading" type="danger" @click="delData(null, true)">
  142. {{ t('exampleDemo.del') }}
  143. </ElButton>
  144. </div>
  145. <Table
  146. v-model:pageSize="tableObject.pageSize"
  147. v-model:currentPage="tableObject.currentPage"
  148. :columns="columns"
  149. :data="tableObject.tableList"
  150. :loading="tableObject.loading"
  151. :pagination="{
  152. total: tableObject.total
  153. }"
  154. @register="register"
  155. >
  156. <template #action="{ row }">
  157. <ElButton type="primary" @click="action(row, 'edit')">
  158. {{ t('exampleDemo.edit') }}
  159. </ElButton>
  160. <ElButton type="success" @click="action(row, 'detail')">
  161. {{ t('exampleDemo.detail') }}
  162. </ElButton>
  163. <ElButton type="danger" @click="delData(row, false)">
  164. {{ t('exampleDemo.del') }}
  165. </ElButton>
  166. </template>
  167. </Table>
  168. </ContentWrap>
  169. <Dialog v-model="dialogVisible" :title="dialogTitle">
  170. <Write v-if="actionType !== 'detail'" ref="writeRef" :current-row="tableObject.currentRow" />
  171. <Detail v-if="actionType === 'detail'" :current-row="tableObject.currentRow" />
  172. <template #footer>
  173. <ElButton v-if="actionType !== 'detail'" type="primary" :loading="loading" @click="save">
  174. {{ t('exampleDemo.save') }}
  175. </ElButton>
  176. <ElButton @click="dialogVisible = false">{{ t('dialogDemo.close') }}</ElButton>
  177. </template>
  178. </Dialog>
  179. </template>