zhangwencui
7 天以前 f35825c1922149df154e3913d6dfebee57ee8cee
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
<script lang="ts" setup>
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { MesTmToolTypeApi } from '#/api/mes/tm/tool/type';
 
import { computed, onMounted, ref, watch } from 'vue';
 
import { IconifyIcon } from '@vben/icons';
 
import { Input } from 'ant-design-vue';
 
import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { getToolTypeSimpleList } from '#/api/mes/tm/tool/type';
 
defineOptions({ name: 'TmToolTypeList' });
 
const emit = defineEmits<{
  select: [row?: MesTmToolTypeApi.ToolType];
}>();
const selectedId = ref<number>(); // 当前选中工具类型编号
const filterText = ref(''); // 工具类型搜索关键字
const toolTypeList = ref<MesTmToolTypeApi.ToolType[]>([]); // 工具类型列表
const filteredList = computed(() =>
  filterText.value
    ? toolTypeList.value.filter((item) => item.name?.includes(filterText.value))
    : toolTypeList.value,
);
 
const [Grid, gridApi] = useVbenVxeGrid({
  gridOptions: {
    autoResize: true,
    border: false,
    columns: [{ field: 'name', title: '类型名称' }],
    data: [],
    height: 'auto',
    pagerConfig: {
      enabled: false,
    },
    rowConfig: {
      isCurrent: true,
      isHover: true,
      keyField: 'id',
    },
    showHeader: false,
    toolbarConfig: {
      enabled: false,
    },
  } as VxeTableGridOptions<MesTmToolTypeApi.ToolType>,
  gridEvents: {
    cellClick: ({ row }: { row: MesTmToolTypeApi.ToolType }) => {
      // 再次点击同一项:取消选中
      if (selectedId.value === row.id) {
        selectedId.value = undefined;
        gridApi.grid.clearCurrentRow();
        emit('select', undefined);
        return;
      }
      selectedId.value = row.id;
      emit('select', row);
    },
  },
});
 
/** 刷新工具类型列表 */
function refreshGridData() {
  gridApi.setGridOptions({ data: filteredList.value });
}
 
/** 加载工具类型列表 */
async function loadList() {
  toolTypeList.value = await getToolTypeSimpleList();
  refreshGridData();
}
 
/** 重置工具类型列表 */
function reset() {
  selectedId.value = undefined;
  filterText.value = '';
  gridApi.grid.clearCurrentRow();
  emit('select', undefined);
  refreshGridData();
}
 
watch(filterText, () => {
  refreshGridData();
});
 
onMounted(loadList);
 
defineExpose({ loadList, reset });
</script>
 
<template>
  <div class="h-full">
    <Input
      v-model:value="filterText"
      allow-clear
      class="mb-3 w-full"
      placeholder="搜索工具类型"
    >
      <template #prefix>
        <IconifyIcon class="size-4" icon="lucide:search" />
      </template>
    </Input>
    <Grid class="h-full" />
  </div>
</template>