gaoluyang
2026-06-24 712aa51536236d43e87273e4ce45ac5691dffad8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<!-- SKU 选择弹窗组件 -->
<script lang="ts" setup>
import type { MallSpuApi } from '#/api/mall/product/spu';
 
import { ref } from 'vue';
 
import { Modal } from 'ant-design-vue';
 
import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { getSpu } from '#/api/mall/product/spu';
 
import { useSkuGridColumns } from './spu-select-data';
 
interface SpuData {
  spuId: number;
}
 
const emit = defineEmits<{
  change: [sku: MallSpuApi.Sku];
}>();
 
const visible = ref(false);
const spuId = ref<number>();
 
const [Grid, gridApi] = useVbenVxeGrid({
  gridOptions: {
    columns: useSkuGridColumns(),
    height: 400,
    border: true,
    radioConfig: {
      reserve: true,
      highlight: true,
    },
    rowConfig: {
      keyField: 'id',
      isHover: true,
    },
    pagerConfig: {
      enabled: false,
    },
  },
  gridEvents: {
    radioChange: () => {
      const selectedRow = gridApi.grid.getRadioRecord() as MallSpuApi.Sku;
      if (selectedRow) {
        emit('change', selectedRow);
        // 关闭弹窗
        visible.value = false;
        gridApi.grid.clearRadioRow();
        spuId.value = undefined;
      }
    },
  },
});
 
/** 关闭弹窗 */
function closeModal() {
  visible.value = false;
  spuId.value = undefined;
}
 
/** 打开弹窗 */
async function openModal(data?: SpuData) {
  if (!data?.spuId) {
    return;
  }
  spuId.value = data.spuId;
  visible.value = true;
  // 注意:useVbenVxeGrid 关闭分页(pagerConfig.enabled=false)后,proxyConfig.ajax.query 的结果不会传递到 vxe-table
  // 需要手动调用 reloadData 设置表格数据
  if (!spuId.value) {
    gridApi.grid?.reloadData([]);
    return;
  }
  const spu = await getSpu(spuId.value);
  gridApi.grid?.reloadData(spu.skus || []);
}
 
/** 对外暴露的方法 */
defineExpose({
  open: openModal,
});
</script>
 
<template>
  <Modal
    v-model:open="visible"
    title="选择规格"
    width="700px"
    :destroy-on-close="true"
    :footer="null"
    @cancel="closeModal"
  >
    <Grid />
  </Modal>
</template>