gaoluyang
2 天以前 b64a0deae5b5d33f9e20671a68936b27f0b9b00b
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<script lang="ts" setup>
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { MesMdProductBomApi } from '#/api/mes/md/item/productBom';
 
import { nextTick, ref } from 'vue';
 
import { message, Modal } from 'ant-design-vue';
 
import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { getProductBomListByItemId } from '#/api/mes/md/item/productBom';
 
import { useProductBomGridColumns } from '../data';
 
defineOptions({ name: 'MdProductBomSelectDialog' });
 
const emit = defineEmits<{
  selected: [row: MesMdProductBomApi.ProductBom];
}>();
 
const open = ref(false); // 弹窗是否打开
const list = ref<MesMdProductBomApi.ProductBom[]>([]); // BOM 子物料列表
const selectedRow = ref<MesMdProductBomApi.ProductBom>(); // 当前选中的 BOM 行
 
const [Grid, gridApi] = useVbenVxeGrid({
  gridOptions: {
    border: true,
    columns: [
      { type: 'radio', width: 55, align: 'center' },
      ...(useProductBomGridColumns(true) || []),
    ],
    data: list.value,
    height: 500,
    keepSource: true,
    pagerConfig: {
      enabled: false,
    },
    radioConfig: {
      highlight: true,
      trigger: 'row',
    },
    rowConfig: {
      keyField: 'bomItemId',
      isHover: true,
    },
    showOverflow: true,
    toolbarConfig: {
      enabled: false,
    },
  } as VxeTableGridOptions<MesMdProductBomApi.ProductBom>,
  gridEvents: {
    cellDblclick: ({ row }: { row: MesMdProductBomApi.ProductBom }) => {
      selectedRow.value = row;
      gridApi.grid.setRadioRow(row);
      handleConfirm();
    },
    radioChange: ({ row }: { row: MesMdProductBomApi.ProductBom }) => {
      selectedRow.value = row;
    },
  },
});
 
/** 打开 BOM 物料选择弹窗 */
async function openModal(itemId: number, selectedBomItemId?: number) {
  open.value = true;
  selectedRow.value = undefined;
  gridApi.setLoading(true);
  try {
    list.value = await getProductBomListByItemId(itemId);
    gridApi.setGridOptions({ data: list.value });
    await nextTick();
    if (selectedBomItemId !== null) {
      const match = list.value.find(
        (row) => row.bomItemId === selectedBomItemId,
      );
      if (match) {
        selectedRow.value = match;
        gridApi.grid.setRadioRow(match);
      }
    }
  } finally {
    gridApi.setLoading(false);
  }
}
 
/** 关闭 BOM 物料选择弹窗 */
async function closeModal() {
  open.value = false;
  selectedRow.value = undefined;
  await gridApi.grid.clearRadioRow();
}
 
/** 确认选择 BOM 物料 */
function handleConfirm() {
  if (!selectedRow.value) {
    message.warning('请选择一条数据');
    return;
  }
  emit('selected', selectedRow.value);
  open.value = false;
}
 
defineExpose({ open: openModal });
</script>
 
<template>
  <Modal
    v-model:open="open"
    title="产品 BOM 物料选择"
    width="800px"
    :destroy-on-close="true"
    @ok="handleConfirm"
    @cancel="closeModal"
  >
    <Grid table-title="BOM 子物料列表" />
  </Modal>
</template>