商品管理: 保存属性值到数据库

This commit is contained in:
puhui999 2023-04-30 17:04:31 +08:00
parent 538d1e0b6c
commit 35c3545e7c

View File

@ -8,7 +8,7 @@
<el-text class="mx-1">属性值</el-text> <el-text class="mx-1">属性值</el-text>
<el-tag <el-tag
v-for="(value, valueIndex) in item.values" v-for="(value, valueIndex) in item.values"
:key="value.name" :key="value.id"
:disable-transitions="false" :disable-transitions="false"
class="mx-1" class="mx-1"
closable closable
@ -22,8 +22,8 @@
v-model="inputValue" v-model="inputValue"
class="!w-20" class="!w-20"
size="small" size="small"
@blur="handleInputConfirm(index)" @blur="handleInputConfirm(index, item.id)"
@keyup.enter="handleInputConfirm(index)" @keyup.enter="handleInputConfirm(index, item.id)"
/> />
<el-button <el-button
v-show="!inputVisible(index)" v-show="!inputVisible(index)"
@ -40,7 +40,10 @@
<script lang="ts" name="ProductAttributes" setup> <script lang="ts" name="ProductAttributes" setup>
import { ElInput } from 'element-plus' import { ElInput } from 'element-plus'
import * as PropertyApi from '@/api/mall/product/property'
const { t } = useI18n() //
const message = useMessage() //
const inputValue = ref('') // const inputValue = ref('') //
const attributeIndex = ref<number | null>(null) // index const attributeIndex = ref<number | null>(null) // index
// //
@ -79,9 +82,16 @@ const showInput = async (index) => {
InputRef.value[index]!.input!.focus() InputRef.value[index]!.input!.focus()
} }
/** 输入框失去焦点或点击回车时触发 */ /** 输入框失去焦点或点击回车时触发 */
const handleInputConfirm = (index) => { const handleInputConfirm = async (index, propertyId) => {
if (inputValue.value) { if (inputValue.value) {
attributeList.value[index].values.push({ name: 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
}
} }
attributeIndex.value = null attributeIndex.value = null
inputValue.value = '' inputValue.value = ''