效果图:
左侧树来自数据库,中间数据来自关键字查询,还有分页条。
一、动态树形菜单
在动态树的实现中,我们这里只关注效果,数据是后端实现的,直接获取。
代码:
<template>
<el-menu router :default-active="$route.path" default-active="2" class="el-menu-vertical-demo"
background-color="#334157" text-color="#fff" active-text-color="#ffd04b" :collapse="collapsed">
<!-- <el-menu default-active="2" :collapse="collapsed" collapse-transition router :default-active="$route.path" unique-opened class="el-menu-vertical-demo" background-color="#334157" text-color="#fff" active-text-color="#ffd04b"> -->
<div class="logobox">
<img class="logoimg" src="../assets/img/logo.png" alt="">
</div>
<!-- <el-menu key="" index="路由跳转路径"> -->
<el-submenu v-for="m in menus" :key="'id_'+m.treeNodeId" :index="'id_'+m.treeNodeId">
<template slot="title">
<i :class="m.icon"></i>
<span>{{m.treeNodeName}}</span>
</template>
<el-menu-item v-for="m2 in m.children" :key="m2.treeNodeId" :index="m2.url">
<i :class="m2.icon"></i>
<span>{{m2.treeNodeName}}</span>
</el-menu-item>
</el-submenu>
</el-menu>
</template>
<script>
export default {
name: 'LeftNav',
data: function() {
return {
ts: new Date().getTime(),
collapsed: false,
menus: []
}
},
methods: {
},
created: function() {
// 这个是为了左侧栏的汉字收缩起来
this.$root.Bus.$on("collapsed-toggle", (v) => {
this.collapsed = v;
});
let url = this.axios.urls.SYSTEM_MENU_TREE;
this.axios.post(url, {}).then((resp) => {
// console.log(resp);
this.menus = resp.data.result;
}).catch(function(error) {
console.log(error);
});
}
}
</script>
<style>
.el-menu-vertical-demo:not(.el-menu--collapse) {
width: 240px;
min-height: 600px;
}
.el-menu-vertical-demo:not(.el-menu--collapse) {
border: none;
text-align: left;
}
.el-menu-item-group__title {
padding: 0px;
}
.el-menu-bg {
background-color: #1f2d3d !important;
}
.el-menu {
border: none;
}
.logobox {
height: 40px;
line-height: 40px;
color: #9d9d9d;
font-size: 20px;
text-align: center;
padding: 20px 0px;
}
.logoimg {
height: 42px;
}
</style>
这里只做了二级菜单的,大概原理就是在请求到了数据中,有一个二级结构,遍历最外层数据,渲染一级菜单,再遍历每一层的子元素,渲染二级菜单。具体样式官网有实例。
需要注意:
router :default-active="$route.path" 是为了标识这是需要做路由跳转的。
index:路由跳转路劲
key:用做唯一标识
另外,这种获取数据的方式会在后面经常用到:
通钩子函数:created在加载时获取数据,将其放入data中定义的属性中,在组件中直接使用该属性,并且数据刷新也只改变此属性内容。
如果index,或者key出了问题,则会出现页面跳转不了或者菜单无法选中。
二、数据表格
1.跳转至数据展示组件
在树形菜单做好后,写一个展示数据的组件,配置路由,将锚点定义在对应位置(我这里定义在AppMain.vue中,)
<template>
<el-container class="main-container">
<el-aside :class="asideClass">
<LeftNav></LeftNav>
</el-aside>
<el-container>
<el-header class="main-header">
<TopNav></TopNav>
</el-header>
<el-main class="main-center">
<router-view></router-view>
</el-main>
</el-container>
</el-container>
</template>
这是现在AppMain.vue的结构。
在LeftNav.vue中定义的路由跳转就会展示在这个锚点上。
2.数据展示页
代码:
<template>
<div>
<el-form :inline="true" :model="formInline" class="user-search">
<el-form-item label="搜索:">
<el-input size="small" v-model="formInline.title" placeholder="文章标题"></el-input>
</el-form-item>
<el-form-item>
<el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
<!-- <el-button size="small" type="primary" icon="el-icon-plus" @click="handleEdit()">添加</el-button> -->
</el-form-item>
</el-form>
<el-table :data="listData" style="width: 100%;" :border="true" max-height="550">
<el-table-column prop="id" label="ID" min-width="10" align="center"></el-table-column>
<el-table-column prop="title" label="标题" min-width="20"></el-table-column>
<el-table-column prop="body" label="内容" min-width="70"></el-table-column>
</el-table>
<el-pagination style="margin-top: 20px;" @size-change="handleSizeChange" @current-change="handleCurrentChange"
:current-page="formInline.page" :page-sizes="[7, 14, 21, 200]" :page-size="100"
layout="total, sizes, prev, pager, next, jumper" :total="formInline.total">
</el-pagination>
</div>
</template>
<script>
export default {
name: "Articles",
data() {
return {
listData: [],
formInline: {
page: 1,
rows: 7,
total: 0,
title: ""
}
}
},
methods: {
doSearch(param) {
let url = this.axios.urls.SYSTEM_ARTICLE_LIST;
this.axios.post(url, param).then((resp) => {
this.listData = resp.data.result;
this.formInline.page = resp.data.pageBean.page;
this.formInline.rows = resp.data.pageBean.rows;
this.formInline.total = resp.data.pageBean.total;
}).catch(function(error) {
console.log(error);
});
},
handleSizeChange(rows) {
console.log("when is" + rows)
this.formInline.page = 1;
this.formInline.rows = rows;
this.doSearch(this.formInline);
},
handleCurrentChange(page) {
this.formInline.page = page;
this.doSearch(this.formInline);
},
search() {
this.doSearch(this.formInline);
}
},
created() {
this.search();
}
}
</script>
<style>
</style>
包括分页和模糊查询。
由于挺简单的,不详细说明。
分页条:
<el-pagination style="margin-top: 20px;" @size-change="handleSizeChange" @current-change="handleCurrentChange"
:current-page="formInline.page" :page-sizes="[7, 14, 21, 200]" :page-size="100"
layout="total, sizes, prev, pager, next, jumper" :total="formInline.total">
</el-pagination>
需要定义出属性:formInline对象,
formInline: {
page: 1,
rows: 7,
total: 0,
title: ""
}
page是当前页,rows是每页条数,total是总条数,title是模糊查询是带的关键字
搜索、新增框
<el-form :inline="true" :model="formInline" class="user-search">
<el-form-item label="搜索:">
<el-input size="small" v-model="formInline.title" placeholder="文章标题"></el-input>
</el-form-item>
<el-form-item>
<el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
<!-- <el-button size="small" type="primary" icon="el-icon-plus" @click="handleEdit()">添加</el-button> -->
</el-form-item>
</el-form>