index.vue 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <script lang="ts" setup>
  2. import type { VxeGridProps } from '#/adapter/vxe-table';
  3. import { Button, message } from 'ant-design-vue';
  4. import { useVbenVxeGrid } from '#/adapter/vxe-table';
  5. import { getExampleTableApi } from '../mock-api';
  6. interface RowType {
  7. category: string;
  8. color: string;
  9. id: string;
  10. price: string;
  11. productName: string;
  12. releaseDate: string;
  13. }
  14. const gridOptions: VxeGridProps<RowType> = {
  15. columns: [
  16. { title: '序号', type: 'seq', width: 50 },
  17. { editRender: { name: 'input' }, field: 'category', title: 'Category' },
  18. { editRender: { name: 'input' }, field: 'color', title: 'Color' },
  19. {
  20. editRender: { name: 'input' },
  21. field: 'productName',
  22. title: 'Product Name',
  23. },
  24. { field: 'price', title: 'Price' },
  25. { field: 'releaseDate', formatter: 'formatDateTime', title: 'Date' },
  26. { slots: { default: 'action' }, title: '操作' },
  27. ],
  28. editConfig: {
  29. mode: 'row',
  30. trigger: 'click',
  31. },
  32. pagerConfig: {},
  33. proxyConfig: {
  34. ajax: {
  35. query: async ({ page }) => {
  36. return await getExampleTableApi({
  37. page: page.currentPage,
  38. pageSize: page.pageSize,
  39. });
  40. },
  41. },
  42. },
  43. showOverflow: true,
  44. };
  45. const [Grid, gridApi] = useVbenVxeGrid({ gridOptions });
  46. function hasEditStatus(row: RowType) {
  47. return gridApi.grid?.isEditByRow(row);
  48. }
  49. function editRowEvent(row: RowType) {
  50. gridApi.grid?.setEditRow(row);
  51. }
  52. async function saveRowEvent(row: RowType) {
  53. await gridApi.grid?.clearEdit();
  54. gridApi.setLoading(true);
  55. setTimeout(() => {
  56. gridApi.setLoading(false);
  57. message.success({
  58. content: `保存成功!category=${row.category}`,
  59. });
  60. }, 600);
  61. }
  62. const cancelRowEvent = (_row: RowType) => {
  63. gridApi.grid?.clearEdit();
  64. };
  65. </script>
  66. <template>
  67. <div class="vp-raw w-full">
  68. <Grid>
  69. <template #action="{ row }">
  70. <template v-if="hasEditStatus(row)">
  71. <Button type="link" @click="saveRowEvent(row)">保存</Button>
  72. <Button type="link" @click="cancelRowEvent(row)">取消</Button>
  73. </template>
  74. <template v-else>
  75. <Button type="link" @click="editRowEvent(row)">编辑</Button>
  76. </template>
  77. </template>
  78. </Grid>
  79. </div>
  80. </template>