index.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <script lang="ts" setup>
  2. import type { UseVbenVxeGrid, VxeGridProps } from '#/adapter/vxe-table';
  3. import { inject } from 'vue';
  4. import { Button } from 'ant-design-vue';
  5. import { type DemoTableApi } from '../mock-api';
  6. import { MOCK_API_DATA } from '../table-data';
  7. interface RowType {
  8. category: string;
  9. color: string;
  10. id: string;
  11. price: string;
  12. productName: string;
  13. releaseDate: string;
  14. }
  15. const useVbenVxeGrid = inject<UseVbenVxeGrid>(
  16. 'useVbenVxeGrid',
  17. ) as UseVbenVxeGrid;
  18. // 数据实例
  19. // const MOCK_TREE_TABLE_DATA = [
  20. // {
  21. // date: '2020-08-01',
  22. // id: 10_000,
  23. // name: 'Test1',
  24. // parentId: null,
  25. // size: 1024,
  26. // type: 'mp3',
  27. // },
  28. // ]
  29. const sleep = (time = 1000) => {
  30. return new Promise((resolve) => {
  31. setTimeout(() => {
  32. resolve(true);
  33. }, time);
  34. });
  35. };
  36. /**
  37. * 获取示例表格数据
  38. */
  39. async function getExampleTableApi(params: DemoTableApi.PageFetchParams) {
  40. return new Promise<{ items: any; total: number }>((resolve) => {
  41. const { page, pageSize } = params;
  42. const items = MOCK_API_DATA.slice((page - 1) * pageSize, page * pageSize);
  43. sleep(1000).then(() => {
  44. resolve({
  45. total: items.length,
  46. items,
  47. });
  48. });
  49. });
  50. }
  51. const gridOptions: VxeGridProps<RowType> = {
  52. checkboxConfig: {
  53. highlight: true,
  54. labelField: 'name',
  55. },
  56. columns: [
  57. { title: '序号', type: 'seq', width: 50 },
  58. { align: 'left', title: 'Name', type: 'checkbox', width: 100 },
  59. { field: 'category', title: 'Category' },
  60. { field: 'color', title: 'Color' },
  61. { field: 'productName', title: 'Product Name' },
  62. { field: 'price', title: 'Price' },
  63. { field: 'releaseDate', formatter: 'formatDateTime', title: 'DateTime' },
  64. ],
  65. exportConfig: {},
  66. // height: 'auto', // 如果设置为 auto,则必须确保存在父节点且不允许存在相邻元素,否则会出现高度闪动问题
  67. keepSource: true,
  68. proxyConfig: {
  69. ajax: {
  70. query: async ({ page }) => {
  71. return await getExampleTableApi({
  72. page: page.currentPage,
  73. pageSize: page.pageSize,
  74. });
  75. },
  76. },
  77. },
  78. toolbarConfig: {
  79. custom: true,
  80. export: true,
  81. // import: true,
  82. refresh: true,
  83. zoom: true,
  84. },
  85. };
  86. const [Grid, gridApi] = useVbenVxeGrid({
  87. gridOptions,
  88. });
  89. </script>
  90. <template>
  91. <div class="vp-raw w-full">
  92. <Grid>
  93. <template #toolbar-tools>
  94. <Button class="mr-2" type="primary" @click="() => gridApi.query()">
  95. 刷新当前页面
  96. </Button>
  97. <Button type="primary" @click="() => gridApi.reload()">
  98. 刷新并返回第一页
  99. </Button>
  100. </template>
  101. </Grid>
  102. </div>
  103. </template>