DefaultTable.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <script setup lang="ts">
  2. import { ContentWrap } from '@/components/ContentWrap'
  3. import { useI18n } from '@/hooks/web/useI18n'
  4. import { Table } from '@/components/Table'
  5. import { getTableListApi } from '@/api/table'
  6. import { TableData } from '@/api/table/types'
  7. import { ref, h } from 'vue'
  8. import { ElTag, ElButton } from 'element-plus'
  9. import { TableColumn, TableSlotDefault } from '@/types/table'
  10. interface Params {
  11. pageIndex?: number
  12. pageSize?: number
  13. }
  14. const { t } = useI18n()
  15. const columns: TableColumn[] = [
  16. {
  17. field: 'index',
  18. label: t('tableDemo.index'),
  19. type: 'index'
  20. },
  21. {
  22. field: 'title',
  23. label: t('tableDemo.title')
  24. },
  25. {
  26. field: 'author',
  27. label: t('tableDemo.author')
  28. },
  29. {
  30. field: 'display_time',
  31. label: t('tableDemo.displayTime'),
  32. sortable: true
  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. field: 'action',
  58. label: t('tableDemo.action')
  59. }
  60. ]
  61. const loading = ref(true)
  62. let tableDataList = ref<TableData[]>([])
  63. const getTableList = async (params?: Params) => {
  64. const res = await getTableListApi(
  65. params || {
  66. pageIndex: 1,
  67. pageSize: 10
  68. }
  69. )
  70. .catch(() => {})
  71. .finally(() => {
  72. loading.value = false
  73. })
  74. if (res) {
  75. tableDataList.value = res.data.list
  76. }
  77. }
  78. getTableList()
  79. const actionFn = (data: TableSlotDefault) => {
  80. console.log(data)
  81. }
  82. </script>
  83. <template>
  84. <ContentWrap :title="t('tableDemo.table')" :message="t('tableDemo.tableDes')">
  85. <Table
  86. :columns="columns"
  87. :data="tableDataList"
  88. :loading="loading"
  89. :defaultSort="{ prop: 'display_time', order: 'descending' }"
  90. >
  91. <template #action="data">
  92. <ElButton type="primary" @click="actionFn(data as TableSlotDefault)">
  93. {{ t('tableDemo.action') }}
  94. </ElButton>
  95. </template>
  96. </Table>
  97. </ContentWrap>
  98. </template>