1.1 参数列表(展示动态参数可选项)
- 动态参数可选项展示及操作,在获取动态参数的方法中进行处理。
res.data.forEach(item => {
item.attr_vals = item.attr_vals ? item.attr_vals.split(' ') : []
item.inputVisible = false
item.inputValue = ''
})
<!-- 展开行 -->
<el-table-column type="expand">
<template slot-scope="scope">
<!-- 循环生成的el-tag -->
<el-tag v-for="(item,i) in scope.row.attr_vals" :key="i" closable>{
{
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>
</template>
</el-table-column>
handleInputConfirm(row){
if(row.inputValue.trim().length===0){
row.inputValue = ''
row.inputVisible = false
return
}
},
showInput(row){
row.inputVisible = true
this.$nextTick(_=>{
this.$refs.saveTagInput.$refs.input.focus()
})
}
1.2 参数列表(添加/删除可选项)
给el-tag添加删除事件
<el-tag v-for="(item,i) in scope.row.attr_vals" :key="i" closable @close="handleClose(i,scope.row)">{
{
item}}</el-tag>
在methods中添加新增,删除事件处理函数
handleInputConfirm(row){
if(row.inputValue.trim().length===0){
row.inputValue = ''
row.inputVisible = false
return
}
row.attr_vals.push(row.inputValue.trim())
row.inputValue = ''
row.inputVisible = false
this.saveAttrVals(row)
},
handleClose(index,row){
row.attr_vals.splice(index,1)
this.saveAttrVals(row)
},
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('修改参数项成功')
}
- 补充:当用户在级联选择框中选中了非三级分类时,需要清空表格中数据
async handleChange() {
if(this.selectedCateKeys.length !== 3){
this.selectedCateKeys = []
this.manyTableData = []
this.onlyTableData = []
return
}
......
- 补充2:当完成了动态参数可选项的功能之后,我们也需要一样的方式完成静态属性可选项的功能。
- 此时我们只需要将动态参数可选项中的展开行复制到静态属性的表格中即可
1.3 参数列表(全部代码)
<template>
<div>
<!-- 面包屑导航 -->
<el-breadcrumb separator-class="el-icon-arrow-right">
<el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
<el-breadcrumb-item>商品管理</el-breadcrumb-item>
<el-breadcrumb-item>参数列表</el-breadcrumb-item>
</el-breadcrumb>
<!-- 卡片区域 -->
<el-card>
<!-- 警告区域 -->
<el-alert
title="注意:只允许为第三极分类设置相关参数"
type="warning"
:closable="false"
show-icon
>
</el-alert>
<!-- 选择商品分类 -->
<el-row class="cate_par">
<el-col>
<span>选择商品分类:</span>
<el-cascader
v-model="selectedCateKeys"
:options="cateList"
:props="cateListProps"
@change="cateIdChange"
>
</el-cascader>
</el-col>
</el-row>
<!-- tab 区域 -->
<el-tabs v-model="activeName" @tab-click="handleTabClick">
<!-- 添加动态参数面板 -->
<el-tab-pane label="动态参数" name="many">
<el-button type="primary" size="mini" :disabled="disabledTabChange" @click="addParams">添加参数</el-button>
<!-- 动态参数表格 -->
<el-table
:data="manyTableDate"
style="width: 100%"
>