• Vue+element 商品参数业务实现


     一开始提示用户只能为三级分类 设置相关参数

    让用户选择分类 使用:级联选择器

    一开始获取全部的分类:

    1. created() {
    2. this.getAllCate()
    3. },
    4. // 获取全部的分类
    5. async getAllCate(){
    6. const {data:res} = await this.$http.get('categories')
    7. if (res.meta.status !==200) return this.$Msg.error('获取商品分类列表失败!')
    8. this.cateLists = res.data
    9. }

     级联选择器使用请求回来的数据 并且绑定回调

    CSet:{
      expandTrigger: 'hover',
      children:'children',
      label:'cat_name',
      value:'cat_id',
    },
    

    当触发选择器的回调时

    1. 首先会进行判断 是否选中的是三级分类。级联选择器返回的是一个数组 里面的值就是设置的value  ==> cat_id  长度不是3 就没有选中三级分类 就清空数据
    2. 选中了三级分类 开始请求数据  用计算属性 this.CurrId 取得第三级的ID、商品参数分 动态参数(many)和静态参数(only)  默认是动态参数(many)
    3. 服务器返回的是有规律的字符串、我们需要把 attr_vals再转化成数组 在为每一项增加以后要控制的属性  最后看标签页是 many 还是 only 来添加对应的数据

     

    标签页动态绑定值到 activeName  点击标签时 name属性会自动替换掉 activeName 在意最新的数据发送请求  请求参数

     当我们选择成功三级分类后 添加按钮就可以点击了 通过计算属性 判断返回布尔值给 添加按钮的 disabled

    添加属性

     然后就是表格里 每一列的值 

    第一列:展开行

    展开后包含、已有标签、新增标签的按钮

    用标签组件 v-for 循环渲染出刚刚我们处理请求数据的那个数组 

    请求过来数据 设置每一项 inputVisible 是false 所以显示新增按钮

    1. <el-table-column type="expand">
    2. <template slot-scope="scope">
    3. <el-tag v-for="(list,i) in scope.row.attr_vals" :key="i" closable @close="handleClose(i,scope.row)">
    4. {{list}}
    5. el-tag>
    6. <el-input
    7. class="input-new-tag"
    8. v-if="scope.row.inputVisible"
    9. style="width: 200px"
    10. v-model="scope.row.inputValue"
    11. ref="saveTagInput"
    12. size="small"
    13. @keyup.enter.native="handleInputConfirm(scope.row)"
    14. @blur="handleInputConfirm(scope.row)"
    15. >
    16. el-input>
    17. <el-button v-else class="button-new-tag" size="small" @click="showInput(scope.row)">+ 添加新标签el-button>
    18. template>
    19. el-table-column>

    点击按钮时显示输入框:

    1. // 点击按钮时显示输入框
    2. showInput(row){
    3. row.inputVisible = true
    4. // 输入框自动获得焦点 $nextTick 当页面上的元素发生刷新(重新渲染)时的回调函数
    5. this.$nextTick(_ => {
    6. this.$refs.saveTagInput.$refs.input.focus();
    7. });
    8. },

    输入框触发 回车 或者失去焦点时:触发回调  并且点击删除时 触发的回调

    1. // 添加标签的输入框 回车或者 失去焦点时触发
    2. handleInputConfirm(row){
    3. // 判断是否输入了内容 没有输入就默认切换 有内容就发起请求
    4. if (row.inputValue.trim().length === 0){
    5. row.inputValue = ''
    6. row.inputVisible = false
    7. }else {
    8. // 输入的时有效内容 之后 先在前端push
    9. row.attr_vals.push(row.inputValue.trim())
    10. row.inputValue = ''
    11. row.inputVisible = false
    12. // 然后再向服务器更新数据 请求回来
    13. this.upTagData(row)
    14. }
    15. },
    16. // 编辑提交参数标签的请求方法
    17. async upTagData(row){
    18. const {data:res} = await this.$http.put(`categories/${this.CurrId}/attributes/${row.attr_id}`,
    19. {
    20. attr_name:row.attr_name,
    21. attr_sel:this.activeName,
    22. attr_vals:row.attr_vals.join(' ')
    23. })
    24. if (res.meta.status !== 200) return this.$Msg.error('更新参数标签失败!')
    25. this.$Msg.success('更新参数标签成功!')
    26. },
    27. // 点击小叉号 删除对应标签
    28. handleClose(i,row){
    29. row.attr_vals = row.attr_vals.splice(i,1)
    30. this.upTagData(row)
    31. }

    剩下的列就比较简单了 index值  参数名称  操作按钮

    1. <el-table-column type="index" label="#">el-table-column>
    2. <el-table-column label="参数名称" prop="attr_name">el-table-column>
    3. <el-table-column label="操作">
    4. <template slot-scope="scope">
    5. <el-button type="primary" size="small" icon="el-icon-edit" @click="editParams(scope.row.attr_id)">编辑el-button>
    6. <el-button type="danger" size="small" icon="el-icon-delete" @click="deleteParams(scope.row.attr_id)">删除el-button>
    7. template>
    8. el-table-column>

    编辑或者删除按钮 传入相应的id 进行回调

    1. <el-dialog
    2. title="编辑相关参数"
    3. :visible.sync="editParamsBox"
    4. @close="EPClose"
    5. width="45%">
    6. <el-form ref="editParamsFormRef"
    7. :model="editParamsInfo"
    8. class="demo-ruleForm"
    9. :rules="FormDataRules"
    10. label-width="110px">
    11. <el-form-item label="编辑参数" prop="attr_name">
    12. <el-input v-model="editParamsInfo.attr_name">el-input>
    13. el-form-item>
    14. el-form>
    15. <span slot="footer" class="dialog-footer">
    16. <el-button type="primary" @click="ToEditParams">确 定el-button>
    17. <el-button @click="editParamsBox = false">取 消el-button>
    18. span>
    19. el-dialog>
    20. <script>
    21. async editParams(id){
    22. const {data:res} = await this.$http.get(`categories/${this.CurrId}/attributes/${id}`)
    23. if (res.meta.status !== 200) return this.$Msg.error('获取编辑内容失败')
    24. this.editParamsInfo = res.data
    25. this.editParamsBox = true
    26. },
    27. async ToEditParams(){
    28. this.$refs.editParamsFormRef.validate(async valid =>{
    29. if (!valid) return
    30. const {data : res} = await this.$http.put(`categories/${this.CurrId}/attributes/${this.editParamsInfo.attr_id}`,
    31. {
    32. attr_name:this.editParamsInfo.attr_name,
    33. attr_sel:this.activeName,
    34. })
    35. if (res.meta.status !== 200) return this.$Msg.error('编辑分类内容失败!')
    36. this.$Msg.success('编辑分类内容成功!')
    37. await this.getParams()
    38. this.editParamsBox = false
    39. })
    40. },
    41. EPClose(){
    42. this.$refs.editParamsFormRef.resetFields()
    43. this.editParamsInfo={}
    44. },
    45. // 删除参数的相关操作
    46. deleteParams(id){
    47. this.$queding('此操作将永久删除该参数, 是否继续?', '提示', {
    48. confirmButtonText: '确定',
    49. cancelButtonText: '取消',
    50. type: 'warning'
    51. }).then(() => {
    52. this.$http.delete(`categories/${this.CurrId}/attributes/${id}`).then(() =>{
    53. this.$Msg({
    54. type: 'success',
    55. message: '删除参数成功!'
    56. });
    57. this.getParams()
    58. }).catch(() => {
    59. this.$Msg.error('删除参数失败!')
    60. })
    61. }).catch(() => {
    62. this.$Msg({
    63. type: 'info',
    64. message: '已取消删除'
    65. });
    66. });
    67. },
    68. script>

    点击添加属性的操作

    1. <el-dialog
    2. :title="'添加'+addTitle"
    3. :visible.sync="addParamsBox"
    4. @close="APClose"
    5. width="45%">
    6. <el-form ref="addParamsFormRef"
    7. :model="addParamsInfo"
    8. class="demo-ruleForm"
    9. :rules="FormDataRules"
    10. label-width="110px">
    11. <el-form-item :label="addTitle" prop="attr_name">
    12. <el-input v-model="addParamsInfo.attr_name">el-input>
    13. el-form-item>
    14. el-form>
    15. <span slot="footer" class="dialog-footer">
    16. <el-button type="primary" @click="ToAddParams">确 定el-button>
    17. <el-button @click="addParamsBox = false">取 消el-button>
    18. span>
    19. el-dialog>
    20. <script>
    21. // 计算属性 判断 对话框的标题
    22. addTitle(){
    23. if (this.activeName ==='many'){
    24. return '动态参数'
    25. }else {
    26. return '静态属性'
    27. }
    28. },
    29. // 添加属性/参数 相关操作
    30. APClose(){
    31. this.$refs.addParamsFormRef.resetFields()
    32. },
    33. // 发起添加参数的请求
    34. ToAddParams(){
    35. this.$refs.addParamsFormRef.validate(async valid =>{
    36. if (!valid) return
    37. const {data : res} = await this.$http.post(`categories/${this.CurrId}/attributes`,
    38. {
    39. attr_name:this.addParamsInfo.attr_name,
    40. attr_sel:this.activeName,
    41. })
    42. if (res.meta.status !== 201) return this.$Msg.error('添加分类失败!')
    43. this.$Msg.success('添加分类成功!')
    44. await this.getParams()
    45. this.addParamsBox = false
    46. })
    47. },
    48. script>

  • 相关阅读:
    2312. 卖木头块
    java毕业设计基于互联网的图书管理系统—借阅管理子模块(附源码、数据库)
    uni-app路由
    前端微信支付代码(公众号支付)
    Java OutputStream.close()功能简介说明
    mongodb入门(五)
    CesiumJS PrimitiveAPI 高级着色入门 - 从参数化几何与 Fabric 材质到着色器 - 上篇
    操作系统:进程退出,进程等待,进程替换,简单的shell命令行解释器
    winform 多个子项目以及调用方法 (类库)
    基于SSM的教师办公管理的设计与实现(有报告)。Javaee项目。
  • 原文地址:https://blog.csdn.net/benlalagang/article/details/126597473