Vue电商后台管理系统
前言
(四)用户列表布局和功能实现
点击用户列表,在右侧主题区域展示用户组件
一、用户列表组件布局
1.创建用户列表链接对应的组件页面
新建components->user->users.vue
<template>
<div>
<h3>用户列表组件</h3>
</div>
</template>
<script>
export default {
}
</script>
<style lang="less" scoped>
</style>
2.在主体区域显示出来
打开router->index.js文件
(1)导入用户列表组件文件
import users from ‘…/components/user/users.vue’
(2)添加home子路由规则
{ path: '/home', component: home ,redirect:'/welcome',children:[
{path:'/welcome',component:welcome},
{path:'/users',component:users}
]}
3.激活状态高亮显示
你想让当前所显示组件对应的二级菜单的图标有高亮显示__为menu设置default-active属性,让其值为对应index值即可
那怎么动态设置呢?
需要在点击二级菜单链接时,把对应的地址保存在sessionStorage中,当刷新页面的时候再把地址取出来,赋值给default-active
(1)给二级菜单绑定单击事件
<el-menu-item :index="'/'+subItem.path" v-for="subItem in item.children" :key="subItem.id" v-on:click="saveNavState('/'+subItem.path)">
saveNavState(activePath){
// 把path值存到sessionStorage中 window.sessionStorage.setItem('activePath',activePath)
this.activePath=activePath
}
(2)在data中定义被激活链接地址
// 被激活的链接地址
activePath:''
(3)把地址从sessionStorage中取出
什么时候呢?在home组件被创建的时候取,即在created时
created() {
this.getMenuList()
this.activePath=window.sessionStorage.getItem('activePath')
},
(4)再把这个值动态绑定到default-active中
:default-active="activePath"
二、绘制用户列表组件的基础布局结构
1.布局
(1)去element-ui中找到想要的布局,复制其代码
<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 :gutter="20">
<el-col :span="8">
<!-- 搜索与添加区域 -->
<el-input placeholder="请输入内容">
<el-button slot="append" icon="el-icon-search"></el-button>
</el-input>
</el-col>
<el-col :span="4">
<el-button type="primary">添加用户</el-button>
</el-col>
</el-row>
</el-card>
</div>
</template>
(2)在element.js中导入对应组件,并全局注册
import {
Breadcrumb,
BreadcrumbItem,
Card,
Row,
Col
} from 'element-ui'
Vue.use(Breadcrumb)
Vue.use(BreadcrumbItem)
Vue.use(Card)
Vue.use(Row)
Vue.use(Col)
(3)添加全局样式
.el-breadcrumb{
margin-bottom: 15px;
font-size: 12px;
}
.el-card{
box-shadow: 0 1px 1px rgba(0,0,0,0.15) !important;
}
2.获取用户列表数据
<script>
export default {
data(){
return {
// 获取用户列表参数对象
queryInfo:{
query:'',
pagenum:1,
pagesize:2
},
// 所有的用户列表
userList:[],
// 总数据条数
total:0
}
},
created(){
this.getUserList()
},
methods:{
async getUserList(){
// 发起ajax数据请求
const {data:res}=await this.$http.get('users',{params:this.queryInfo})
this.userList=res.data.users
this.total=res.data.total
console.log(res)
}
}
}
</script>
3.用el-table组件渲染基本的用户列表
:data="userList"表格数据绑定,是上面我们获取的数据,写在了data()中
(1)去element-ui中复制代码
<!-- 用户列表区域 -->
<el-table :data="userList" border stripe>
<el-table-column label="姓名" prop="username"></el-table-column>
<el-table-column label="邮箱" prop="email"></el-table-column>
<el-table-column label="电话" prop="mobile"></el-table-column>
<el-table-column label="角色" prop="role_name"></el-table-column>
<el-table-column label="状态" prop="mg_state"></el-table-column>
<el-table-column label="操作"></el-table-column>
</el-table>
(2)在element.js中导入对应组件,并全局注册
import {
Table,
TableColumn
} from 'element-ui'
Vue.use(Table)
Vue.use(TableColumn)
(3)添加全局样式
.el-table{
margin-top: 15px;
font-size: 12px;
}
(4)为表格添加索引列
只需要设置type属性,值为index即可
<el-table-column type="index"></el-table-column>
(5)状态列显示效果
当前状态列显示的值时mg_state,但这个值指向的是true/false,而布尔值是没办法直接渲染到页面中的,所以希望把布尔值渲染成开关的形式
怎么把true/false显示的页面上呢__通过插槽
<el-table-column label="状态" prop="mg_state">
<template slot-scope="scope">
<!-- scope.row_拿到这一行的数据 -->
{{scope.row.mg_state}}
</template>
</el-table-column>
怎么渲染成开关呢?
给开关绑定属性,值为刚才我们获得到的true/false
<el-table-column label="状态" prop="mg_state">
<template slot-scope="scope">
<el-switch v-model="scope.row.mg_state"></el-switch>
</template>
</el-table-column>
(6)实现分页效果
<!-- 分页区域 -->
<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryInfo.pagenum" :page-sizes="[1, 2, 5, 10]"
:page-size="queryInfo.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total"></el-pagination>
// 监听pagesize改变的事件
handleSizeChange(newSize){
// console.log(newSize)
this.queryInfo.pagesize=newSize
this.getUserList()
},
// 监听页码值改变事件
handleCurrentChange(newPage){
// console.log(newPage)
this.queryInfo.pagenum=newPage
this.getUserList()
}
.el-pagination{
margin-top: 15px;
}
(7)修改用户状态
把用户状态的修改,同步保存到数据库中:
监听开关状态的改变,从而拿到最新的状态?
switch的change事件
<el-switch v-model="scope.row.mg_state" @change="userStateChanged(scope.row)"></el-switch>
// 监听switch开关状态的改变
userStateChanged(userinfo){
console.log(userinfo)
}
调用对应的api接口,把这次最新的状态保存到数据库中?
// 监听switch开关状态的改变
async userStateChanged(userinfo){
// console.log(userinfo)
const {data:res}=await this.$http.put(`users/${userinfo.id}/state/:${userinfo.mg_state}`)
if(res.meta.status!==200){
userinfo.mg_state=!userinfo.mg_state
return this.$message.error('用户状态更新失败')
}
this.$message.success('更新用户状态成功!')
}
(8)实现搜索功能
将文本框和data中的数据进行双向绑定,点击搜索,调用获取用户列表函数,显示对应数据即可
<el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getUserList">
<el-button slot="append" icon="el-icon-search" @click="getUserList"></el-button>
</el-input>