• Vue+element 商品分类业务实现


    商品分类:

     整体布局一眼看上去很是熟悉了 我们已经写过两三遍了

    这里不同的是 要用到新的额外组件GitHub - MisterTaki/vue-table-with-tree-grid: A table (with tree-grid) component for Vue.js 2.0. (Its style extends @iView)A table (with tree-grid) component for Vue.js 2.0. (Its style extends @iView) - GitHub - MisterTaki/vue-table-with-tree-grid: A table (with tree-grid) component for Vue.js 2.0. (Its style extends @iView)https://github.com/MisterTaki/vue-table-with-tree-grid

    树形表格组件、我们需要额外安装

    安装

     npm i vue-table-with-tree-grid -S

    在main.js 里注册

    // 配置树形表格
    import TreeTable from 'vue-table-with-tree-grid'
    Vue.component('tree-table',TreeTable)

     核心表格部分和el-table也有些类似

    1. columns:[
    2. {
    3. label: '分类名称',
    4. prop: 'cat_name',
    5. },
    6. {
    7. label: '是否有效',
    8. // 表示将当前的列定义为模板列
    9. type:'template',
    10. // 表示当前这一列使用模板名称
    11. template:'isOk'
    12. },
    13. {
    14. label: '分类等级',
    15. // 表示将当前的列定义为模板列
    16. type:'template',
    17. // 表示当前这一列使用模板名称
    18. template:'level'
    19. },
    20. {
    21. label: '相关操作',
    22. // 表示将当前的列定义为模板列
    23. type:'template',
    24. // 表示当前这一列使用模板名称
    25. template:'setting'
    26. }
    27. ],
    1. <tree-table :data="cateLists"
    2. index-text="#"
    3. :expand-type="false"
    4. :show-row-hover="false"
    5. border
    6. class="tree-table"
    7. show-index
    8. stripe
    9. :selection-type="false"
    10. :columns="columns">
    11. <template slot="isOk" slot-scope="scope">
    12. <i class="el-icon-success" style="color:#53e457" v-if="scope.row.cat_deleted === false">i>
    13. <i class="el-icon-error" style="color: #ff3355" v-else>i>
    14. template>
    15. <template slot="level" slot-scope="scope">
    16. <el-tag v-if="scope.row.cat_level === 0">一级分类el-tag>
    17. <el-tag v-else-if="scope.row.cat_level === 1" type="success">二级分类el-tag>
    18. <el-tag v-else type="warning">三级分类el-tag>
    19. template>
    20. <template slot="setting" slot-scope="scope">
    21. <el-button type="primary" icon="el-icon-edit" size="medium" @click="openEdit(scope.row.cat_id)">编辑el-button>
    22. <el-button type="danger" icon="el-icon-delete" size="medium" @click="deleteCate(scope.row.cat_id)">删除el-button>
    23. template>
    24. tree-table>

    进入页面后请求回来的分类数据:

     配合使用作用域插槽就可以渲染出来页面了

     编辑和删除分类

    点击相应按钮传入当前ID触发相应的事件

    删除之前 询问框然后直接删除 这里用到原生promise 没有用 async await 简化

    1. // 删除分类相关操作
    2. deleteCate(id){
    3. this.$queding('此操作将永久删除该分类, 是否继续?', '提示', {
    4. confirmButtonText: '确定',
    5. cancelButtonText: '取消',
    6. type: 'warning'
    7. }).then(() => {
    8. this.$http.delete(`categories/${id}`).then(() =>{
    9. this.$Msg({
    10. type: 'success',
    11. message: '删除分类成功!'
    12. });
    13. this.getAllCate()
    14. }).catch(() => {
    15. this.$Msg.error('删除分类失败!')
    16. })
    17. }).catch(() => {
    18. this.$Msg({
    19. type: 'info',
    20. message: '已取消删除'
    21. });
    22. });
    23. }

    点击编辑显示对话框  显示原有的数据 编辑完成后在提交更新

    1. <el-dialog
    2. title="编辑分类"
    3. :visible.sync="editCateBox"
    4. @close="ECateClose"
    5. width="45%">
    6. <el-form ref="editCateFormRef"
    7. :model="editCateInfo"
    8. class="demo-ruleForm"
    9. :rules="FormDataRules"
    10. label-width="110px">
    11. <el-form-item label="分类名称" prop="cat_name">
    12. <el-input v-model="editCateInfo.cat_name">el-input>
    13. el-form-item>
    14. el-form>
    15. <span slot="footer" class="dialog-footer">
    16. <el-button type="primary" @click="ToEditCate">确 定el-button>
    17. <el-button @click="editCateBox = false">取 消el-button>
    18. span>
    19. el-dialog>
    20. <script>
    21. // 编辑分类相关操作
    22. openEdit(id){
    23. this.getEditTie(id)
    24. this.editCateBox = true
    25. },
    26. async getEditTie(id){
    27. const {data:res} = await this.$http.get(`categories/${id}`)
    28. if (res.meta.status !==200) return this.$Msg.error('获取编辑内容失败!')
    29. this.editCateInfo = res.data
    30. },
    31. ToEditCate(){
    32. this.$refs.editCateFormRef.validate(async valid =>{
    33. if (!valid) return
    34. const {data : res} = await this.$http.put(`categories/${this.editCateInfo.cat_id}`,
    35. {cat_name:this.editCateInfo.cat_name})
    36. if (res.meta.status !== 200) return this.$Msg.error('更新分类失败!')
    37. this.$Msg.success('更新分类成功!')
    38. await this.getAllCate()
    39. this.editCateBox = false
    40. })
    41. },
    42. ECateClose(){
    43. this.$refs.editCateFormRef.resetFields()
    44. },
    45. script>

    增加分类

    注意点:增加分类要选择 父级分类  根据选择的父级分类来决定 添加的是几级分类

    点击添加按钮出现对话框---获取级联选择框的数据(到二级分类即可)

    像服务器发起post 操作 

            addCateInfo:{
              cat_pid:0,添加分类的父级id,0则表示父级为0.添加一级分类
              cat_level:0,添加分类的等级,0则表示添加一级分类
              cat_name:'' 分类名 双向绑定直接获取
            },

    1. <el-dialog
    2. title="添加分类"
    3. :visible.sync="addCateBox"
    4. @close="ACateClose"
    5. width="45%">
    6. <el-form ref="addCateFormRef"
    7. :model="addCateInfo"
    8. class="demo-ruleForm"
    9. :rules="FormDataRules"
    10. label-width="110px">
    11. <el-form-item label="分类名称" prop="cat_name">
    12. <el-input v-model="addCateInfo.cat_name">el-input>
    13. el-form-item>
    14. <el-form-item label="父级分类">
    15. <el-cascader
    16. v-model="CurrArr"
    17. :options="PCateLists"
    18. clearable
    19. class="cascader"
    20. :props="CSet"
    21. @change="handleChange">el-cascader>
    22. el-form-item>
    23. el-form>
    24. <span slot="footer" class="dialog-footer">
    25. <el-button type="primary" @click="ToAddCate">确 定el-button>
    26. <el-button @click="addCateBox = false">取 消el-button>
    27. span>
    28. el-dialog>
    29. <script>
    30. data(){
    31. return {
    32. // 添加分类相关对话框
    33. FormDataRules: {
    34. cat_name: [
    35. {
    36. required: true,
    37. message: '请输入要添加的分类名',
    38. trigger: 'blur'
    39. },
    40. ],
    41. },
    42. addCateBox:false,
    43. addCateInfo:{
    44. cat_pid:0,
    45. cat_level:0,
    46. cat_name:''
    47. },
    48. CurrArr:[],
    49. // 级联选择框的相关配置
    50. CSet:{
    51. expandTrigger: 'hover',
    52. children:'children',
    53. label:'cat_name',
    54. value:'cat_id',
    55. checkStrictly:true
    56. }
    57. }
    58. }
    59. // 添加分类相关操作
    60. AddCateShow(){
    61. this.getPrentCate()
    62. this.addCateBox = true
    63. },
    64. async getPrentCate(){
    65. const {data:res} = await this.$http.get('categories',{params:{type:2}})
    66. if (res.meta.status !==200) return this.$Msg.error('获取父级分类列表失败!')
    67. this.PCateLists = res.data
    68. //console.log(this.PCateLists)
    69. },
    70. ACateClose(){
    71. this.$refs.addCateFormRef.resetFields()
    72. this.addCateInfo.cat_level = 0
    73. this.addCateInfo.cat_pid = 0
    74. this.CurrArr = []
    75. },
    76. ToAddCate(){
    77. this.$refs.addCateFormRef.validate(async valid =>{
    78. if (!valid) return
    79. const {data : res} = await this.$http.post('categories',this.addCateInfo)
    80. if (res.meta.status !== 201) return this.$Msg.error('添加分类失败!')
    81. this.$Msg.success('添加分类成功!')
    82. await this.getAllCate()
    83. this.addCateBox = false
    84. })
    85. },
    86. // 联集选择框值发生改变时
    87. handleChange(){
    88. //console.log(this.CurrArr)
    89. // 如果 选择数组中的length 大于0 证明选中了父级分类
    90. // 反之 就说明没有选中任何的父级分类
    91. if (this.CurrArr.length > 0){
    92. this.addCateInfo.cat_level = this.CurrArr.length
    93. this.addCateInfo.cat_pid = this.CurrArr[this.CurrArr.length-1]
    94. return
    95. }else {
    96. this.addCateInfo.cat_level = 0
    97. this.addCateInfo.cat_pid = 0
    98. }
    99. },
    100. script>

  • 相关阅读:
    数据结构 | 树与二叉树
    【vscode编辑器插件】前端 php unity自用插件分享
    Python 教程之控制流(4)Python 中的循环技术
    【深度学习】 Python 和 NumPy 系列教程(廿二):Matplotlib详解:2、3d绘图类型(8)3D饼图(3D Pie Chart)
    极空间z2pro bitwarden+frp+nginx教程
    《Java并发编程的艺术》读书笔记 - 第五章 - Java中的锁
    视频剪辑素材哪里找?这个几个网站就够了。
    Java练习题十四期:不要二
    Quartz的分布式功能化设计
    Selenium Python教程第5章
  • 原文地址:https://blog.csdn.net/benlalagang/article/details/126581147