UseTableDemo.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <script setup lang="tsx">
  2. import { ContentWrap } from '@/components/ContentWrap'
  3. import { useI18n } from '@/hooks/web/useI18n'
  4. import { Table, TableColumn, TableSlotDefault } from '@/components/Table'
  5. import { getTableListApi } from '@/api/table'
  6. import { ref, reactive, unref } from 'vue'
  7. import { ElTag, ElButton } from 'element-plus'
  8. import { useTable } from '@/hooks/web/useTable'
  9. const { tableRegister, tableMethods, tableState } = useTable({
  10. fetchDataApi: async () => {
  11. const { currentPage, pageSize } = tableState
  12. const res = await getTableListApi({
  13. pageIndex: unref(currentPage),
  14. pageSize: unref(pageSize)
  15. })
  16. return {
  17. list: res.data.list,
  18. total: res.data.total
  19. }
  20. }
  21. })
  22. const { loading, dataList, total, currentPage, pageSize } = tableState
  23. const { setProps, setColumn, getElTableExpose, addColumn, delColumn, refresh } = tableMethods
  24. const { t } = useI18n()
  25. const columns = reactive<TableColumn[]>([
  26. {
  27. field: 'expand',
  28. type: 'expand',
  29. slots: {
  30. default: (data: TableSlotDefault) => {
  31. const { row } = data
  32. return (
  33. <div class="ml-30px">
  34. <div>
  35. {t('tableDemo.title')}:{row.title}
  36. </div>
  37. <div>
  38. {t('tableDemo.author')}:{row.author}
  39. </div>
  40. <div>
  41. {t('tableDemo.displayTime')}:{row.display_time}
  42. </div>
  43. </div>
  44. )
  45. }
  46. }
  47. },
  48. {
  49. field: 'selection',
  50. type: 'selection'
  51. },
  52. {
  53. field: 'index',
  54. label: t('tableDemo.index'),
  55. type: 'index'
  56. },
  57. {
  58. field: 'content',
  59. label: t('tableDemo.header'),
  60. children: [
  61. {
  62. field: 'title',
  63. label: t('tableDemo.title')
  64. },
  65. {
  66. field: 'author',
  67. label: t('tableDemo.author')
  68. },
  69. {
  70. field: 'display_time',
  71. label: t('tableDemo.displayTime')
  72. },
  73. {
  74. field: 'importance',
  75. label: t('tableDemo.importance'),
  76. formatter: (_: Recordable, __: TableColumn, cellValue: number) => {
  77. return (
  78. <ElTag type={cellValue === 1 ? 'success' : cellValue === 2 ? 'warning' : 'danger'}>
  79. {cellValue === 1
  80. ? t('tableDemo.important')
  81. : cellValue === 2
  82. ? t('tableDemo.good')
  83. : t('tableDemo.commonly')}
  84. </ElTag>
  85. )
  86. }
  87. },
  88. {
  89. field: 'pageviews',
  90. label: t('tableDemo.pageviews')
  91. }
  92. ]
  93. },
  94. {
  95. field: 'action',
  96. label: t('tableDemo.action'),
  97. slots: {
  98. default: (data) => {
  99. return (
  100. <ElButton type="primary" onClick={() => actionFn(data)}>
  101. {t('tableDemo.action')}
  102. </ElButton>
  103. )
  104. }
  105. }
  106. }
  107. ])
  108. const actionFn = (data: TableSlotDefault) => {
  109. console.log(data)
  110. }
  111. const canShowPagination = ref(true)
  112. const showPagination = (show: boolean) => {
  113. canShowPagination.value = show
  114. }
  115. const reserveIndex = (custom: boolean) => {
  116. setProps({
  117. reserveIndex: custom
  118. })
  119. }
  120. const showSelections = (show: boolean) => {
  121. setColumn([
  122. {
  123. field: 'selection',
  124. path: 'hidden',
  125. value: !show
  126. }
  127. ])
  128. }
  129. const index = ref(1)
  130. const changeTitle = () => {
  131. setColumn([
  132. {
  133. field: 'title',
  134. path: 'label',
  135. value: `${t('tableDemo.title')}${unref(index)}`
  136. }
  137. ])
  138. index.value++
  139. }
  140. const showExpandedRows = (show: boolean) => {
  141. setColumn([
  142. {
  143. field: 'expand',
  144. path: 'hidden',
  145. value: !show
  146. }
  147. ])
  148. }
  149. const selectAllNone = async () => {
  150. const elTableRef = await getElTableExpose()
  151. elTableRef?.toggleAllSelection()
  152. }
  153. const showAction = ref(true)
  154. const delOrAddAction = () => {
  155. if (unref(showAction)) {
  156. delColumn('action')
  157. showAction.value = false
  158. } else {
  159. addColumn({
  160. field: 'action',
  161. label: t('tableDemo.action'),
  162. slots: {
  163. default: (data) => {
  164. return (
  165. <ElButton type="primary" onClick={() => actionFn(data)}>
  166. {t('tableDemo.action')}
  167. </ElButton>
  168. )
  169. }
  170. }
  171. })
  172. showAction.value = true
  173. }
  174. }
  175. const showStripe = ref(false)
  176. const showOrHiddenStripe = () => {
  177. setProps({
  178. stripe: !unref(showStripe)
  179. })
  180. showStripe.value = !unref(showStripe)
  181. }
  182. const height = ref<string | number>('auto')
  183. const fixedHeaderOrAuto = () => {
  184. if (unref(height) === 'auto') {
  185. setProps({
  186. height: 300
  187. })
  188. height.value = 300
  189. } else {
  190. setProps({
  191. height: 'auto'
  192. })
  193. height.value = 'auto'
  194. }
  195. }
  196. const getSelections = async () => {
  197. const elTableRef = await getElTableExpose()
  198. const selections = elTableRef?.getSelectionRows()
  199. console.log(selections)
  200. }
  201. </script>
  202. <template>
  203. <ContentWrap :title="`UseTable ${t('tableDemo.operate')}`" style="margin-bottom: 20px">
  204. <ElButton @click="showPagination(true)">
  205. {{ t('tableDemo.show') }} {{ t('tableDemo.pagination') }}
  206. </ElButton>
  207. <ElButton @click="showPagination(false)">
  208. {{ t('tableDemo.hidden') }} {{ t('tableDemo.pagination') }}
  209. </ElButton>
  210. <ElButton @click="reserveIndex(true)">{{ t('tableDemo.reserveIndex') }}</ElButton>
  211. <ElButton @click="reserveIndex(false)">{{ t('tableDemo.restoreIndex') }}</ElButton>
  212. <ElButton @click="showSelections(true)">{{ t('tableDemo.showSelections') }}</ElButton>
  213. <ElButton @click="showSelections(false)">{{ t('tableDemo.hiddenSelections') }}</ElButton>
  214. <ElButton @click="changeTitle">{{ t('tableDemo.changeTitle') }}</ElButton>
  215. <ElButton @click="showExpandedRows(true)">{{ t('tableDemo.showExpandedRows') }}</ElButton>
  216. <ElButton @click="showExpandedRows(false)">{{ t('tableDemo.hiddenExpandedRows') }}</ElButton>
  217. <ElButton @click="selectAllNone">{{ t('tableDemo.selectAllNone') }}</ElButton>
  218. <ElButton @click="delOrAddAction">{{ t('tableDemo.delOrAddAction') }}</ElButton>
  219. <ElButton @click="showOrHiddenStripe">{{ t('tableDemo.showOrHiddenStripe') }}</ElButton>
  220. <ElButton @click="fixedHeaderOrAuto">{{ t('tableDemo.fixedHeaderOrAuto') }}</ElButton>
  221. <ElButton @click="getSelections">{{ t('tableDemo.getSelections') }}</ElButton>
  222. <!-- <ElButton @click="showOrHiddenSortable">{{ t('tableDemo.showOrHiddenSortable') }}</ElButton> -->
  223. </ContentWrap>
  224. <ContentWrap :title="`UseTable ${t('tableDemo.example')}`">
  225. <Table
  226. v-model:pageSize="pageSize"
  227. v-model:currentPage="currentPage"
  228. showAction
  229. :columns="columns"
  230. :data="dataList"
  231. :loading="loading"
  232. :pagination="
  233. canShowPagination
  234. ? {
  235. total: total
  236. }
  237. : undefined
  238. "
  239. @register="tableRegister"
  240. @refresh="refresh"
  241. />
  242. </ContentWrap>
  243. </template>
  244. <style lang="less" scoped>
  245. .el-button {
  246. margin-top: 10px;
  247. }
  248. </style>