文章目录
0. 准备工作
component下新建文件夹goods下新建文件Params.vue,并在路由中引入文件。
1.界面布局
1.1 界面样式

1.2 界面导航
<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-alert
show-icon
title="注意:只允许为第三级分类设置相关参数!"
type="warning"
:closable="false"
></el-alert>
2.选择商品分类
2.1 界面样式
<el-row class="cat_opt">
<el-col>
<span>选择商品分类:</span>
<!-- 选择商品的级联选择框 -->
<el-cascader
v-model="selectedCateKeys"
:options="cateList"
:props="casteProps"
@change="handleChange"
clearable
></el-cascader>
</el-col>
</el-row>
2.2 数据添加
// 商品分类列表
cateList: [],
// 指定级联选择器的配置对象
casteProps: {
expandTrigger: 'hover',
value: 'cat_id',
label: 'cat_name',
children: 'children',
checkStrictly: true
},
// 级联选择框双向绑定到的数组
selectedCateKeys: [],
2.3 方法实现
// 级联选择框中选项变化会触发这个函数
handleChange () {
this.getParamsData()
},
3.页签区添加参数、属性
3.1 界面样式
<el-tabs v-model="activeName" @tab-click="handleTabClick">
<!-- 添加动态参数面板 -->
<el-tab-pane label="动态参数" name="many">
<!-- 添加参数按钮 -->
<el-button
type="primary"
size="mini"
:disabled="isBtnDisabled"
@click="addDialogVisible=true"
>添加参数</el-button
>
<!-- 动态参数表格 -->
<el-table :data="manyTableData" border stripe>
<!-- 展开行 -->
<el-table-column type="expand">
<template slot-scope="scope">
<!-- 渲染tag标签 -->
<el-tag
v-for="(item, index) in scope.row.attr_vals"
:key="index"
closable
@close="handleClose(index, scope.row)"
>{{item}}</el-tag>
<!-- 输入文本框 -->
<el-input
class="input-new-tag"
v-if="scope.row.inputVisible"
v-model="scope.row.w"
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"></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"
icon="el-icon-edit"
size="mini"
@click="showEditDialog(scope.row.attr_id)"
>编辑</el-button>
<el-button
type="danger"
icon="el-icon-delete"
size="mini"
@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"
size="mini"
:disabled="isBtnDisabled"
@click="addDialogVisible=true"
>添加属性</el-button>
<!-- 属性表格 -->
<el-table :data="onlyTableData" border stripe>
<!-- 展开行 -->
<el-table-column type="expand">
<!-- 索引列 -->
<template slot-scope="scope">
<!-- 循环渲染标签 -->
<el-tag
v-for="(item, index) in scope.row.attr_vals"
:key="index"
closable
@close="handleClose(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"></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"
icon="el-icon-edit"
size="mini"
@click="showEditDialog(scope.row.attr_id)"
>编辑</el-button>
<el-button
type="danger"
icon="el-icon-delete"
@click="removeParams(scope.row.attr_id)"
size="mini">删除</el-button>
</template>
</el-table-column>
</el-table>
</el-tab-pane>
</el-tabs>
3.2 数据添加
// 被激活页签名称
activeName: 'many',
// 动态参数数据
manyTableData: [],
// 静态属性的数据
onlyTableData: [],
3.3 方法实现
// tab页签点击事件的处理函数
handleTabClick () {
this.getParamsData()
},
// 获取参数的列表数据
async getParamsData () {
if (this.selectedCateKeys.length !== 3) {
this.selectedCateKeys = []
this.manyTableData = []
this.onlyTableData = []
}
// 选中的是三级分类
// 根据所选分类 id 和当前所处的版面,获取对应参数
const { data: result } = await this.$http.get(
`categories/${this.cateId}/attributes`,
{
params: { sel: this.activeName }
})
if (result.meta.status !== 200) {
return this.$message.error('获取参数列表失败')
}
result.data.forEach(item => {
item.attr_vals = item.attr_vals ? item.attr_vals.split(' ') : []
// 添加一个布尔值,控制文本框的显示与隐藏
item.inputVisible = false
// 文本框输入的值
item.inputValue = ''
})
if (this.activeName === 'many') {
this.manyTableData = result.data
} else {
this.onlyTableData = result.data
}
},
// 点击按钮展示修改的对话框
async showEditDialog (id) {
const { data: result } = await this.$http.get(
`categories/${this.cateId}/attributes/${id}`,
{ params: { attr_sel: this.activeName } })
if (result.meta.status !== 200) {
return this.$message.error('获取参数失败!')
}
this.editForm = result.data
this.editDialogVisible = true
},
async removeParams (id) {
const confirmResult = await this.$confirm(
'此操作将永远删除该参数,是否继续?',
'提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).catch(err => err)
if (confirmResult !== confirm) {
return this.$message.info('已取消删除')
}
// 删除
const { data: result } = await this.$http.delete(
`categories/${this.cateId}/attributes/${id}`
)
if (result.meta.status !== 200) {
return this.$message.error('删除属性失败!')
}
this.$message.success('删除属性成功!')
this.getParamsData()
},
// 文本框失去焦点,或按下enter键,会触发事件
handleInputConfirm (row) {
if (row.inputValue.trim().length === 0) {
row.inputValue = ''
row.inputVisible = false
return
}
// 如果没有return,即有内容输入,要后续处理
row.attr_vals.push(row.inputValue.trim())
row.inputValue = ''
row.inputVisible = false
this.saveAttrVals(row)
},
// 点击按钮,展示文本输入框
showInput (row) {
row.inputVisible = true
// 让文本框自动获得焦点
// $nextTick方法作用:
// 当页面上元素被重新渲染之后,才会执行回调函数中的代码
this.$nextTick(_ => {
this.$refs.saveTagInput.$refs.input.focus()
})
},
4.添加参数对话框
4.1 界面样式
<el-dialog
:title="`添加${titleText}`"
:visible.sync="addDialogVisible"
width="50%"
@close="addDialogClosed">
<!-- 内容主体区 -->
<el-form
:model="addForm"
:rules="addFormRules"
ref="addFormRef"
label-width="100px"
>
<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>
4.2 数据添加
// 控制添加对话框的显示与隐藏
addDialogVisible: false,
// 添加参数的表单数据对象
addForm: {
attr_name: ''
},
// 添加表单的验证对象
addFormRules: {
attr_name: [
{ required: true, message: '请输入参数名', trigger: 'blur' }
]
},
4.3 方法实现
computed: {
// 如果按钮需要被禁用,返回真
isBtnDisabled () {
if (this.selectedCateKeys.length !== 3) {
return true
}
return false
},
cateId () {
if (this.selectedCateKeys.length === 3) {
return this.selectedCateKeys[2]
}
return null
},
// 动态计算标题文本
titleText () {
if (this.activeName === 'many') {
return '动态参数'
}
return '静态属性'
}
},
// 点击按钮添加参数
addParams () {
this.$refs.addFormRef.validate(
async valid => {
if (!valid) return
const { data: result } = await this.$http.post(
`categories/${this.cateId}/attributes`, {
attr_name: this.addForm.attr_name,
attr_sel: this.activeName
})
if (result.meta.status !== 201) {
return this.$message.error('添加参数失败')
}
this.$message.success('添加参数成功!')
this.addDialogVisible = false
this.getParamsData()
}
)
},
5.修改参数对话框
5.1 界面样式
<el-dialog
:title="`修改${titleText}`"
:visible.sync="editDialogVisible"
width="50%"
@close="editDialogClosed">
<!-- 内容主体区 -->
<el-form
:model="editForm"
:rules="addFormRules"
ref="editFormRef"
label-width="100px">
<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>
5.2 数据添加
// 控制修改对话框的显示与隐藏
editDialogVisible: false,
// 修改的表单数据对象
editForm: {}
5.3 方法实现
// 监听修改对话框的关闭事件
editDialogClosed () {
this.$refs.editFormRef.resetFields()
},
// 点击按钮修改参数
editParams () {
this.$refs.editFormRef.validate(async valid => {
if (!valid) return
const { data: result } = await this.$http.put(
`categories/${this.cateId}/attributes/${this.editForm.attr_id}`,
{
attr_name: this.editForm.attr_name,
attr_sel: this.activeName
})
if (result.meta.status !== 200) {
return this.$message.error('修改参数失败')
}
this.$message.success('修改参数成功')
this.getParamsData()
this.editDialogVisible = false
})
},
// 将 对attr_vals操作后的数据保存到数据库
async saveAttrVals (row) {
// 需要发起请求保存操作
const { data: result } = 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 (result.meta.status !== 200) {
return this.$message.error('修改参数项失败!')
}
this.$message.success('修改参数项成功!')
}
6.功能展示


本文档详细介绍了使用Vue和Element-UI构建前端电商项目中商品分类参数的实现过程,包括界面布局、选择商品分类、页签区参数添加、参数对话框的创建和功能展示等关键步骤。
1738

被折叠的 条评论
为什么被折叠?



