DefaultTable.vue 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <script setup lang="tsx">
  2. import { ContentWrap } from '@/components/ContentWrap'
  3. import { useI18n } from '@/hooks/web/useI18n'
  4. import { Table, TableColumn } 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. interface Params {
  10. pageIndex?: number
  11. pageSize?: number
  12. }
  13. const { t } = useI18n()
  14. const columns: TableColumn[] = [
  15. {
  16. field: 'title',
  17. label: t('tableDemo.title')
  18. },
  19. {
  20. field: 'author',
  21. label: t('tableDemo.author')
  22. },
  23. {
  24. field: 'display_time',
  25. label: t('tableDemo.displayTime'),
  26. sortable: true
  27. },
  28. {
  29. field: 'importance',
  30. label: t('tableDemo.importance'),
  31. formatter: (_: Recordable, __: TableColumn, cellValue: number) => {
  32. return h(
  33. ElTag,
  34. {
  35. type: cellValue === 1 ? 'success' : cellValue === 2 ? 'warning' : 'danger'
  36. },
  37. () =>
  38. cellValue === 1
  39. ? t('tableDemo.important')
  40. : cellValue === 2
  41. ? t('tableDemo.good')
  42. : t('tableDemo.commonly')
  43. )
  44. }
  45. },
  46. {
  47. field: 'pageviews',
  48. label: t('tableDemo.pageviews')
  49. },
  50. {
  51. field: 'action',
  52. label: t('tableDemo.action'),
  53. slots: {
  54. default: (data) => {
  55. return (
  56. <ElButton type="primary" onClick={() => actionFn(data)}>
  57. {t('tableDemo.action')}
  58. </ElButton>
  59. )
  60. }
  61. }
  62. }
  63. ]
  64. const loading = ref(true)
  65. let tableDataList = ref<TableData[]>([])
  66. const getTableList = async (params?: Params) => {
  67. const res = await getTableListApi(
  68. params || {
  69. pageIndex: 1,
  70. pageSize: 10
  71. }
  72. )
  73. .catch(() => {})
  74. .finally(() => {
  75. loading.value = false
  76. })
  77. if (res) {
  78. tableDataList.value = res.data.list
  79. }
  80. }
  81. getTableList()
  82. const actionFn = (data: any) => {
  83. console.log(data)
  84. }
  85. </script>
  86. <template>
  87. <ContentWrap :title="t('tableDemo.table')" :message="t('tableDemo.tableDes')">
  88. <Table
  89. :columns="columns"
  90. :data="tableDataList"
  91. :loading="loading"
  92. :defaultSort="{ prop: 'display_time', order: 'descending' }"
  93. />
  94. </ContentWrap>
  95. </template>