✨ feat(mes): 新增物料产品选择器 V2 组件
parent
1313347c85
commit
9f9ac51edf
|
|
@ -0,0 +1,272 @@
|
||||||
|
<!--
|
||||||
|
MES 物料产品弹窗选择器 V2(支持单选/多选)
|
||||||
|
|
||||||
|
Props:
|
||||||
|
multiple — true 多选(checkbox),false 单选(radio);默认 true
|
||||||
|
Events:
|
||||||
|
selected(rows: MdItemVO[]) — 确认选择后触发,单选时数组长度为 1
|
||||||
|
Expose:
|
||||||
|
open(selectedIds?: number[]) — 打开弹窗,可传入已选 ID 用于预选高亮
|
||||||
|
-->
|
||||||
|
<template>
|
||||||
|
<Dialog title="物料产品选择" v-model="dialogVisible" width="80%">
|
||||||
|
<el-row :gutter="20">
|
||||||
|
<!-- 左侧:分类树 -->
|
||||||
|
<el-col :span="5">
|
||||||
|
<MdItemTypeTree ref="typeTreeRef" @node-click="handleNodeClick" />
|
||||||
|
</el-col>
|
||||||
|
<!-- 右侧:物料表格 -->
|
||||||
|
<el-col :span="19">
|
||||||
|
<!-- 搜索表单 -->
|
||||||
|
<el-form :inline="true" :model="queryParams" class="mb-10px" label-width="80px">
|
||||||
|
<el-form-item label="物料编码">
|
||||||
|
<el-input
|
||||||
|
v-model="queryParams.code"
|
||||||
|
placeholder="请输入物料编码"
|
||||||
|
clearable
|
||||||
|
@keyup.enter="handleQuery"
|
||||||
|
/>
|
||||||
|
</el-form-item>
|
||||||
|
<el-form-item label="物料名称">
|
||||||
|
<el-input
|
||||||
|
v-model="queryParams.name"
|
||||||
|
placeholder="请输入物料名称"
|
||||||
|
clearable
|
||||||
|
@keyup.enter="handleQuery"
|
||||||
|
/>
|
||||||
|
</el-form-item>
|
||||||
|
<el-form-item>
|
||||||
|
<el-button type="primary" @click="handleQuery">
|
||||||
|
<Icon icon="ep:search" class="mr-5px" /> 搜索
|
||||||
|
</el-button>
|
||||||
|
<el-button @click="resetQuery">
|
||||||
|
<Icon icon="ep:refresh" class="mr-5px" /> 重置
|
||||||
|
</el-button>
|
||||||
|
</el-form-item>
|
||||||
|
</el-form>
|
||||||
|
<!-- 数据表格:单选 radio / 多选 checkbox 统一在一个 table 内 -->
|
||||||
|
<el-table
|
||||||
|
ref="tableRef"
|
||||||
|
v-loading="loading"
|
||||||
|
:data="list"
|
||||||
|
:stripe="true"
|
||||||
|
:show-overflow-tooltip="true"
|
||||||
|
border
|
||||||
|
:highlight-current-row="!multiple"
|
||||||
|
@selection-change="handleSelectionChange"
|
||||||
|
@row-dblclick="handleRowDblClick"
|
||||||
|
>
|
||||||
|
<!-- 多选:checkbox -->
|
||||||
|
<el-table-column v-if="multiple" type="selection" width="50" align="center" />
|
||||||
|
<!-- 单选:radio -->
|
||||||
|
<el-table-column v-else width="50" align="center">
|
||||||
|
<template #default="{ row }">
|
||||||
|
<el-radio
|
||||||
|
v-model="selectedRadioId"
|
||||||
|
:value="row.id"
|
||||||
|
class="radio-no-label"
|
||||||
|
@change="handleRadioChange(row)"
|
||||||
|
/>
|
||||||
|
</template>
|
||||||
|
</el-table-column>
|
||||||
|
<el-table-column label="物料编码" align="center" prop="code" width="200" />
|
||||||
|
<el-table-column label="物料名称" align="left" prop="name" min-width="150" />
|
||||||
|
<el-table-column label="规格型号" align="left" prop="specification" min-width="120" />
|
||||||
|
<el-table-column label="单位" align="center" prop="unitMeasureName" width="80" />
|
||||||
|
<el-table-column label="物料/产品" align="center" prop="itemOrProduct" width="100">
|
||||||
|
<template #default="scope">
|
||||||
|
<dict-tag :type="DICT_TYPE.MES_MD_ITEM_OR_PRODUCT" :value="scope.row.itemOrProduct" />
|
||||||
|
</template>
|
||||||
|
</el-table-column>
|
||||||
|
<el-table-column label="所属分类" align="center" prop="itemTypeName" width="120" />
|
||||||
|
<el-table-column
|
||||||
|
label="创建时间"
|
||||||
|
align="center"
|
||||||
|
prop="createTime"
|
||||||
|
:formatter="dateFormatter"
|
||||||
|
width="180"
|
||||||
|
/>
|
||||||
|
</el-table>
|
||||||
|
<!-- 分页 -->
|
||||||
|
<Pagination
|
||||||
|
:total="total"
|
||||||
|
v-model:page="queryParams.pageNo"
|
||||||
|
v-model:limit="queryParams.pageSize"
|
||||||
|
@pagination="getList"
|
||||||
|
/>
|
||||||
|
</el-col>
|
||||||
|
</el-row>
|
||||||
|
<template #footer>
|
||||||
|
<el-button type="primary" @click="confirmSelect">确 定</el-button>
|
||||||
|
<el-button @click="dialogVisible = false">取 消</el-button>
|
||||||
|
</template>
|
||||||
|
</Dialog>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup lang="ts">
|
||||||
|
import { DICT_TYPE } from '@/utils/dict'
|
||||||
|
import { dateFormatter } from '@/utils/formatTime'
|
||||||
|
import { MdItemApi, MdItemVO } from '@/api/mes/md/item'
|
||||||
|
import { MdItemTypeVO } from '@/api/mes/md/item/type'
|
||||||
|
import MdItemTypeTree from '@/views/mes/md/item/type/components/MdItemTypeTree.vue'
|
||||||
|
|
||||||
|
defineOptions({ name: 'MdItemSelectDialogV2' })
|
||||||
|
|
||||||
|
const props = withDefaults(
|
||||||
|
defineProps<{
|
||||||
|
multiple?: boolean // true 多选(checkbox),false 单选(radio)
|
||||||
|
}>(),
|
||||||
|
{
|
||||||
|
multiple: true
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
const message = useMessage()
|
||||||
|
const emit = defineEmits<{
|
||||||
|
selected: [rows: MdItemVO[]]
|
||||||
|
}>()
|
||||||
|
|
||||||
|
const dialogVisible = ref(false) // 弹窗是否展示
|
||||||
|
const loading = ref(false) // 列表加载中
|
||||||
|
const list = ref<MdItemVO[]>([]) // 物料列表
|
||||||
|
const total = ref(0) // 总条数
|
||||||
|
|
||||||
|
// ==================== 选中状态 ====================
|
||||||
|
const tableRef = ref() // 表格 Ref
|
||||||
|
const typeTreeRef = ref() // 分类树 Ref
|
||||||
|
const selectedRows = ref<MdItemVO[]>([]) // 多选模式:选中行
|
||||||
|
const selectedRadioId = ref<number>() // 单选模式:选中 ID
|
||||||
|
const currentRadioRow = ref<MdItemVO>() // 单选模式:选中行对象
|
||||||
|
const preSelectedIds = ref<number[]>([]) // 打开弹窗时传入的已选 ID
|
||||||
|
|
||||||
|
/** 多选:checkbox 变化 */
|
||||||
|
const handleSelectionChange = (rows: MdItemVO[]) => {
|
||||||
|
if (props.multiple) {
|
||||||
|
selectedRows.value = rows
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 单选:radio 变化 */
|
||||||
|
const handleRadioChange = (row: MdItemVO) => {
|
||||||
|
currentRadioRow.value = row
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 双击行:单选模式直接确认 */
|
||||||
|
const handleRowDblClick = (row: MdItemVO) => {
|
||||||
|
if (props.multiple) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
selectedRadioId.value = row.id
|
||||||
|
currentRadioRow.value = row
|
||||||
|
confirmSelect()
|
||||||
|
}
|
||||||
|
|
||||||
|
// ==================== 分类树 ====================
|
||||||
|
|
||||||
|
/** 点击分类树节点,按分类筛选 */
|
||||||
|
const handleNodeClick = (data: MdItemTypeVO) => {
|
||||||
|
queryParams.itemTypeId = data.id
|
||||||
|
handleQuery()
|
||||||
|
}
|
||||||
|
|
||||||
|
// ==================== 物料查询 ====================
|
||||||
|
const queryParams = reactive({
|
||||||
|
pageNo: 1, // 页码
|
||||||
|
pageSize: 10, // 每页条数
|
||||||
|
code: undefined as string | undefined, // 物料编码
|
||||||
|
name: undefined as string | undefined, // 物料名称
|
||||||
|
itemTypeId: undefined as number | undefined // 物料分类编号
|
||||||
|
})
|
||||||
|
|
||||||
|
/** 查询物料列表 */
|
||||||
|
const getList = async () => {
|
||||||
|
loading.value = true
|
||||||
|
try {
|
||||||
|
const data = await MdItemApi.getItemPage(queryParams)
|
||||||
|
list.value = data.list
|
||||||
|
total.value = data.total
|
||||||
|
await nextTick()
|
||||||
|
applyPreSelection()
|
||||||
|
} finally {
|
||||||
|
loading.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 恢复预选状态(当前页可见范围内) */
|
||||||
|
const applyPreSelection = () => {
|
||||||
|
if (preSelectedIds.value.length === 0) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (props.multiple) {
|
||||||
|
const table = tableRef.value
|
||||||
|
if (!table) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
list.value.forEach((row) => {
|
||||||
|
if (preSelectedIds.value.includes(row.id)) {
|
||||||
|
table.toggleRowSelection(row, true)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
const match = list.value.find((row) => preSelectedIds.value.includes(row.id))
|
||||||
|
if (match) {
|
||||||
|
selectedRadioId.value = match.id
|
||||||
|
currentRadioRow.value = match
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 搜索按钮操作 */
|
||||||
|
const handleQuery = () => {
|
||||||
|
queryParams.pageNo = 1
|
||||||
|
getList()
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 重置查询条件 */
|
||||||
|
const resetQuery = () => {
|
||||||
|
queryParams.code = undefined
|
||||||
|
queryParams.name = undefined
|
||||||
|
queryParams.itemTypeId = undefined
|
||||||
|
handleQuery()
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 确认选择 */
|
||||||
|
const confirmSelect = () => {
|
||||||
|
if (props.multiple) {
|
||||||
|
if (selectedRows.value.length === 0) {
|
||||||
|
message.warning('请至少选择一条数据')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
emit('selected', selectedRows.value)
|
||||||
|
} else {
|
||||||
|
if (!currentRadioRow.value) {
|
||||||
|
message.warning('请选择一条数据')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
emit('selected', [currentRadioRow.value])
|
||||||
|
}
|
||||||
|
dialogVisible.value = false
|
||||||
|
}
|
||||||
|
|
||||||
|
// ==================== 打开弹窗 ====================
|
||||||
|
|
||||||
|
/** 打开弹窗,可传入已选 ID 用于预选高亮 */
|
||||||
|
const open = async (selectedIds?: number[]) => {
|
||||||
|
dialogVisible.value = true
|
||||||
|
selectedRows.value = []
|
||||||
|
selectedRadioId.value = undefined
|
||||||
|
currentRadioRow.value = undefined
|
||||||
|
preSelectedIds.value = selectedIds ?? []
|
||||||
|
await getList()
|
||||||
|
}
|
||||||
|
defineExpose({ open })
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<style lang="scss" scoped>
|
||||||
|
/* 隐藏 radio 的 label 文字,只保留圆圈 */
|
||||||
|
.radio-no-label {
|
||||||
|
:deep(.el-radio__label) {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
|
@ -0,0 +1,159 @@
|
||||||
|
<!--
|
||||||
|
MES 物料产品选择器 V2:只读输入框 + 点击弹窗选择
|
||||||
|
|
||||||
|
交互:显示为只读 el-input,点击打开弹窗(单选模式)进行选择
|
||||||
|
Props:
|
||||||
|
modelValue — 绑定的物料 ID(v-model)
|
||||||
|
disabled — 是否禁用
|
||||||
|
clearable — 是否允许清空(鼠标悬停时显示清除图标)
|
||||||
|
placeholder — 占位文字
|
||||||
|
Events:
|
||||||
|
update:modelValue — v-model 更新
|
||||||
|
change(item) — 选中物料变化时触发,传递完整 MdItemVO(清空时为 undefined)
|
||||||
|
-->
|
||||||
|
<template>
|
||||||
|
<div
|
||||||
|
class="w-full"
|
||||||
|
:class="disabled ? 'cursor-not-allowed' : 'cursor-pointer'"
|
||||||
|
@click="handleClick"
|
||||||
|
@mouseenter="hovering = true"
|
||||||
|
@mouseleave="hovering = false"
|
||||||
|
>
|
||||||
|
<el-input
|
||||||
|
ref="inputRef"
|
||||||
|
:model-value="displayLabel"
|
||||||
|
:placeholder="placeholder"
|
||||||
|
:disabled="disabled"
|
||||||
|
readonly
|
||||||
|
:suffix-icon="suffixIcon"
|
||||||
|
:class="disabled ? 'is-select-disabled' : 'is-select-clickable'"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<!-- 弹窗选择器(单选模式) -->
|
||||||
|
<MdItemSelectDialogV2 ref="dialogRef" :multiple="false" @selected="handleSelected" />
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup lang="ts">
|
||||||
|
import { MdItemApi, MdItemVO } from '@/api/mes/md/item'
|
||||||
|
import { Search, CircleClose } from '@element-plus/icons-vue'
|
||||||
|
import MdItemSelectDialogV2 from './MdItemSelectDialogV2.vue'
|
||||||
|
|
||||||
|
defineOptions({ name: 'MdItemSelectV2' })
|
||||||
|
|
||||||
|
const props = withDefaults(
|
||||||
|
defineProps<{
|
||||||
|
modelValue?: number // 绑定的物料 ID
|
||||||
|
disabled?: boolean // 是否禁用
|
||||||
|
clearable?: boolean // 是否允许清空
|
||||||
|
placeholder?: string // 占位文字
|
||||||
|
}>(),
|
||||||
|
{
|
||||||
|
disabled: false,
|
||||||
|
clearable: true,
|
||||||
|
placeholder: '请选择产品物料'
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
const emit = defineEmits<{
|
||||||
|
'update:modelValue': [value: number | undefined]
|
||||||
|
change: [item: MdItemVO | undefined]
|
||||||
|
}>()
|
||||||
|
|
||||||
|
const dialogRef = ref() // 弹窗 Ref
|
||||||
|
const inputRef = ref() // 输入框 Ref
|
||||||
|
const hovering = ref(false) // 鼠标是否悬停
|
||||||
|
|
||||||
|
// ==================== 名称回显 ====================
|
||||||
|
const selectedItem = ref<MdItemVO | undefined>() // 当前选中的物料对象
|
||||||
|
const resolving = ref(false) // 是否正在查询回显
|
||||||
|
|
||||||
|
/** 显示文本:物料名称 */
|
||||||
|
const displayLabel = computed(() => {
|
||||||
|
return selectedItem.value?.name ?? ''
|
||||||
|
})
|
||||||
|
|
||||||
|
/** 是否显示清除图标 */
|
||||||
|
const showClear = computed(() => {
|
||||||
|
return props.clearable && !props.disabled && hovering.value && props.modelValue != null
|
||||||
|
})
|
||||||
|
|
||||||
|
/** 后缀图标:悬停且有值时显示清除,否则显示搜索 */
|
||||||
|
const suffixIcon = computed(() => {
|
||||||
|
return showClear.value ? CircleClose : Search
|
||||||
|
})
|
||||||
|
|
||||||
|
/** 根据 ID 单条查询物料信息(用于编辑回显) */
|
||||||
|
const resolveItemById = async (id: number | undefined) => {
|
||||||
|
if (id == null) {
|
||||||
|
selectedItem.value = undefined
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (selectedItem.value?.id === id) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
resolving.value = true
|
||||||
|
try {
|
||||||
|
selectedItem.value = await MdItemApi.getItem(id)
|
||||||
|
} finally {
|
||||||
|
resolving.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 监听 modelValue 变化,触发回显 */
|
||||||
|
watch(
|
||||||
|
() => props.modelValue,
|
||||||
|
(val) => {
|
||||||
|
resolveItemById(val)
|
||||||
|
},
|
||||||
|
{ immediate: true }
|
||||||
|
)
|
||||||
|
|
||||||
|
// ==================== 点击交互 ====================
|
||||||
|
|
||||||
|
/** 点击组件:清除或打开弹窗 */
|
||||||
|
const handleClick = (e: MouseEvent) => {
|
||||||
|
if (props.disabled) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// 点击清除图标:清空选中
|
||||||
|
const target = e.target as HTMLElement
|
||||||
|
if (showClear.value && target.closest('.el-input__suffix')) {
|
||||||
|
e.stopPropagation()
|
||||||
|
selectedItem.value = undefined
|
||||||
|
emit('update:modelValue', undefined)
|
||||||
|
emit('change', undefined)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// 打开弹窗,传入当前选中 ID 用于预选高亮
|
||||||
|
const selectedIds = props.modelValue != null ? [props.modelValue] : []
|
||||||
|
dialogRef.value.open(selectedIds)
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 弹窗选中回调 */
|
||||||
|
const handleSelected = (rows: MdItemVO[]) => {
|
||||||
|
if (!rows || rows.length === 0) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
const item = rows[0]
|
||||||
|
selectedItem.value = item
|
||||||
|
emit('update:modelValue', item.id)
|
||||||
|
emit('change', item)
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<style lang="scss" scoped>
|
||||||
|
/* :deep 用于穿透 el-input 内部元素的 cursor 样式,UnoCSS 无法直接处理组件内部 DOM */
|
||||||
|
.is-select-clickable {
|
||||||
|
:deep(.el-input__wrapper),
|
||||||
|
:deep(.el-input__inner) {
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
.is-select-disabled {
|
||||||
|
:deep(.el-input__wrapper),
|
||||||
|
:deep(.el-input__inner) {
|
||||||
|
cursor: not-allowed;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</style>
|
||||||
Loading…
Reference in New Issue