1.参数页面渲染
<!--展开行-->
<el-table-column type="expand">
<template slot-scope="scope">
<el-tag closable v-for="(item, i) in scope.row.attr_vals" :key="i">{{item}}</el-tag>
</template>
</el-table-column>
// 获取参数的列表数据
async getParamsData() {
// 证明选中的不是三级分类
if (this.selectedCateKeys.length !== 3) {
this.selectedCateKeys = []
return this.$message.error('选中的不是三级分类')
}
// 证明选中的是三级分类
// 根据所选分类的id和当前所处的面板,获取对应的参数
const { data: res } = await this.$http.get(
`categories/${this.cateId}/attributes`,
{
params: { sel: this.activeName }
}
)
if (res.meta.status !== 200) {
return this.$message.error('获取参数列表失败')
}
res.data.forEach(item => {
item.attr_vals = item.attr_vals ? item.attr_vals = item.attr_vals.split(' ') : []
})
console.log(res.data)
if (this.activeName === 'many') {
this.manyTableData = res.data
} else {
this.onlyTableData = res.data
}
}
2.控制按钮与文本框的切换显示
Tag 标签—动态编辑标签
// 自动获得聚焦
showInput() {
this.inputVisible = true;
this.KaTeX parse error: Expected '}', got 'EOF' at end of input: … this.refs.saveTagInput.$refs.input.focus();
})
<!--循环渲染tag标签-->
<el-tag
closable
v-for="(item, i) in scope.row.attr_vals"
:key="i"
>{{ item }}</el-tag
>
<!--输入的文本框-->
<el-input
class="input-new-tag"
v-if="scope.row.inputVisible"
v-model="scope.row.inputValue"
ref="saveTagInput"
size="small"
@keyup.enter.native="handleInputConfirm(scope.row)"
@blur="handleInputConfirm(scope.row)"
>
</el-input>
<!--添加按钮-->
<el-button
v-else
class="button-new-tag"
size="small"
@click="showInput(scope.row)"
>+ New Tag</el-button
>
// 文本框失去焦点或键盘按确定
handleInputConfirm(row) {
if (row.inputValue.trim().length === 0) {
row.inputValue = ''
row.inputVisible = false
return this.$message.error('输入不合法')
}
// 用户正确输入内容
row.attr_vals.push(row.inputValue.trim())
row.inputValue = ''
row.inputVisible = false
// 发起请求保存操作
this.saveAttrVals(row)
},
// 展示文本输入框
showInput(row) {
row.inputVisible = true
// 自动聚焦
this.$nextTick(_ => {
this.$refs.saveTagInput.$refs.input.focus()
})
}
3.完成添加和删除操作
// 将对attr_vals的操作保存到后台
async saveAttrVals (row) {
// 发起请求保存操作
const { data: res } = await this.$http.put(`categories/${this.cateId}/attributes/${row.attr_id}`, {
attr_name: row.attr_name,
attr_sel: row.attr_sel,
attr_vals: row.attr_vals.join(' ')
})
if (res.meta.status !== 200) {
return this.$message.error('修改参数失败')
}
this.$message.success('修改参数成功')
},
// 删除对应的可选项
handleClose (i, row) {
row.attr_vals.splice(i, 1)
this.saveAttrVals(row)
}