1.新建分支
git branch—git checkout -b goods_params----git push -u origin goods_params
2.新建路由Params并导入
3.面包屑导航区
<!--面包屑导航区-->
<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>
4.卡片视图区域
<el-card>
<el-alert show-icon :closable="false" title="注意:只允许为第三级分类设置参数" type="warning"> </el-alert>
</el-card>
5.级联选择框的数据渲染
Cascader 级联选择器
<!--卡片视图区域-->
<el-card>
<el-alert
show-icon
:closable="false"
title="注意:只允许为第三级分类设置参数"
type="warning"
>
</el-alert>
<!--选择商品分类区域-->
<el-row class="cat_opt">
<el-col>
<span>选择商品分类:</span>
<!--选择商品分类的级联选择框-->
<el-cascader
v-model="selectedCateKeys"
:options="catelist"
:props="cateProps"
@change="handleChange"
></el-cascader>
</el-col>
</el-row>
</el-card>
data() {
return {
// 商品分类列表
catelist: [],
// 级联选择框
cateProps: {
expandTrigger: 'hover',
value: 'cat_id',
label: 'cat_name',
children: 'children'
},
// 级联选择器的双向绑定
selectedCateKeys: []
}
},
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
},
// 级联选择框选中变化会触发这个函数
handleChange() {
// 证明选中的不是三级分类
if (this.selectedCateKeys.length !== 3) {
this.selectedCateKeys = []
return this.$message.error('选中的不是三级分类')
}
// 证明选中的是三级分类
console.log('123')
}
}
6.渲染分类参数页的Tab页签
<!--商品页签区域-->
<el-tabs v-model="activeName" @tab-click="handleTabClick">
<el-tab-pane label="动态参数" name="first">用户管理</el-tab-pane>
<el-tab-pane label="静态属性" name="second">配置管理</el-tab-pane>
</el-tabs>
// tab页签点击事件的处理函数
handleTabClick () {
console.log(this.activeName)
}
7.渲染添加参数按钮并控制它的启用和禁用
<!--商品页签区域-->
<el-tabs v-model="activeName" @tab-click="handleTabClick">
<!--添加动态参数页面-->
<el-tab-pane label="动态参数" name="first">
<el-button :disabled="isBtnDisabled" type="primary" size="mini">添加参数</el-button>
</el-tab-pane>
<!--添加静态属性页面-->
<el-tab-pane label="静态属性" name="second">
<el-button :disabled="isBtnDisabled" type="primary" size="mini">添加属性</el-button>
</el-tab-pane>
与methods同级
computed: {
// 如果按钮需要被禁用
isBtnDisabled() {
if (this.selectedCateKeys.length !== 3) {
return true
}
return false
}
}
8.获取参数列表
<!--商品页签区域-->
<el-tabs v-model="activeName" @tab-click="handleTabClick">
<!--添加动态参数页面-->
<el-tab-pane label="动态参数" name="many">
<el-button :disabled="isBtnDisabled" type="primary" size="mini"
>添加参数</el-button
>
<!--动态参数表格-->
<el-table :data="manyTableData" border stripe>
<!--展开行-->
<el-table-column type="expand"></el-table-column>
<!--索引列-->
<el-table-column type="index"></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"
>编辑</el-button
>
<el-button type="danger" size="mini" icon="el-icon-delete"
>删除</el-button
>
</template>
</el-table-column>
</el-table>
</el-tab-pane>
<!--添加静态属性页面-->
<el-tab-pane label="静态属性" name="only">
<el-button :disabled="isBtnDisabled" type="primary" size="mini"
>添加属性</el-button
>
<!--静态属性表格-->
<el-table :data="onlyTableData" border stripe>
<!--展开行-->
<el-table-column type="expand"></el-table-column>
<!--索引列-->
<el-table-column type="index"></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"
>编辑</el-button
>
<el-button type="danger" size="mini" icon="el-icon-delete"
>删除</el-button
>
</template>
</el-table-column>
</el-table>
</el-tab-pane>
</el-tabs>
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)
},
// 级联选择框选中变化会触发这个函数
handleChange() {
this.getParamsData()
},
// tab页签点击事件的处理函数
handleTabClick() {
console.log(this.activeName)
this.getParamsData()
},
// 获取参数的列表数据
async getParamsData() {
// 证明选中的不是三级分类
if (this.selectedCateKeys.length !== 3) {
this.selectedCateKeys = []
return this.$message.error('选中的不是三级分类')
}
// 证明选中的是三级分类
console.log(this.selectedCateKeys)
// 根据所选分类的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('获取参数列表失败')
}
if (this.activeName === 'many') {
this.manyTableData = res.data
} else {
this.onlyTableData = res.data
}
}
},
computed: {
// 如果按钮需要被禁用
isBtnDisabled() {
if (this.selectedCateKeys.length !== 3) {
return true
}
return false
},
// 当前选中的三级分类id
cateId() {
if (this.selectedCateKeys.length === 3) {
return this.selectedCateKeys[2]
}
return null
}
}
9.添加按钮功能实现
<!--添加参数对话框-->
<el-dialog
:title="'添加' + titelText"
:visible.sync="addDialogVisible"
width="50%"
@close="addDialogClosed"
>
<!--添加参数对话框的表单-->
<el-form
:model="addForm"
:rules="addFormRules"
ref="addFormRef"
label-width="100px"
>
<el-form-item :label="titelText" 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="addDialogVisible = false"
>确 定</el-button
>
</span>
</el-dialog>
10.调用api实现添加功能
为确定按钮绑定事件addParams
// 点击按钮 添加参数
addParams () {
this.$refs.addFormRef.validate(async valid => {
if (!valid) return this.$message.error('验证失败')
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.addDialogVisible = false
this.getParamsData()
})
}
11.修改和删除功能
<!--编辑参数对话框-->
<el-dialog
:title="'修改' + titelText"
:visible.sync="editDialogVisible"
width="50%"
@close="editDialogClosed"
>
<!--编辑参数对话框的表单-->
<el-form
:model="editForm"
:rules="editFormRules"
ref="editFormRef"
label-width="100px"
>
<el-form-item :label="titelText" 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>
// 点击修改,展示修改的对话框
async showEditDialog(attr_id) {
// 查询当前参数id
const { data: res } = await this.$http.get(`categories/${this.cateId}/attributes/${attr_id}`, {
params: { attr_sel: this.activeName }
})
if (res.meta.status !== 200) {
return this.$message.error('查询参数失败')
}
this.editForm = res.data
this.editDialogVisible = true
},
editDialogClosed() {
this.$refs.editFormRef.resetFields()
},
// 点击按钮 修改参数
editParams() {
this.$refs.editFormRef.validate(async valid => {
if (!valid) return this.$message.error('校验失败')
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.editDialogVisible = false
this.getParamsData()
})
},
// 根据id删除参数项
async removeParams (attr_id) {
const confirmResult = await this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).catch(err => err)
// 用户取消了操作
if (confirmResult !== 'confirm') {
return this.$message.info('已取消了操作')
}
// 删除
const { data: res } = await this.$http.delete(`categories/${this.cateId}/attributes/${attr_id}`)
if (res.meta.status !== 200) {
return this.$message.error('删除参数失败')
}
this.$message.success('删除参数成功')
this.getParamsData()
}