ソースを参照

添加商品分类分类

zhuyijun 2 年 前
コミット
bb3cbdd959
7 ファイル変更323 行追加11 行削除
  1. 3 2
      package.json
  2. 11 0
      src/assets/css/global.css
  3. 284 0
      src/components/goods/Cate.vue
  4. 4 1
      src/main.js
  5. 3 2
      src/plugins/element.js
  6. 6 1
      src/router/index.js
  7. 12 5
      yarn.lock

+ 3 - 2
package.json

@@ -10,9 +10,10 @@
   "dependencies": {
     "axios": "^0.24.0",
     "core-js": "^3.6.5",
-    "element-ui": "^2.4.5",
+    "element-ui": "^2.15.7",
     "vue": "^2.6.11",
-    "vue-router": "^3.2.0"
+    "vue-router": "^3.2.0",
+    "vue-table-with-tree-grid": "^0.2.4"
   },
   "devDependencies": {
     "@vue/cli-plugin-babel": "~4.5.0",

+ 11 - 0
src/assets/css/global.css

@@ -15,4 +15,15 @@ html, body , #app {
 .el-table{
     margin-top: 15px;
     font-size: 12px;
+}
+/* 级联选择器全局样式 */
+.el-cascader-menu {
+    height: 300px;
+}
+
+.el-cascader {
+    width: 100%;
+}
+.el-cascader-panel {
+    height: 300px;
 }

+ 284 - 0
src/components/goods/Cate.vue

@@ -0,0 +1,284 @@
+<!--
+ * @Description: 
+ * @Version: 1.0
+ * @Autor: zhuyijun
+ * @Date: 2021-11-23 20:45:35
+ * @LastEditTime: 2021-11-23 23:03:36
+-->
+<template>
+  <div>
+    <!-- 面包屑导航区 -->
+    <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-card>
+      <!-- 添加商品分类按钮 -->
+      <el-row>
+        <el-col>
+          <el-button type="primary"
+                     @click="showAddCateDialog">添加分类</el-button>
+        </el-col>
+      </el-row>
+
+      <!-- 表格区 -->
+      <!-- 
+        设置索引列
+        :show-index="true"
+         index-text="#"
+       -->
+      <tree-table class="treeTable"
+                  :data="cateList"
+                  :columns="columns"
+                  :selection-type="false"
+                  :expand-type="false"
+                  border
+                  :show-row-hover="false">
+        <!-- 插槽 - 是否有效 -->
+        <template slot="isok"
+                  slot-scope="scope">
+          <i v-if="scope.row.cat_deleted === false "
+             class="el-icon-success"
+             style="color: lightgreen;"></i>
+          <i v-else
+             class="el-icon-error"
+             style="color: lightgreen;"></i>
+        </template>
+
+        <!-- 排序 order -->
+        <template slot="order"
+                  slot-scope="scope">
+          <el-tag v-if="scope.row.cat_level === 0">一级</el-tag>
+          <el-tag v-else-if="scope.row.cat_level === 1"
+                  type="success">二级</el-tag>
+          <el-tag v-else
+                  type="warning">三级</el-tag>
+        </template>
+        <!-- 操作 -->
+        <template slot="oprate"
+                  slot-scope="scope">
+          <el-button type="primary"
+                     icon="el-icon-edit"
+                     size="mini">编辑</el-button>
+          <el-button type="danger"
+                     icon="el-icon-delete"
+                     size="mini">删除</el-button>
+        </template>
+      </tree-table>
+      <!-- 分页区 -->
+      <el-pagination @size-change="handleSizeChange"
+                     @current-change="handleCurrentChange"
+                     :current-page="queryInfo.pagenum"
+                     :page-sizes="[3, 5, 10, 20,50,100]"
+                     :page-size="queryInfo.pagesize"
+                     layout="total, sizes, prev, pager, next, jumper"
+                     :total="total">
+      </el-pagination>
+    </el-card>
+    <!-- 添加分类的对话框 -->
+    <el-dialog title="添加分类"
+               :visible.sync="addCateDialogVisible"
+               width="50%"
+               @close="addCateDialogClosed">
+      <!-- 添加分类表单 -->
+      <!-- 内容主体区 -->
+      <el-form :model="addCateForm"
+               :rules="addCateFormRules"
+               ref="addCateFormRef"
+               label-width="100px">
+
+        <!-- 用户名称 -->
+        <el-form-item label="分类名称: "
+                      prop="username">
+          <el-input v-model="addCateForm.cat_name"
+                    type="text"></el-input>
+        </el-form-item>
+        <!-- 父级分类 -->
+        <el-form-item label="父级分类: ">
+          <!-- options用来指定数据源 
+          change-on-select 允许选中任意一项
+          filterable 可搜索
+          -->
+          <el-cascader v-model="selectedKey"
+                       :options="parentCateList"
+                       :props="cascaderProps"
+                       @change="parentCateChange"
+                       clearable
+                       filterable></el-cascader>
+        </el-form-item>
+      </el-form>
+      <span slot="footer"
+            class="dialog-footer">
+        <el-button @click="addCateDialogVisible = false">取 消</el-button>
+        <el-button type="primary"
+                   @click="addCate">确 定</el-button>
+      </span>
+    </el-dialog>
+  </div>
+</template>
+<script>
+export default {
+  data () {
+    return {
+      //商品分类的数据列表
+      cateList: [],
+      queryInfo: {
+        type: 3,
+        pagenum: 1,
+        pagesize: 5
+      },
+      total: 0,
+      //   为table指定列的定义
+      columns: [
+        {
+          label: '编号',
+          prop: 'cat_id'
+        },
+        {
+          label: '分类名称',
+          prop: 'cat_name'
+        },
+        {
+          label: '是否有效',
+          // 表示将当前列定义为模板类
+          type: 'template',
+          // 表示当前模板名称
+          template: 'isok'
+        },
+        {
+          label: '排序',
+          type: 'template',
+          // 表示当前模板名称
+          template: 'order'
+        },
+        {
+          label: '操作',
+          type: 'template',
+          // 表示当前模板名称
+          template: 'oprate',
+          width: '200px'
+        }
+      ],
+      //   控制添加分类的显示与否
+      addCateDialogVisible: false,
+      //   添加分类表单数据
+      addCateForm: {
+        cat_name: '',
+        // 默认根节点id
+        cat_pid: 0,
+        // 默认一级
+        cat_level: 0
+      },
+      addCateFormRules: {
+        cat_name: [
+          { required: true, message: '请输入分类名称', trigger: 'blur' },
+          { min: 2, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
+        ]
+      },
+      parentCateList: [],
+      cascaderProps: {
+        expandTrigger: 'hover',
+        value: 'cat_id',
+        label: 'cat_name',
+        children: 'children',
+        expandTrigger: 'hover',
+        checkStrictly: true
+      },
+      //   选中的父级分类地址
+      selectedKey: []
+    }
+  },
+  created () {
+    this.getCateList()
+
+  }, methods: {
+    async getCateList () {
+      const { data: res } = await this.$http.get('categories', { params: this.queryInfo })
+      if (res.meta.status !== 200) {
+        return this.$message.error(res.meta.msg)
+      }
+      this.cateList = res.data.result
+      //   总数据条数
+      this.total = res.data.total
+      //   this.$message.success(res.meta.msg)
+    },
+    // 监听pagesize改变事件
+    handleSizeChange (newSize) {
+      this.queryInfo.pagesize = newSize
+      this.getCateList()
+    },
+    // 监听pagenum的改变
+    handleCurrentChange (newPage) {
+      this.queryInfo.pagenum = newPage
+      this.getCateList()
+    },
+    // 点击按钮展示添加分类对话框
+    showAddCateDialog () {
+      // 加载父级分类
+      this.getParentCateList()
+      // 展示对话框
+      this.addCateDialogVisible = true
+    },
+    // 获取父级分类列表
+    async getParentCateList () {
+      const { data: res } = await this.$http.get('categories', {
+        params: {
+          type: 2
+        }
+      })
+      if (res.meta.status !== 200) {
+        return this.$message.error(res.meta.msg)
+      }
+      this.parentCateList = res.data
+    },
+    // 选种项发生变化触发这个函数
+    parentCateChange () {
+      // 如果selectKeys数组中的length大于0 代表选中了
+      let size = this.selectedKey.length
+      if (size > 0) {
+        this.addCateForm.cat_pid = this.selectedKey[size - 1]
+        // 当前分类的等级
+        this.addCateForm.cat_level = size
+        return
+      }
+      this.addCateForm.cat_pid = 0
+      // 当前分类的等级
+      this.addCateForm.cat_level = 0
+    },
+    // 新增分类
+    addCate () {
+      this.$refs.addCateFormRef.validate(async (vaild) => {
+        if (!vaild) return
+        const { data: res } = await this.$http.post(`categories`, this.addCateForm)
+        if (res.meta.status !== 201) {
+          return this.$message.error(res.meta.msg)
+        }
+        this.cateList.push(res.data)
+        this.addCateDialogVisible = false
+        this.$message.success(res.meta.msg)
+      })
+    },
+    // 监听对话框关闭事件
+    addCateDialogClosed () {
+      this.$refs.addCateFormRef.resetFields()
+      // 选中数据清空
+      this.selectedKey = []
+      // 重置
+      this.addCateForm.cat_name = ''
+      this.addCateForm.cat_level = 0
+      this.addCateForm.cat_pid = 0
+    }
+  }
+}
+</script>
+<style lang="less" scoped>
+.treeTable {
+  margin-top: 15px;
+}
+.el-cascader {
+  width: 100%;
+}
+</style>

+ 4 - 1
src/main.js

@@ -3,7 +3,7 @@
  * @Version: 1.0
  * @Autor: zhuyijun
  * @Date: 2021-11-21 23:56:19
- * @LastEditTime: 2021-11-22 11:54:40
+ * @LastEditTime: 2021-11-23 21:12:57
  */
 import Vue from 'vue'
 import App from './App.vue'
@@ -13,6 +13,8 @@ import './plugins/element.js'
 import './assets/css/global.css'
 // 导入字体图标
 // import './assets/fonts/iconfont.css'
+// 插件
+import TreeTable from 'vue-table-with-tree-grid'
 
 import axios from 'axios'
 // 拦截器 设置请求头中的token
@@ -28,6 +30,7 @@ Vue.prototype.$http = axios
 
 Vue.config.productionTip = false
 
+Vue.component('tree-table', TreeTable)
 new Vue({
     router,
     render: h => h(App)

+ 3 - 2
src/plugins/element.js

@@ -3,14 +3,14 @@
  * @Version: 1.0
  * @Autor: zhuyijun
  * @Date: 2021-11-21 23:56:19
- * @LastEditTime: 2021-11-22 22:36:05
+ * @LastEditTime: 2021-11-23 22:11:10
  */
 import Vue from 'vue'
 import {
     Button, Form, FormItem, Input, Message, Container, Header, Aside, Main, Footer,
     Menu, MenuItem, Submenu, Breadcrumb, BreadcrumbItem, Card, Row, Col, Table,
     TableColumn, Switch, Tooltip, Pagination, Dialog, MessageBox, Tag, Tree, Select,
-    Option
+    Option, Cascader
 } from 'element-ui'
 Vue.use(Button)
 Vue.use(Form)
@@ -39,5 +39,6 @@ Vue.use(Tag)
 Vue.use(Tree)
 Vue.use(Select)
 Vue.use(Option)
+Vue.use(Cascader)
 Vue.prototype.$message = Message
 Vue.prototype.$confirm = MessageBox.confirm

+ 6 - 1
src/router/index.js

@@ -3,7 +3,7 @@
  * @Version: 1.0
  * @Autor: zhuyijun
  * @Date: 2021-11-21 23:56:19
- * @LastEditTime: 2021-11-22 19:58:08
+ * @LastEditTime: 2021-11-23 20:48:09
  */
 import Vue from 'vue'
 import VueRouter from 'vue-router'
@@ -13,6 +13,7 @@ import Welcome from '../components/Welcome.vue'
 import Users from '../components/user/Users.vue'
 import Rights from '../components/power/Rights.vue'
 import Roles from '../components/power/Roles.vue'
+import Cate from '../components/goods/Cate.vue'
 Vue.use(VueRouter)
 
 const routes = [
@@ -46,6 +47,10 @@ const routes = [
             {
                 path: '/roles',
                 component: Roles
+            },
+            {
+                path: '/categories',
+                component: Cate
             }
         ]
     }

+ 12 - 5
yarn.lock

@@ -3402,10 +3402,10 @@ electron-to-chromium@^1.3.896:
   resolved "https://registry.npmmirror.com/electron-to-chromium/download/electron-to-chromium-1.3.904.tgz?cache=0&sync_timestamp=1637391807108&other_urls=https%3A%2F%2Fregistry.npmmirror.com%2Felectron-to-chromium%2Fdownload%2Felectron-to-chromium-1.3.904.tgz"
   integrity sha512-x5uZWXcVNYkTh4JubD7KSC1VMKz0vZwJUqVwY3ihsW0bst1BXDe494Uqbg3Y0fDGVjJqA8vEeGuvO5foyH2+qw==
 
-element-ui@^2.4.5:
-  version "2.15.6"
-  resolved "https://registry.npmmirror.com/element-ui/download/element-ui-2.15.6.tgz?cache=0&sync_timestamp=1637231763572&other_urls=https%3A%2F%2Fregistry.npmmirror.com%2Felement-ui%2Fdownload%2Felement-ui-2.15.6.tgz"
-  integrity "sha1-yWCa3TWvWmhqS3aF3B11fHXgHfM= sha512-rcYXEKd/j2G0AgficAOk1Zd1AsnHRkhmrK4yLHmNOiimU2JfsywgfKUjMoFuT6pQx0luhovj8lFjpE4Fnt58Iw=="
+element-ui@^2.15.7:
+  version "2.15.7"
+  resolved "https://registry.npmmirror.com/element-ui/download/element-ui-2.15.7.tgz?cache=0&sync_timestamp=1637231763572&other_urls=https%3A%2F%2Fregistry.npmmirror.com%2Felement-ui%2Fdownload%2Felement-ui-2.15.7.tgz#9e5f8461edb53cfd89a88614b6caa82bd110ee38"
+  integrity sha512-+J6rnXajxzLwV6w8Q6bf7Yqzk1FO1ewbIrCy/4B5alnd7tj8WEpfQoAvISirVaUGVGy77d9Ji3o2bF4f0AsJLQ==
   dependencies:
     async-validator "~1.8.1"
     babel-helper-vue-jsx-merge-props "^2.0.0"
@@ -8529,6 +8529,13 @@ vue-style-loader@^4.1.0, vue-style-loader@^4.1.2:
     hash-sum "^1.0.2"
     loader-utils "^1.0.2"
 
+vue-table-with-tree-grid@^0.2.4:
+  version "0.2.4"
+  resolved "https://registry.npm.taobao.org/vue-table-with-tree-grid/download/vue-table-with-tree-grid-0.2.4.tgz#b60b22810ab861bdb669afb3d6f0079a4cee52a4"
+  integrity sha1-tgsigQq4Yb22aa+z1vAHmkzuUqQ=
+  dependencies:
+    vue "^2.4.3"
+
 vue-template-compiler@^2.6.11:
   version "2.6.14"
   resolved "https://registry.nlark.com/vue-template-compiler/download/vue-template-compiler-2.6.14.tgz?cache=0&sync_timestamp=1623059640396&other_urls=https%3A%2F%2Fregistry.nlark.com%2Fvue-template-compiler%2Fdownload%2Fvue-template-compiler-2.6.14.tgz"
@@ -8542,7 +8549,7 @@ vue-template-es2015-compiler@^1.9.0:
   resolved "https://registry.npm.taobao.org/vue-template-es2015-compiler/download/vue-template-es2015-compiler-1.9.1.tgz"
   integrity sha1-HuO8mhbsv1EYvjNLsV+cRvgvWCU=
 
-vue@^2.6.11:
+vue@^2.4.3, vue@^2.6.11:
   version "2.6.14"
   resolved "https://registry.npmmirror.com/vue/download/vue-2.6.14.tgz?cache=0&sync_timestamp=1636947969556&other_urls=https%3A%2F%2Fregistry.npmmirror.com%2Fvue%2Fdownload%2Fvue-2.6.14.tgz"
   integrity "sha1-5RqlJQJQ1Wmj+606ilpofWA24jU= sha512-x2284lgYvjOMj3Za7kqzRcUSxBboHqtgRE2zlos1qWaOye5yUmHn42LB1250NJBLRwEcdrB0JRwyPTEPhfQjiQ=="