B.創(chuàng)建子級(jí)路由
創(chuàng)建categories子級(jí)路由組件并設(shè)置路由規(guī)則
import Cate from './components/goods/Cate.vue'
path: '/home', component: Home, redirect: '/welcome', children: [
{ path: "/welcome", component: Welcome },
{ path: "/users", component: Users },
{ path: "/rights", component: Rights },
{ path: "/roles", component: Roles },
{ path: "/categories", component: Cate }
]
C.添加組件基本布局
在Cate.vue組件中添加面包屑導(dǎo)航以及卡片視圖中的添加分類按鈕
<template>
<div>
<h3>商品分類</h3>
<!-- 面包屑導(dǎo)航 -->
<el-breadcrumb separator="/">
<el-breadcrumb-item :to="{ path: '/home' }">首頁(yè)</el-breadcrumb-item>
<el-breadcrumb-item>商品管理</el-breadcrumb-item>
<el-breadcrumb-item>商品分類</el-breadcrumb-item>
</el-breadcrumb>
<!-- 卡片視圖區(qū)域 -->
<el-card>
<!-- 添加分類按鈕區(qū)域 -->
<el-row>
<el-col>
<el-button type="primary">添加分類</el-button>
</el-col>
</el-row>
<!-- 分類表格 -->
<!-- 分頁(yè) -->
</el-card>
</div>
</template>
D.請(qǐng)求分類數(shù)據(jù)
請(qǐng)求分類數(shù)據(jù)并將數(shù)據(jù)保存在data中
<script>
export default {
data() {
return {
// 商品分類數(shù)據(jù)列表
cateList: [],
//查詢分類數(shù)據(jù)的條件
queryInfo: {
type: 3,
pagenum: 1,
pagesize: 5
},
//保存總數(shù)據(jù)條數(shù)
total: 0
}
},
created() {
this.getCateList()
},
methods: {
async getCateList() {
//獲取商品分類數(shù)據(jù)
const { data: res } = await this.$http.get('categories', {
params: queryInfo
})
if (res.meta.status !== 200) {
return this.$message.error('獲取商品列表數(shù)據(jù)失敗')
}
//將數(shù)據(jù)列表賦值給cateList
this.cateList = res.data.result
//保存總數(shù)據(jù)條數(shù)
this.total = res.data.total
// console.log(res.data);
}
}
}
</script>
E.使用插件展示數(shù)據(jù)
使用第三方插件vue-table-with-tree-grid展示分類數(shù)據(jù)
1).在vue 控制臺(tái)中點(diǎn)擊依賴->安裝依賴->運(yùn)行依賴->輸入vue-table-with-tree-gird->點(diǎn)擊安裝
2).打開(kāi)main.js封恰,導(dǎo)入vue-table-with-tree-grid
import TreeTable from 'vue-table-with-tree-grid'
.....
Vue.config.productionTip = false
//全局注冊(cè)組件
Vue.component('tree-table', TreeTable)
3).使用組件展示分類數(shù)據(jù)
<!-- 分類表格
:data(設(shè)置數(shù)據(jù)源) :columns(設(shè)置表格中列配置信息) :selection-type(是否有復(fù)選框)
:expand-type(是否展開(kāi)數(shù)據(jù)) show-index(是否設(shè)置索引列) index-text(設(shè)置索引列頭)
border(是否添加縱向邊框) :show-row-hover(是否鼠標(biāo)懸停高亮) -->
<tree-table :data="cateList" :columns="columns" :selection-type="false"
:expand-type="false" show-index index-text="#" border :show-row-hover="false">
</tree-table>
在數(shù)據(jù)中添加columns:
columns: [
{label:'分類名稱',prop:'cat_name'}
]
F.自定義數(shù)據(jù)列
使用vue-table-with-tree-grid定義模板列并添加自定義列
//先在columns中添加一個(gè)列
columns: [
{label:'分類名稱',prop:'cat_name'},
//type:'template'(將該列設(shè)置為模板列)廓推,template:'isok'(設(shè)置該列模板的名稱為isok)
{label:'是否有效',prop:'',type:'template',template:'isok'},
{label:'排序',prop:'',type:'template',template:'order'},
{label:'操作',prop:'',type:'template',template:'opt'}
]
<!-- 是否有效區(qū)域, 設(shè)置對(duì)應(yīng)的模板列: slot="isok"(與columns中設(shè)置的template一致) -->
<template slot="isok" slot-scope="scope">
<i class="el-icon-success" v-if="scope.row.cat_deleted === false" style="color:lightgreen"></i>
<i class="el-icon-error" v-else style="color:red"></i>
</template>
<!-- 排序 -->
<template slot="order" slot-scope="scope">
<el-tag size="mini" v-if="scope.row.cat_level===0">一級(jí)</el-tag>
<el-tag size="mini" type="success" v-else-if="scope.row.cat_level===1">二級(jí)</el-tag>
<el-tag size="mini" type="warning" v-else>三級(jí)</el-tag>
</template>
<!-- 操作 -->
<template slot="opt" slot-scope="scope">
<el-button size="mini" type="primary" icon="el-icon-edit">編輯</el-button>
<el-button size="mini" type="danger" icon="el-icon-delete">刪除</el-button>
</template>
G.完成分頁(yè)功能
<!-- 分頁(yè) -->
<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>
//添加對(duì)應(yīng)的事件函數(shù)
methods:{
.......
handleSizeChange(newSize){
//當(dāng)pagesize發(fā)生改變時(shí)觸發(fā)
this.queryInfo.pagesize = newSize;
this.getCateList();
},
handleCurrentChange(newPage){
//當(dāng)pagenum發(fā)生改變時(shí)觸發(fā)
this.queryInfo.pagenum = newPage;
this.getCateList();
}
}
H.完成添加分類
......
<!-- 添加分類按鈕區(qū)域 -->
<el-row>
<el-col>
<el-button type="primary" @click="showAddCateDialog">添加分類</el-button>
</el-col>
</el-row>
......
<!-- 添加分類對(duì)話框 -->
<el-dialog title="添加分類" :visible.sync="addCateDialogVisible" width="50%" @close="addCateDialogClosed">
<!-- 添加分類表單 -->
<el-form :model="addCateForm" :rules="addCateFormRules" ref="addCateFormRuleForm" 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="父級(jí)分類" prop="cat_pid">
</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>
//用來(lái)顯示或隱藏添加分類對(duì)話框
addCateDialogVisible: false,
//添加分類的表單數(shù)據(jù)對(duì)象
addCateForm:{
//分類名稱
cat_name:'',
//添加分類的父級(jí)id鸯匹,0則表示父級(jí)為0.添加一級(jí)分類
cat_pid:0,
//添加分類的等級(jí),0則表示添加一級(jí)分類
cat_level:0
},
//添加分類校驗(yàn)規(guī)則
addCateFormRules:{
//驗(yàn)證規(guī)則
cat_name:[ {required:true , message:'請(qǐng)輸入分類名稱',trigger:'blur'} ]
},
//保存1,2級(jí)父級(jí)分類的列表
parentCateList:[]
.......
showAddCateDialog() {
//調(diào)用getParentCateList獲取分類列表
this.getParentCateList()
//顯示添加分類對(duì)話框
this.addCateDialogVisible = true
},
async getParentCateList(){
//獲取父級(jí)分類數(shù)據(jù)列表
const { data: res } = await this.$http.get('categories', {
params: {type:2}
})
if (res.meta.status !== 200) {
return this.$message.error('獲取商品分類列表數(shù)據(jù)失敗')
}
this.parentCateList = res.data
}
添加級(jí)聯(lián)菜單顯示父級(jí)分類
先導(dǎo)入Cascader組件,并注冊(cè)
然后添加使用級(jí)聯(lián)菜單組件:
<el-form-item label="父級(jí)分類" prop="cat_pid">
<!-- expandTrigger='hover'(鼠標(biāo)懸停觸發(fā)級(jí)聯(lián)) v-model(設(shè)置級(jí)聯(lián)菜單綁定數(shù)據(jù)) :options(指定級(jí)聯(lián)菜單數(shù)據(jù)源) :props(用來(lái)配置數(shù)據(jù)顯示的規(guī)則)
clearable(提供“X”號(hào)完成刪除文本功能) change-on-select(是否可以選中任意一級(jí)的菜單) -->
<el-cascader expandTrigger='hover' v-model="selectedKeys" :options="parentCateList" :props="cascaderProps" @change="parentCateChange" clearable change-on-select></el-cascader>
</el-form-item>
添加數(shù)據(jù)
//配置級(jí)聯(lián)菜單中數(shù)據(jù)如何展示
cascaderProps:{
value:'cat_id',
label:'cat_name',
children:'children',
expandTrigger:'hover'
},
//綁定用戶選擇的分類值
selectedKeys:[]
.....
methods:{
.....
parentCateChange(){
//級(jí)聯(lián)菜單中選擇項(xiàng)發(fā)生變化時(shí)觸發(fā)
console.log(this.selectedKeys)
//如果用戶選擇了父級(jí)分類
if(this.selectedKeys.length > 0){
//則將數(shù)組中的最后一項(xiàng)設(shè)置為父級(jí)分類
this.addCateForm.cat_pid = this.selectedKeys[this.selectedKeys.length - 1]
//level也要跟著發(fā)生變化
this.addCateForm.cat_level = this.selectedKeys.length
return
}else{
this.addCateForm.cat_pid = 0
this.addCateForm.cat_level = 0
return
}
},
addCateDialogClosed(){
//當(dāng)關(guān)閉添加分類對(duì)話框時(shí)茫藏,重置表單
this.$refs.addCateFormRef.resetFields()
this.selectedKeys = [];
this.addCateForm.cat_pid = 0
this.addCateForm.cat_level = 0
},
addCate() {
//點(diǎn)擊確定,完成添加分類
console.log(this.addCateForm)
this.$refs.addCateFormRef.validate(async valid => {
if (!valid) return
//發(fā)送請(qǐng)求完成添加分類
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
})
}
}
I.推送代碼
制作完添加分類之后薇芝,將代碼提交到倉(cāng)庫(kù)蓬抄,推送到碼云,將goods_cate分支合并到master
git add .
git commit -m '完成商品分類'
git push
git checkout master
git merge goods_cate
2.參數(shù)管理
只允許給三級(jí)分類內(nèi)容設(shè)置參數(shù),參數(shù)分為動(dòng)態(tài)參數(shù)和靜態(tài)參數(shù)屬性
A.添加子級(jí)組件
添加Params.vue子組件夯到,并在router.js中引入該組件并設(shè)置路由規(guī)則
import Params from './components/goods/Params.vue'
......
path: '/home', component: Home, redirect: '/welcome', children: [
{ path: "/welcome", component: Welcome },
{ path: "/users", component: Users },
{ path: "/rights", component: Rights },
{ path: "/roles", component: Roles },
{ path: "/categories", component: Cate },
{ path: "/params", component: Params }
]
B.完成組件基本布局
完成Params.vue組件的基本布局
其中警告提示信息使用了el-alert嚷缭,在element.js引入該組件并注冊(cè)
<template>
<div>
<h3>分類參數(shù)</h3>
<!-- 面包屑導(dǎo)航 -->
<el-breadcrumb separator="/">
<el-breadcrumb-item :to="{ path: '/home' }">首頁(yè)</el-breadcrumb-item>
<el-breadcrumb-item>商品管理</el-breadcrumb-item>
<el-breadcrumb-item>分類參數(shù)</el-breadcrumb-item>
</el-breadcrumb>
<!-- 卡片視圖區(qū)域 -->
<el-card>
<!-- 警告區(qū)域 :closable="false"(是否展示“X”號(hào)) show-icon(顯示圖標(biāo)) -->
<el-alert title="注意:只允許為第三級(jí)分類設(shè)置相關(guān)參數(shù)" type="warning" :closable="false" show-icon>
</el-alert>
<!-- 選擇商品分類區(qū)域 -->
<el-row class="cat_opt">
<el-col>
<span>選擇商品分類:</span>
<!-- 選擇商品分類的級(jí)聯(lián)選擇框 -->
</el-col>
<el-col></el-col>
</el-row>
</el-card>
</div>
</template>
C.完成級(jí)聯(lián)選擇框
完成商品分類級(jí)聯(lián)選擇框
<!-- 選擇商品分類區(qū)域 -->
<el-row class="cat_opt">
<el-col>
<span>選擇商品分類:</span>
<!-- 選擇商品分類的級(jí)聯(lián)選擇框 -->
<el-cascader expandTrigger='hover' v-model="selectedCateKeys" :options="cateList" :props="cateProps" @change="handleChange" clearable></el-cascader>
</el-col>
<el-col></el-col>
</el-row>
......
<script>
export default {
data() {
return {
//分類列表
cateList:[],
//用戶在級(jí)聯(lián)下拉菜單中選中的分類id
selectedCateKeys:[],
//配置級(jí)聯(lián)菜單中數(shù)據(jù)如何展示
cateProps: {
value: 'cat_id',
label: 'cat_name',
children: 'children'
}
}
},
created() {
this.getCateList()
},
methods: {
async getCateList(){
//獲取所有的商品分類列表
const { data: res } = await this.$http.get('categories')
if (res.meta.status !== 200) {
return this.$message.error('獲取分類數(shù)據(jù)失敗')
}
//將數(shù)據(jù)列表賦值給cateList
this.cateList = res.data
// //保存總數(shù)據(jù)條數(shù)
// this.total = res.data.total
// console.log(res.data);
},
handleChange(){
//當(dāng)用戶在級(jí)聯(lián)菜單中選擇內(nèi)容改變時(shí)觸發(fā)
console.log(this.selectedCateKeys);
}
}
}
</script>
D.展示參數(shù)
展示動(dòng)態(tài)參數(shù)數(shù)據(jù)以及靜態(tài)屬性數(shù)據(jù)
<!-- tab頁(yè)簽區(qū)域 -->
<el-tabs v-model="activeName" @tab-click="handleTabClick">
<!-- 添加動(dòng)態(tài)參數(shù)的面板 將標(biāo)簽頁(yè)改為many -->
<el-tab-pane label="動(dòng)態(tài)參數(shù)" name="many">
<el-button size="mini" type="primary" :disabled="isButtonDisabled">添加參數(shù)</el-button>
<!-- 動(dòng)態(tài)參數(shù)表格 -->
<el-table :data="manyTableData" border stripe>
<!-- 展開(kāi)行 -->
<el-table-column type="expand"></el-table-column>
<!-- 索引列 -->
<el-table-column type="index"></el-table-column>
<el-table-column label="參數(shù)名稱" prop="attr_name"></el-table-column>
<el-table-column label="操作">
<template slot-scope="scope">
<el-button size="mini" type="primary" icon="el-icon-edit">編輯</el-button>
<el-button size="mini" type="danger" icon="el-icon-delete">刪除</el-button>
</template>
</el-table-column>
</el-table>
</el-tab-pane>
<!-- 添加靜態(tài)屬性的面板 將標(biāo)簽頁(yè)改為only -->
<el-tab-pane label="靜態(tài)屬性" name="only">
<el-button size="mini" type="primary" :disabled="isButtonDisabled">添加屬性</el-button>
<!-- 靜態(tài)屬性表格 -->
<el-table :data="onlyTableData" border stripe>
<!-- 展開(kāi)行 -->
<el-table-column type="expand"></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 size="mini" type="primary" icon="el-icon-edit">編輯</el-button>
<el-button size="mini" type="danger" icon="el-icon-delete">刪除</el-button>
</template>
</el-table-column>
</el-table>
</el-tab-pane>
</el-tabs>
<script>
export default {
data() {
return {
......
//tab頁(yè)簽激活顯示的頁(yè)簽項(xiàng)
activeName: 'many',
//用來(lái)保存動(dòng)態(tài)參數(shù)數(shù)據(jù)
manyTableData: [],
//用來(lái)保存靜態(tài)屬性數(shù)據(jù)
onlyTableData: []
}
methods: {
.......
async handleChange() {
//當(dāng)用戶在級(jí)聯(lián)菜單中選擇內(nèi)容改變時(shí)觸發(fā)
console.log(this.selectedCateKeys)
//發(fā)送請(qǐng)求,根據(jù)用戶選擇的三級(jí)分類和面板獲取參數(shù)數(shù)據(jù)
const { data: res } = await this.$http.get(
`categories/${this.cateId}/attributes`,
{ params: { sel: this.activeName } }
)
if (res.meta.status !== 200) {
return this.$message.error('獲取參數(shù)列表數(shù)據(jù)失敗')
}
console.log(res.data)
if (this.activeName === 'many') {
//獲取的是動(dòng)態(tài)參數(shù)
this.manyTableData = res.data
} else if (this.activeName === 'only') {
//獲取的是靜態(tài)屬性
this.onlyTableData = res.data
}
},
handleTabClick() {
console.log(this.activeName)
this.handleChange()
}
},
computed: {
//添加計(jì)算屬性用來(lái)獲取按鈕禁用與否
isButtonDisabled() {
return this.selectedCateKeys.length !== 3
},
//獲取選中的三級(jí)分類id
cateId() {
if (this.selectedCateKeys.length === 3) {
return this.selectedCateKeys[this.selectedCateKeys.length - 1]
}
return null
}
}
E.添加參數(shù)
完成添加參數(shù)或?qū)傩?/p>
<!-- 添加參數(shù)或?qū)傩詫?duì)話框 -->
<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>
export default {
data() {
return {
.......
//控制添加參數(shù).屬性對(duì)話框的顯示或隱藏
addDialogVisible: false,
//添加參數(shù)的表單數(shù)據(jù)對(duì)象
addForm: {
attr_name: ''
},
//添加表單驗(yàn)證規(guī)則
addFormRules: {
attr_name: [{ required: true, message: '請(qǐng)輸入名稱', trigger: 'blur' }]
}
}
},methods: {
.......
addParams() {
//當(dāng)用戶點(diǎn)擊對(duì)話框中的確定時(shí)耍贾,校驗(yàn)表單
this.$refs.addFormRef.validate(async valid => {
//校驗(yàn)不通過(guò)阅爽,return
if (!valid) return
//校驗(yàn)通過(guò),發(fā)送請(qǐng)求完成添加參數(shù)或者屬性
const { data: res } = this.$http.post(`categories/${this.cateId}/attributes`,
{
attr_name: this.addForm.attr_name,
attr_sel: this.activeName,
attr_vals: "a,b,c"
}
)
console.log(res)
if (res.meta.status !== 201) {
return this.$message.error('添加' + this.titleText + '數(shù)據(jù)失敗')
}
this.$message.success('添加' + this.titleText + '數(shù)據(jù)成功')
this.addDialogVisible = false
this.getCateList()
})
}
}
F.編輯參數(shù)
完成編輯參數(shù)或?qū)傩?/p>
<!-- 修改參數(shù)或?qū)傩詫?duì)話框 -->
<el-dialog :title="'修改'+titleText" :visible.sync="editDialogVisible" width="50%" @close="editDialogClosed">
<!-- 添加表單 -->
<el-form :model="editForm" :rules="editFormRules" 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>
export default {
data() {
return {
.......
//控制修改參數(shù).屬性對(duì)話框的顯示或隱藏
editDialogVisible:false,
//修改參數(shù).屬性對(duì)話框中的表單
editForm:{
attr_name:''
},
//修改表單的驗(yàn)證規(guī)則
editFormRules:{
attr_name:[
{ required: true, message: '請(qǐng)輸入名稱', trigger: 'blur' }
]
}
}
},methods: {
.......
async showEditDialog(attr_id){
//發(fā)起請(qǐng)求獲取需要修改的那個(gè)參數(shù)數(shù)據(jù)
const {data:res} = await this.$http.get(`categories/${this.cateId}/attributes/${attr_id}`,
{params:{ attr_sel:this.activeName }})
if (res.meta.status !== 200) {
return this.$message.error('獲取參數(shù)數(shù)據(jù)失敗')
}
this.editForm = res.data;
//顯示修改參數(shù).屬性對(duì)話框
this.editDialogVisible = true;
},
editDialogClosed(){
//當(dāng)關(guān)閉修改參數(shù).屬性對(duì)話框時(shí)
this.$refs.editFormRef.resetFields()
},
editParams(){
//驗(yàn)證表單
this.$refs.editFormRef.validate(async valid => {
if(!valid) return;
//發(fā)送請(qǐng)求完成修改
const {data:res} = await this.$http.put(`categories/${this.cateId}/attributes/${this.editForm.attr_id}`,
{attr_name:this.editForm.attr_name,attr_sel:this.activeName})
if (res.meta.status !== 200) {
return this.$message.error('獲取參數(shù)數(shù)據(jù)失敗')
}
this.$message.success('修改' + this.titleText + '數(shù)據(jù)成功')
this.editDialogVisible = false
this.handleChange();
})
}
}
G.刪除參數(shù)
刪除參數(shù)或?qū)傩?/p>
給兩個(gè)刪除按鈕添加事件
<el-button size="mini" type="danger" icon="el-icon-delete" @click="removeParams(scope.row.attr_id)">刪除</el-button>
<el-button size="mini" type="danger" icon="el-icon-delete" @click="removeParams(scope.row.attr_id)">刪除</el-button>
添加對(duì)應(yīng)的事件處理函數(shù)
async removeParams(attr_id){
//根據(jù)id刪除對(duì)應(yīng)的參數(shù)或?qū)傩? //彈窗提示用戶是否要?jiǎng)h除
const confirmResult = await this.$confirm(
'請(qǐng)問(wèn)是否要?jiǎng)h除該'+this.titleText,
'刪除提示',
{
confirmButtonText: '確認(rèn)刪除',
cancelButtonText: '取消',
type: 'warning'
}
).catch(err => err)
//如果用戶點(diǎn)擊確認(rèn)荐开,則confirmResult 為'confirm'
//如果用戶點(diǎn)擊取消, 則confirmResult獲取的就是catch的錯(cuò)誤消息'cancel'
if (confirmResult != 'confirm') {
return this.$message.info('已經(jīng)取消刪除')
}
//沒(méi)有取消就是要?jiǎng)h除优床,發(fā)送請(qǐng)求完成刪除
const {data:res} = await this.$http.delete(`categories/${this.cateId}/attributes/${attr_id}`)
if (res.meta.status !== 200) {
return this.$message.error('刪除參數(shù)數(shù)據(jù)失敗')
}
this.$message.success('刪除' + this.titleText + '數(shù)據(jù)成功')
this.handleChange()
}