Files
ipms-sjy-ui/src/views/mall/product/management/components/ProductAttributes.vue

100 lines
2.8 KiB
Vue
Raw Normal View History

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>
<el-text class="mx-1">{{ item.name }}</el-text>
</div>
<div>
<el-text class="mx-1">属性值</el-text>
<el-tag
v-for="(value, valueIndex) in item.values"
:key="value.id"
2023-04-29 21:39:25 +08:00
:disable-transitions="false"
class="mx-1"
closable
@close="handleClose(index, valueIndex)"
>
{{ value.name }}
</el-tag>
<el-input
v-show="inputVisible(index)"
2023-04-29 21:39:25 +08:00
ref="InputRef"
v-model="inputValue"
class="!w-20"
size="small"
@blur="handleInputConfirm(index, item.id)"
@keyup.enter="handleInputConfirm(index, item.id)"
2023-04-29 21:39:25 +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'
import * as PropertyApi from '@/api/mall/product/property'
2023-04-29 21:39:25 +08:00
const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗
2023-04-29 21:39:25 +08:00
const inputValue = ref('') // 输入框值
const attributeIndex = ref<number | null>(null) // 获取焦点时记录当前属性项的index
// 输入框显隐控制
const inputVisible = computed(() => (index) => {
if (attributeIndex.value === null) return false
if (attributeIndex.value === index) return true
})
const InputRef = ref() //标签输入框Ref
const attributeList = ref([]) // 商品属性列表
2023-04-29 21:39:25 +08:00
const props = defineProps({
attributeData: {
type: Array,
2023-04-29 21:39:25 +08:00
default: () => {}
}
})
watch(
() => props.attributeData,
(data) => {
if (!data) return
attributeList.value = data
},
{
deep: true,
immediate: true
}
)
/** 删除标签 tagValue 标签值*/
const handleClose = (index, valueIndex) => {
attributeList.value[index].values?.splice(valueIndex, 1)
2023-04-29 21:39:25 +08:00
}
/** 显示输入框并获取焦点 */
const showInput = async (index) => {
attributeIndex.value = index
// 因为组件在ref中所以需要用索引获取对应的Ref
InputRef.value[index]!.input!.focus()
2023-04-29 21:39:25 +08:00
}
/** 输入框失去焦点或点击回车时触发 */
const handleInputConfirm = async (index, propertyId) => {
2023-04-29 21:39:25 +08:00
if (inputValue.value) {
// 保存属性值
try {
const id = await PropertyApi.createPropertyValue({ propertyId, name: inputValue.value })
attributeList.value[index].values.push({ id, name: inputValue.value })
message.success(t('common.createSuccess'))
} catch {
message.error('添加失败,请重试') // TODO 缺少国际化
}
2023-04-29 21:39:25 +08:00
}
attributeIndex.value = null
2023-04-29 21:39:25 +08:00
inputValue.value = ''
}
</script>