RefTable.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <script setup lang="ts">
  2. import { ContentWrap } from '@/components/ContentWrap'
  3. import { useI18n } from '@/hooks/web/useI18n'
  4. import { Table, TableExpose } from '@/components/Table'
  5. import { getTableListApi } from '@/api/table'
  6. import { TableData } from '@/api/table/types'
  7. import { ref, h, reactive, unref } from 'vue'
  8. import { ElTag, ElButton } from 'element-plus'
  9. import { useTable } from '@/hooks/web/useTable'
  10. import { Pagination, TableColumn, TableSlotDefault } from '@/types/table'
  11. const { t } = useI18n()
  12. const columns = reactive<TableColumn[]>([
  13. {
  14. field: 'index',
  15. label: t('tableDemo.index'),
  16. type: 'index'
  17. },
  18. {
  19. field: 'content',
  20. label: t('tableDemo.header'),
  21. children: [
  22. {
  23. field: 'title',
  24. label: t('tableDemo.title')
  25. },
  26. {
  27. field: 'author',
  28. label: t('tableDemo.author')
  29. },
  30. {
  31. field: 'display_time',
  32. label: t('tableDemo.displayTime')
  33. },
  34. {
  35. field: 'importance',
  36. label: t('tableDemo.importance'),
  37. formatter: (_: Recordable, __: TableColumn, cellValue: number) => {
  38. return h(
  39. ElTag,
  40. {
  41. type: cellValue === 1 ? 'success' : cellValue === 2 ? 'warning' : 'danger'
  42. },
  43. () =>
  44. cellValue === 1
  45. ? t('tableDemo.important')
  46. : cellValue === 2
  47. ? t('tableDemo.good')
  48. : t('tableDemo.commonly')
  49. )
  50. }
  51. },
  52. {
  53. field: 'pageviews',
  54. label: t('tableDemo.pageviews')
  55. }
  56. ]
  57. },
  58. {
  59. field: 'action',
  60. label: t('tableDemo.action')
  61. }
  62. ])
  63. const { register, tableObject, methods } = useTable<TableData>({
  64. getListApi: getTableListApi,
  65. response: {
  66. list: 'list',
  67. total: 'total'
  68. },
  69. props: {
  70. columns
  71. }
  72. })
  73. const { getList } = methods
  74. getList()
  75. const tableRef = ref<TableExpose>()
  76. const acitonFn = (data: TableSlotDefault) => {
  77. console.log(data)
  78. }
  79. const paginationObj = ref<Pagination>()
  80. const showPagination = (show: boolean) => {
  81. if (show) {
  82. paginationObj.value = {
  83. total: tableObject.total
  84. }
  85. } else {
  86. paginationObj.value = undefined
  87. }
  88. }
  89. const reserveIndex = (custom: boolean) => {
  90. unref(tableRef)?.setProps({
  91. reserveIndex: custom
  92. })
  93. }
  94. const showSelections = (show: boolean) => {
  95. unref(tableRef)?.setProps({
  96. selection: show
  97. })
  98. }
  99. const index = ref(1)
  100. const changeTitle = () => {
  101. unref(tableRef)?.setColumn([
  102. {
  103. field: 'title',
  104. path: 'label',
  105. value: `${t('tableDemo.title')}${unref(index)}`
  106. }
  107. ])
  108. index.value++
  109. }
  110. const showExpandedRows = (show: boolean) => {
  111. unref(tableRef)?.setProps({
  112. expand: show
  113. })
  114. }
  115. const selectAllNone = () => {
  116. unref(tableRef)?.elTableRef?.toggleAllSelection()
  117. }
  118. </script>
  119. <template>
  120. <ContentWrap :title="`RefTable ${t('tableDemo.operate')}`">
  121. <ElButton @click="showPagination(true)">
  122. {{ t('tableDemo.show') }} {{ t('tableDemo.pagination') }}
  123. </ElButton>
  124. <ElButton @click="showPagination(false)">
  125. {{ t('tableDemo.hidden') }} {{ t('tableDemo.pagination') }}
  126. </ElButton>
  127. <ElButton @click="reserveIndex(true)">{{ t('tableDemo.reserveIndex') }}</ElButton>
  128. <ElButton @click="reserveIndex(false)">{{ t('tableDemo.restoreIndex') }}</ElButton>
  129. <ElButton @click="showSelections(true)">{{ t('tableDemo.showSelections') }}</ElButton>
  130. <ElButton @click="showSelections(false)">{{ t('tableDemo.hiddenSelections') }}</ElButton>
  131. <ElButton @click="changeTitle">{{ t('tableDemo.changeTitle') }}</ElButton>
  132. <ElButton @click="showExpandedRows(true)">{{ t('tableDemo.showExpandedRows') }}</ElButton>
  133. <ElButton @click="showExpandedRows(false)">{{ t('tableDemo.hiddenExpandedRows') }}</ElButton>
  134. <ElButton @click="selectAllNone">{{ t('tableDemo.selectAllNone') }}</ElButton>
  135. </ContentWrap>
  136. <ContentWrap :title="`RefTable ${t('tableDemo.example')}`">
  137. <Table
  138. ref="tableRef"
  139. v-model:pageSize="tableObject.pageSize"
  140. v-model:currentPage="tableObject.currentPage"
  141. :data="tableObject.tableList"
  142. :loading="tableObject.loading"
  143. :pagination="paginationObj"
  144. @register="register"
  145. >
  146. <template #action="data">
  147. <ElButton type="primary" @click="acitonFn(data as TableSlotDefault)">
  148. {{ t('tableDemo.action') }}
  149. </ElButton>
  150. </template>
  151. <template #expand="data">
  152. <div class="ml-30px">
  153. <div>{{ t('tableDemo.title') }}:{{ data.row.title }}</div>
  154. <div>{{ t('tableDemo.author') }}:{{ data.row.author }}</div>
  155. <div>{{ t('tableDemo.displayTime') }}:{{ data.row.display_time }}</div>
  156. </div>
  157. </template>
  158. </Table>
  159. </ContentWrap>
  160. </template>