<template>
<div>
<!-- A面包屑导航区域 -->
<el-breadcrumb separator-class="el-icon-arrow-right">
<el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
<el-breadcrumb-item>用户管理</el-breadcrumb-item>
<el-breadcrumb-item>用户列表</el-breadcrumb-item>
</el-breadcrumb>
<!-- B卡片视图区域 -->
<el-card>
<!-- a 警告区域 -->
<el-alert show-icon :closable="false" title="注意:只允许为第三级分类设置相关参数"type="warning"></el-alert>
<!-- b 选择商品分类区域 -->
<el-row class="cat_opt">
<el-col>
<!--级联选择框 v-model可以把选中的id值双向绑定到对应的数组中 -->
<el-cascader
expand-trigger="hover"
:options="catelist"
v-model="selectedCateKeys"
:props="cateProps"
@change="handleChange">
</el-cascader>
</el-col>
</el-row>
<!-- c tab页签区域 -->
<el-tabs v-model="activeName" @tab-click="handleTabClick">
<el-tab-pane label="动态参数" name="many">
<el-button type="primary" :disabled="isBtnDisabled" size="mini" @click="addDialogVisible=true">添加参数</el-button>
<!-- 动态参数表格 -->
<el-table :data="manyTableData" border stripe>
<!-- 展开行 ?????????????????-->
<el-table-column type="expand">
<template slot-scope="scope">
<!-- {{scope.row}} -->
<!-- 循环渲染tag标签 attr_vals就是New Tag新添加的标签-->
<el-tag v-for="(item,index) in scope.row.attr_vals"
:key="item.index" closable @close="handleClosed(index,scope.row)">{{item}}</el-tag>
<!-- 展开行里面的文本框和按钮切换功能 (当点击New Tag的时候,这个el-input才会显示出来!!!!!!!!)-->
<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>
<el-table-column type="index" label="#"></el-table-column>
<el-table-column label="参数名称" prop="attr_name"></el-table-column>
<el-table-column label="操作">
<template slot-scope="scope">
<el-button type="primary" size="mini" icon="el-icon-edit" @click="showEditDialog(scope.row.attr_id)">编辑</el-button>
<el-button type="danger" size="mini" icon="el-icon-delete" @click="removeParams(scope.row.attr_id)">删除</el-button>
</template>
</el-table-column>
</el-table>
</el-tab-pane>
<el-tab-pane label="静态属性" name="only">
<el-button type="primary" :disabled="isBtnDisabled" size="mini" @click="addDialogVisible=true">添加属性</el-button>
<!-- 静态属性表格 -->
<el-table :data="onlyTableData" border stripe>
<!-- 展开行 -->
<el-table-column type="expand">
<template slot-scope="scope">
<!-- {{scope.row}} -->
<!-- 循环渲染tag标签 -->
<el-tag v-for="(item,index) in scope.row.attr_vals"
:key="item.index" closable @close="handleClosed(index,scope.row)">{{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>
<el-table-column type="index" label="#"></el-table-column>
<el-table-column label="属性名称" prop="attr_name"></el-table-column>
<el-table-column label="操作">
<template slot-scope="scope">
<el-button type="primary" size="mini" icon="el-icon-edit" @click="showEditDialog(scope.row.attr_id)">编辑</el-button>
<el-button type="danger" size="mini" icon="el-icon-delete" @click="removeParams(scope.row.attr_id)">删除</el-button>
</template>
</el-table-column>
</el-table>
</el-tab-pane>
</el-tabs>
</el-card>
<!-- C添加参数的对话框 -->
<el-dialog
:title="'添加'+titleText"
:visible.sync="addDialogVisible"
width="50%"
@close="addDialogClosed">
<!-- <span>这是一段信息</span> -->
<el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-
width="100px" class="demo-ruleForm">
<el-form-item :label="titleText" prop="attr_name">
<el-input v-model="addForm.attr_name"></el-input>
</el-form-item>
</el-form>
<span slot="footer" class="dialog-footer">
<el-button @click="addDialogVisible = false">取 消</el-button>
<el-button type="primary" @click="addParams">确 定</el-button>
</span>
</el-dialog>
<!-- D编辑参数的对话框 -->
<el-dialog
:title="'编辑'+titleText"
:visible.sync="editDialogVisible"
width="50%"
@close="editDialogClosed">
<!-- <span>这是一段信息</span> -->
<el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="100px" class="demo-ruleForm">
<el-form-item :label="titleText" prop="attr_name">
<el-input v-model="editForm.attr_name"></el-input>
</el-form-item>
</el-form>
<span slot="footer" class="dialog-footer">
<el-button @click="editDialogVisible = false">取 消</el-button>
<el-button type="primary" @click="editParams">确 定</el-button>
</span>
</el-dialog>
</div>
</template>
<script>
export default {
name:"Params",
data() {
return {
catelist:[],//商品分类列表
//级联选择框的配置对象
cateProps:{
value:"cat_id",//指定选中的值
label:"cat_name",//指定看到的值
children:'children'//指定父子节点的嵌套属性
},
selectedCateKeys:[],//级联选择框双向绑定到的数组
activeName:"many",//默认被激活的页签的名称
manyTableData:[],//动态参数的数据
onlyTableData:[],//静态属性的数据
addDialogVisible:false,//添加参数的对话框 显示
addForm:{ //添加参数的表单数据对象
attr_name:""
},
addFormRules:{ //规则
attr_name:[{required:true,message:"请输入参数名称",trigger:"blur"}]
},
// 编辑的相关内容
editDialogVisible:false,
editForm:{},
editFormRules:{
attr_name:[
{required:true,message:"请输入参数名称",trigger:"blur"}
]
},
// 展开行的相关内容
// inputVisible:false,//控制文本框的显示
// inputValue:"",//文本框中输入的内容
}
},
created() {
this.getCateList()
},
methods: {
// 获取所有商品分类列表
async getCateList(){
const {data:res}=await this.$http.get("categories")
if(res.meta.status!==200){
return this.$message.error("获取商品分类失败")
}
this.catelist=res.data
console.log("这里是this.catelist",this.catelist);
},
// 级联选择框选中项变化,会触发这个函数
handleChange(){
this.getParamsData()
},
//tab页签点击事件的处理函数 点击tab时触发(点击‘动态参数’/‘静态属性’)
handleTabClick(){
console.log("这里activeName",this.activeName);
this.getParamsData()
},
//获取参数的列表数据 (级联选择框或者tab页签 变化时触发)
async getParamsData(){
// 证明选中的不是三级分类
if(this.selectedCateKeys.length!==3){
this.selectedCateKeys=[]
this.manyTableData=[]//当选中的不是三级分类时,下面的表格数据应该清空
this.onlyTableData=[]
alert('你选中的不是三级分类!!')
return //这里这个return写不写都可以
}else{//选中的是三级分类
console.log("这里selectedCateKeys",this.selectedCateKeys);
const {data:res}=await this.$http.get(`categories/${this.cateId}/attributes`,
{params:{sel:this.activeName}})
if(res.meta.status!==200){
this.$message.error("获取参数列表失败")
}
res.data.forEach(item => {//把res.data里面的attr_vals字符串以,分割分割变成数组
item.attr_vals=item.attr_vals?item.attr_vals.split(","):[]//利用三元,判断attr_vals是否为空,有内容才显示到展开行按钮中
item.inputVisible=false//控制文本框的显示
item.inputValue=""//文本框中输入的内容
});
console.log("这里getParamsData的res.data",res.data);
this.$message.success("获取参数列表成功")
if(this.activeName==="many"){
this.manyTableData=res.data
}else{
this.onlyTableData=res.data
}
}
},
addDialogClosed(){ //对话框关闭 重置数据
this.$refs.addFormRef.resetFields()
},
// 点击添加参数对话框的确定按钮,触发这个方法
addParams(){
this.$refs.addFormRef.validate(async valid=>{
if(!valid) return
const {data:res}=await this.$http.post(`categories/${this.cateId}/attributes`,{
attr_name:this.addForm.attr_name,
attr_sel:this.activeName
})
if(res.meta.status!==201){
return this.$message.error("添加参数失败")
}
this.$message.success("添加参数成功")
this.getParamsData()
this.addDialogVisible=false
})
},
// 点击添加参数的编辑按钮 展示编辑对话框
async showEditDialog(attr_id){
// 根据id,查询当前参数的信息
const {data:res}=await this.$http.get(`categories/${this.cateId}/attributes/${attr_id}`,
{attr_sel:this.activeName})
if(res.meta.status!==200){
return this.$message.error("获取参数信息失败")
}
this.$message.success("获取参数信息成功")
this.editForm=res.data
this.editDialogVisible=true
},
// 点击编辑对话框的确定按钮,修改参数信息
editParams(){
this.$refs.editFormRef.validate(async valid=>{
if(!valid) return
const {data:res} = await this.$http.put(`categories/${this.cateId}/attributes/${this.editForm.attr_id}`
,{attr_name:this.editForm.attr_name,
attr_sel:this.activeName}
)
if(res.meta.status!==200){
return this.$message.error("编辑参数信息失败")
}
this.$message.success("编辑参数信息成功")
this.getParamsData()
this.editDialogVisible=false
})
},
editDialogClosed(){
this.$refs.editFormRef.resetFields()
},
// 点击删除按钮,根据ID删除对应参数项
async removeParams(attr_id){
const confirmResult=await this.$confirm('此操作将永久删除该参数项, 是否继续?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).catch(err=>err)
if(confirmResult!=="confirm"){
this.$message.info("已取消删除参数项")
}//确认删除后 以下根据ID发送删除请求
const {data:res}=await this.$http.delete(`categories/${this.cateId}/attributes/${attr_id}`)
if(res.meta.status!==200){
this.$message.error("删除参数项失败")
}
this.$message.success("删除参数项成功")
this.getParamsData()
},
// 文本框失去焦点或者按下enter都会触发这个事件(展开行)
async handleInputConfirm(scoperow){
if(scoperow.inputValue.trim().length===0){
scoperow.inputValue=''
scoperow.inputVisible=false
return
}//如果没有return,证明输入内容不为空,则继续做后续处理
scoperow.attr_vals.push(scoperow.inputValue.trim())
scoperow.inputValue=''
scoperow.inputVisible=false
this.saveAttrVals(scoperow)
},
//点击按钮,展示文本输入框(展开行)
showInput(scoperow){
scoperow.inputVisible=true
// 让文本框自动获取焦点,$nextTick方法作用,就是当!!!!!!!!!!!!!!!!!!!!!!!
// 页面上元素被重新渲染后,才会执行回调函数中的代码
this.$nextTick(_ => {
this.$refs.saveTagInput.$refs.input.focus();//第一个$refs获取saveTaginput,第二个$refs获取原生DOM input??????????
});
},
//把对attr_vals的操作,保存到数据库(展开行)
async saveAttrVals(scoperow){
// 需求发起请求保存这次操作,否则刷新页面就没了
const {data:res}=await this.$http.put(`categories/${this.cateId}/attributes/${scoperow.attr_id}`,
{attr_name:scoperow.attr_name,
attr_sel:scoperow.attr_sel,
attr_vals:scoperow.attr_vals.join(",")})//要把attr-vals转回为字符串,才能发送请求
if(res.meta.status!==200){
return this.$message.error("修改参数项失败")
}
this.$message.success("修改参数项成功")
},
// 删除展开列里面的对应的参数可选项
handleClosed(index,scoperow){
scoperow.attr_vals.splice(index,1)//从索引号开始删除一个
this.saveAttrVals(scoperow)
}
},
computed:{
//如果按钮需要被禁用,就返回true,否则返回false
isBtnDisabled(){
if(this.selectedCateKeys.length!==3){
return true
}else{
return false
}
},
//当前选中的三级分类的id (cateId就是文档里分类列表请求参数的那个:id)
cateId(){
if(this.selectedCateKeys.length===3){
return this.selectedCateKeys[2]
}
return null
},
titleText(){
if(this.activeName==="many"){
return "动态参数"
}
return "静态属性"
}
}
}
</script>
<style lang="less" scoped>
.cat_opt{
margin: 15px 0;
}
.input-new-tag{
width: 150px;
}
</style>
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/126771.html