2023-04-29 21:39:25 +08:00
|
|
|
|
<template>
|
|
|
|
|
<el-col v-for="(item, index) in attributeList" :key="index">
|
|
|
|
|
<div>
|
|
|
|
|
<el-text class="mx-1">属性名:</el-text>
|
2023-05-20 12:05:18 +08:00
|
|
|
|
<el-tag class="mx-1" closable type="success" @close="handleCloseProperty(index)"
|
|
|
|
|
>{{ item.name }}
|
|
|
|
|
</el-tag>
|
2023-04-29 21:39:25 +08:00
|
|
|
|
</div>
|
|
|
|
|
<div>
|
|
|
|
|
<el-text class="mx-1">属性值:</el-text>
|
|
|
|
|
<el-tag
|
2023-04-29 23:10:18 +08:00
|
|
|
|
v-for="(value, valueIndex) in item.values"
|
2023-04-30 17:04:31 +08:00
|
|
|
|
:key="value.id"
|
2023-04-29 21:39:25 +08:00
|
|
|
|
class="mx-1"
|
|
|
|
|
closable
|
2023-05-20 12:05:18 +08:00
|
|
|
|
@close="handleCloseValue(index, valueIndex)"
|
2023-04-29 21:39:25 +08:00
|
|
|
|
>
|
|
|
|
|
{{ value.name }}
|
|
|
|
|
</el-tag>
|
|
|
|
|
<el-input
|
2023-04-29 23:10:18 +08:00
|
|
|
|
v-show="inputVisible(index)"
|
2023-05-20 12:05:18 +08:00
|
|
|
|
:id="`input${index}`"
|
|
|
|
|
:ref="setInputRef"
|
2023-04-29 21:39:25 +08:00
|
|
|
|
v-model="inputValue"
|
|
|
|
|
class="!w-20"
|
|
|
|
|
size="small"
|
2023-04-30 17:04:31 +08:00
|
|
|
|
@blur="handleInputConfirm(index, item.id)"
|
|
|
|
|
@keyup.enter="handleInputConfirm(index, item.id)"
|
2023-04-29 21:39:25 +08:00
|
|
|
|
/>
|
2023-04-29 23:10:18 +08:00
|
|
|
|
<el-button
|
|
|
|
|
v-show="!inputVisible(index)"
|
|
|
|
|
class="button-new-tag ml-1"
|
|
|
|
|
size="small"
|
|
|
|
|
@click="showInput(index)"
|
|
|
|
|
>
|
2023-04-29 21:39:25 +08:00
|
|
|
|
+ 添加
|
|
|
|
|
</el-button>
|
|
|
|
|
</div>
|
|
|
|
|
<el-divider class="my-10px" />
|
|
|
|
|
</el-col>
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<script lang="ts" name="ProductAttributes" setup>
|
|
|
|
|
import { ElInput } from 'element-plus'
|
2023-04-30 17:04:31 +08:00
|
|
|
|
import * as PropertyApi from '@/api/mall/product/property'
|
2023-04-29 21:39:25 +08:00
|
|
|
|
|
2023-04-30 17:04:31 +08:00
|
|
|
|
const { t } = useI18n() // 国际化
|
|
|
|
|
const message = useMessage() // 消息弹窗
|
2023-04-29 21:39:25 +08:00
|
|
|
|
const inputValue = ref('') // 输入框值
|
2023-04-29 23:10:18 +08:00
|
|
|
|
const attributeIndex = ref<number | null>(null) // 获取焦点时记录当前属性项的index
|
|
|
|
|
// 输入框显隐控制
|
|
|
|
|
const inputVisible = computed(() => (index) => {
|
|
|
|
|
if (attributeIndex.value === null) return false
|
|
|
|
|
if (attributeIndex.value === index) return true
|
|
|
|
|
})
|
2023-05-20 12:05:18 +08:00
|
|
|
|
const inputRef = ref([]) //标签输入框Ref
|
|
|
|
|
/** 解决 ref 在 v-for 中的获取问题*/
|
|
|
|
|
const setInputRef = (el) => {
|
|
|
|
|
if (el === null || typeof el === 'undefined') return
|
|
|
|
|
// 如果不存在id相同的元素才添加
|
|
|
|
|
if (!inputRef.value.some((item) => item.input?.attributes.id === el.input?.attributes.id)) {
|
|
|
|
|
inputRef.value.push(el)
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-30 02:26:35 +08:00
|
|
|
|
const attributeList = ref([]) // 商品属性列表
|
2023-04-29 21:39:25 +08:00
|
|
|
|
const props = defineProps({
|
2023-05-17 18:24:34 +08:00
|
|
|
|
propertyList: {
|
2023-04-30 02:26:35 +08:00
|
|
|
|
type: Array,
|
2023-04-29 21:39:25 +08:00
|
|
|
|
default: () => {}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
watch(
|
2023-05-17 18:24:34 +08:00
|
|
|
|
() => props.propertyList,
|
2023-04-29 21:39:25 +08:00
|
|
|
|
(data) => {
|
|
|
|
|
if (!data) return
|
|
|
|
|
attributeList.value = data
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
deep: true,
|
|
|
|
|
immediate: true
|
|
|
|
|
}
|
|
|
|
|
)
|
2023-05-06 22:54:41 +08:00
|
|
|
|
|
2023-05-20 12:05:18 +08:00
|
|
|
|
/** 删除属性值*/
|
|
|
|
|
const handleCloseValue = (index, valueIndex) => {
|
2023-04-29 23:10:18 +08:00
|
|
|
|
attributeList.value[index].values?.splice(valueIndex, 1)
|
2023-04-29 21:39:25 +08:00
|
|
|
|
}
|
2023-05-20 12:05:18 +08:00
|
|
|
|
/** 删除属性*/
|
|
|
|
|
const handleCloseProperty = (index) => {
|
|
|
|
|
attributeList.value?.splice(index, 1)
|
|
|
|
|
}
|
2023-04-29 21:39:25 +08:00
|
|
|
|
/** 显示输入框并获取焦点 */
|
2023-04-29 23:10:18 +08:00
|
|
|
|
const showInput = async (index) => {
|
|
|
|
|
attributeIndex.value = index
|
2023-05-20 12:05:18 +08:00
|
|
|
|
inputRef.value[index].focus()
|
2023-04-29 21:39:25 +08:00
|
|
|
|
}
|
2023-05-06 22:54:41 +08:00
|
|
|
|
|
2023-05-20 12:05:18 +08:00
|
|
|
|
const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调
|
|
|
|
|
|
2023-04-29 21:39:25 +08:00
|
|
|
|
/** 输入框失去焦点或点击回车时触发 */
|
2023-04-30 17:04:31 +08:00
|
|
|
|
const handleInputConfirm = async (index, propertyId) => {
|
2023-04-29 21:39:25 +08:00
|
|
|
|
if (inputValue.value) {
|
2023-04-30 17:04:31 +08:00
|
|
|
|
// 保存属性值
|
|
|
|
|
try {
|
|
|
|
|
const id = await PropertyApi.createPropertyValue({ propertyId, name: inputValue.value })
|
|
|
|
|
attributeList.value[index].values.push({ id, name: inputValue.value })
|
|
|
|
|
message.success(t('common.createSuccess'))
|
2023-05-20 12:05:18 +08:00
|
|
|
|
emit('success', attributeList.value)
|
2023-04-30 17:04:31 +08:00
|
|
|
|
} catch {
|
|
|
|
|
message.error('添加失败,请重试') // TODO 缺少国际化
|
|
|
|
|
}
|
2023-04-29 21:39:25 +08:00
|
|
|
|
}
|
2023-04-29 23:10:18 +08:00
|
|
|
|
attributeIndex.value = null
|
2023-04-29 21:39:25 +08:00
|
|
|
|
inputValue.value = ''
|
|
|
|
|
}
|
|
|
|
|
</script>
|