Vue实战之 7. 商品管理---商品分类

news/2024/7/24 11:29:55 标签: html5, css3, 前端

1. 商品分类概述

商品分类用于在购物时,快速找到所要购买的商品,可以通过电商平台主页直观地看到。

在这里插入图片描述

2. 商品分类列表

  • 实现基本布局
  • 实现分类列表数据加载
 // 获取商品分类数据
    async getCateList () {
      const { data: res } = await this.$http.get('categories', { params: this.queryInfo })
      if (res.meta.status !== 200) {
        return this.$message('获取商品分类失败!')
      }

      console.log(res.data)
      // 把数据列表赋值给cateList
      this.cateList = res.data.result
      // 为总数据条数赋值
      this.total = res.data.total
    }

3. 树形表格

1. 第三方树形表格的基本使用

在这里插入图片描述

import TreeTable from 'vue-table-with-tree-grid'

Vue.component('tree-table', TreeTable)

2. 实现分类树形列表

在这里插入图片描述

4. 分页功能

1. 实现分页组件效果

 <!-- 分页区域 -->
       <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[3,5,10,15]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>

2. 分页组件数据处理

// 监听 pagesize 改变的事件
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getCateList()
    },
    // 监听 pagenum的改变
    handleCurrentChange (newPage) {
      this.queryInfo.pagenum = newPage
      this.getCateList()
    }

5. 添加分类

1. 实现分类树形列表

 <!-- 添加分类的对话框 -->
    <el-dialog
  title="添加分类"
  :visible.sync="addCateDialogVisible"
  width="50%">
  <!-- 添加分类的表单 -->
  <el-form :model="addCateForm" :rules="addCateFormRules"
  ref="addCateFormRef" label-width="100px">
  <el-form-item label="分类名称" prop="cat_name">
    <el-input v-model="addCateForm.cat_name"></el-input>
  </el-form-item>
  <el-form-item label="父级分类">
  </el-form-item>
  </el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="addCateDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="addCateDialogVisible = false">确 定</el-button>
  </span>
</el-dialog>

2. 实现分类级联菜单效果

实现级联菜单效果:

<el-cascader clearable change-on-select
    expand-trigger="hover"
    :options="parentCateList"
    :props="cascaderProps"
    v-model="selectedKeys"
    @change="parentCateChange"></el-cascader>

级联菜单数据加载与填充:

 // 获取父级分类的数据列表
    async getParentList () {
      const { data: res } = await this.$http.get('categories', {
        params: {
          type: 2
        }
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取父级分类数据失败!')
      }
      // console.log(res.data)
      this.parentCateList = res.data
    }

3. 控制父级分类的选择

父级分类选择时,获取对应的分类id。

// 选择项发生变化触发的函数
    parentCateChange () {
      // 数组长度大于0,证明选中了父级分类
      // 反之就说明,没有选中任何父级分类
      if (this.selectedKeys.length > 0) {
        // 父级分类的id
        this.addCateForm.cat_pid = this.selectedKeys[this.selectedKeys.length - 1]
        // 为当前分类的等级赋值
        this.addCateForm.cat_level = this.selectedKeys.length
      } else {
        this.addCateForm.cat_pid = 0
        this.addCateForm.cat_level = 0
      }
    }

4. 完成分类添加

// 点击按钮,添加新的分类
    async addCate () {
      // console.log(this.addCateForm)
      this.$refs.addCateFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post('categories', this.addCateForm)
        if (res.meta.status !== 201) {
          return this.$message.error('添加分类失败!')
        }
        this.$message.success('添加分类成功!')
        this.getCateList()
        this.addCateDialogVisible = false
      })

6. 编辑分类信息

 // 展示编辑分类的对话框
    async showEditDialog (id) {
      const { data: res } = await this.$http.get('categories/' + id)
      // console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error('查询分类信息失败!')
      }
      // console.log(res.data)
      this.editCateForm = res.data
      this.EditDialogVisible = true
    },
    // 监听修改用户对话框的关闭事件
    editDialogClosed () {
      this.$refs.editCateFormRef.resetFields()
    },
    // 修改角色信息并且提交
    async editCate () {
      this.$refs.editCateFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.put('categories/' + this.editCateForm.cat_pid,
          { cat_name: this.editCateForm.cat_name })
        if (res.meta.status !== 200) {
          return this.$message.error('更新分类信息失败!')
        }

        // 关闭对话框
        this.EditDialogVisible = false
        // 刷新数据列表
        this.getCateList()
        // 提示修改成功
        this.$message.success('更新分类信息成功!')
      })
    }

7. 删除分类

 // 根据id 删除分类对应的信息
    async removeCateById (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/' + id)

      if (res.meta.status !== 200) {
        return this.$message.error('删除分类失败!')
      }
      this.$message.success('删除分类成功!')
      // 重新渲染数据
      this.getCateList()
    }

http://www.niftyadmin.cn/n/1333735.html

相关文章

Vue实战之 8.商品管理 -- 分类参数

1. 参数管理概述 商品参数用于显示商品的固定的特征信息&#xff0c;可以通过电商平台详情页直观地看到。 2. 商品分类选择 1. 选择商品分类 页面基本布局加载商品分类数据实现商品分类的级联选择效果 // 获取所有的商品分类列表async getCateList () {const { data: res } …

Vue实战之 9.商品管理 -- 商品列表

1. 商品管理概述 商品管理模块用于维护电商平台的商品信息&#xff0c;包括商品的类型、参数、详情等。通过商品管理模块可以实现商品的添加、修改、展示和删除等功能。 2. 商品列表 实现商品列表布局效果调用后台接口获取商品列表数据 // 根据分页获取对应的商品列表async …

Vue实战之 10. 订单管理

1. 订单管理概述 订单管理模块用于维护商品的订单信息&#xff0c;可以查看订单的商品信息、物流信息&#xff0c;并且可以根据实际的运营情况对订单做适当的调整。 2. 订单列表 1. 订单列表展示 订单数据加载订单列表布局 // 获取订单列表async getOrderList () {const { d…

Vue实战之 11. 数据统计

1. 数据统计概述 数据统计模块主要用于统计电商平台运营过程中的各种统计数据&#xff0c;并通过直观的可视化方式展现出来&#xff0c;方便相关运营和管理人员查看。 2. 用户来源数据统计报表 1. Echarts 第三方可视化库的基本使用 详细代码可以去 菜鸟教程 进行复制 2. 实现…

9.1黑马Vue电商后台管理系统商品管理模块完善:编辑商品的功能

在原视频中&#xff0c;老师跳过了这个功能&#xff0c;我觉得自己去实现也可以锻炼自己&#xff0c;于是自己补充了编辑功能 同用户管理&#xff0c;权限管理等之前各个模块的编辑功能不同&#xff0c;因为商品具有很多可编辑的选项&#xff0c;所以选择像添加商品一样&#x…

Vue实战之 12. 项目优化

1. 项目优化 1. 项目优化策略 生成打包报告第三方库启用CDNElement-UI 组件按需加载路由懒加载首页内容定制 1. 生成打包报告 打包时&#xff0c;为了直观地发现项目中存在的问题&#xff0c;可以在打包时生成报告。生成报告的方式有两种&#xff1a; 通过命令行参数的形式…

12. 1 补充:关于 babel-plugin-transform-remove-console 插件

这个插件的作用是在项目发布上线阶段&#xff0c;去掉我们在开发阶段时进行测试的console.log&#xff08;&#xff09; 1. 关于使用 找到项目的babel.config.js&#xff0c;加入一句代码&#xff1a; 但是&#xff0c;如果只是加入这么一句话&#xff0c;其实无论是在项目的开…

Gstreamer编译篇_libmount

Gstreamer编译篇_libmount /usr/lib/x86_64-linux-gnu/libmount.so: file not recognized: File format not… 在交叉编译的时候出现libmount.so文件格式不正确 因为现在是在交叉编译环境下啊。 仔细对比了glib的meson.build # libmount is only used by gio, but we need …