Skip to content

Commit 35c3545

Browse files
author
puhui999
committed
商品管理: 保存属性值到数据库
1 parent 538d1e0 commit 35c3545

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

src/views/mall/product/management/components/ProductAttributes.vue

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<el-text class="mx-1">属性值:</el-text>
99
<el-tag
1010
v-for="(value, valueIndex) in item.values"
11-
:key="value.name"
11+
:key="value.id"
1212
:disable-transitions="false"
1313
class="mx-1"
1414
closable
@@ -22,8 +22,8 @@
2222
v-model="inputValue"
2323
class="!w-20"
2424
size="small"
25-
@blur="handleInputConfirm(index)"
26-
@keyup.enter="handleInputConfirm(index)"
25+
@blur="handleInputConfirm(index, item.id)"
26+
@keyup.enter="handleInputConfirm(index, item.id)"
2727
/>
2828
<el-button
2929
v-show="!inputVisible(index)"
@@ -40,7 +40,10 @@
4040

4141
<script lang="ts" name="ProductAttributes" setup>
4242
import { ElInput } from 'element-plus'
43+
import * as PropertyApi from '@/api/mall/product/property'
4344
45+
const { t } = useI18n() // 国际化
46+
const message = useMessage() // 消息弹窗
4447
const inputValue = ref('') // 输入框值
4548
const attributeIndex = ref<number | null>(null) // 获取焦点时记录当前属性项的index
4649
// 输入框显隐控制
@@ -79,9 +82,16 @@ const showInput = async (index) => {
7982
InputRef.value[index]!.input!.focus()
8083
}
8184
/** 输入框失去焦点或点击回车时触发 */
82-
const handleInputConfirm = (index) => {
85+
const handleInputConfirm = async (index, propertyId) => {
8386
if (inputValue.value) {
84-
attributeList.value[index].values.push({ name: inputValue.value })
87+
// 保存属性值
88+
try {
89+
const id = await PropertyApi.createPropertyValue({ propertyId, name: inputValue.value })
90+
attributeList.value[index].values.push({ id, name: inputValue.value })
91+
message.success(t('common.createSuccess'))
92+
} catch {
93+
message.error('添加失败,请重试') // TODO 缺少国际化
94+
}
8595
}
8696
attributeIndex.value = null
8797
inputValue.value = ''

0 commit comments

Comments
 (0)