vue
四十、vue项目上手 | 用户管理系统 实现弹窗,搜索和详细页功能(下篇) -凯发ag旗舰厅登录网址下载
@author:runsen
@date:2020/7/10
人生最重要的不是所站的位置,而是内心所朝的方向。只要我在每篇博文中写得自己体会,修炼身心;在每天的不断重复学习中,耐住寂寞,练就真功,不畏艰难,奋勇前行,不忘初心,砥砺前行,人生定会有所收获,不留遗憾 (作者:runsen )
作者介绍:runsen目前大三下学期,专业化学工程与工艺,大学沉迷日语,python, java和一系列数据分析软件。导致翘课严重,专业排名中下。.在大学60%的时间,都在csdn。决定今天比昨天要更加努力。我的征途是星辰大海!
上次完成了用户管理系统的添加用户功能,具体效果如下所示。
但是现在需要弹出的时候,有弹窗。这样就可以显示出添加用户成功。
在这里我选择了可关闭的警告框
文章目录
- 添加弹窗
- 传递参数
- 实现搜索功能
- 详细页功能完成
添加弹窗,我们只需要在customers.vue添加一个组件,这里取名叫做alert,因此新建一个alert.vue。
弹出的信息用message传递。alert.vue代码如下。
<template> <div class="alert alert-warning alert-dismissible" role="alert"><button type="button" class="close" data-dismiss="alert" aria-label="close"><span aria-hidden="true">×</span></button><strong>{{message}}</strong> </div> </template> <script> export default {name: 'alert',props:["message"],data () {return { }} } </script> <!-- add "scoped" attribute to limit css to this component only --> <style scoped> </style>现在就在customers.vue中引用就可以了。
import alert from './alert' components:{alert}在template的模板上添加
下面需要设置弹出的信息,这样就简单,通过add.vue在this.$router.push({path:"/",query:{alert:"用户信息添加成功!"}});设置query弹出的信息。
具体的add.vue的代码如下。
<template><div class="add container"><h1 class="pag-header">添加用户</h1><form v-on:submit="addcustomer"><div class="well"><h4>用户信息</h4><div class="form-group"><label>姓名</label><input type="text"class="form-control"placeholder="name"v-model="customer.name"></div><div class="form-group"><label>电话</label><input type="text"class="form-control"placeholder="phone"v-model="customer.phone"></div><div class="form-group"><label>邮箱</label><input type="text"class="form-control"placeholder="email"v-model="customer.email"></div><div class="form-group"><label>学历</label><input type="text"class="form-control"placeholder="education"v-model="customer.education"></div><div class="form-group"><label>毕业学校</label><input type="text"class="form-control"placeholder="graduationschool"v-model="customer.graduationschool"></div><div class="form-group"><label>职业</label><input type="text"class="form-control"placeholder="profession"v-model="customer.profession"></div><div class="form-group"><label>个人简介</label><!-- <input type="text" class="form-control" placeholder="profile" v-model="customer.profile"> --><textarea class="form-control"rows="10"v-model="customer.profile"></textarea></div><button type="submit"class="btn btn-primary">添加</button></div></form></div> </template><script> export default {name: 'add',data() {return {customer:{}}},methods:{addcustomer(e){// console.log(123);if (!this.customer.name || !this.customer.phone || !this.customer.email) {// console.log("请添加对应的信息!");this.alert = "请添加对应的信息!";}else{let newcustomer = {name:this.customer.name,phone:this.customer.phone,email:this.customer.email,education:this.customer.education,graduationschool:this.customer.graduationschool,profession:this.customer.profession,profile:this.customer.profile}this.$http.post("http://localhost:3000/users",newcustomer).then(function(response){// console.log(response);this.$router.push({path:"/",query:{alert:"用户信息添加成功!"}});})e.preventdefault();}e.preventdefault();}}, } </script><!-- add "scoped" attribute to limit css to this component only --> <style scoped> </style>在拿到query的alert中的用户信息添加成功!信息,在创建用户的时候判断是否有弹出信息,template中的alert通过v-bind:message绑定传给alert.vue,add.vue代码如下。
<template><div class="customers container"><alert v-if="alert" v-bind:message="alert"></alert><h1 class="page-header">用户管理系统</h1><table class="table table-striped"><thead><tr><th>姓名</th><th>电话</th><th>邮箱</th><th></th></tr></thead><tbody v-for="customer in customers"><tr><td>{{customer.name}}</td><td>{{customer.phone}}</td><td>{{customer.email}}</td><td></td></tr></tbody></table></div> </template> <script> import alert from './alert' export default {name: 'customers',data() {return {customers :[],alert:""}},methods: {// 连接数据fetchcustomers(){this.$http.get("http://localhost:3000/users").then(function(response){console.log(response)this.customers = response.body})}},created() {if(this.$route.query.alert){this.alert = this.$route.query.alert;}this.fetchcustomers();},updated() {this.fetchcustomers();},components:{alert} } </script> <!-- add "scoped" attribute to limit css to this component only --> <style scoped> </style>下面测试添加用户是否弹出弹框。
下面我们在凯发ag旗舰厅登录网址下载主页上添加一个input标签实现搜索功能。
<input type="text" class="form-control" placeholder="搜索" v-model="filterinput">关键是如何实现搜索功能,其实就是一过滤函数,这里定义为filterby,传入customers总人数和关键词参数。在v-for使用函数即可,customers.vue代码如下。
<template><div class="customers container"><alert v-if="alert" v-bind:message="alert"></alert><h1 class="page-header">用户管理系统</h1><input type="text" class="form-control" placeholder="搜索" v-model="filterinput"><br><table class="table table-striped"><thead><tr><th>姓名</th><th>电话</th><th>邮箱</th><th></th></tr></thead><tbody><tr v-for="customer in filterby(customers,filterinput)"><td>{{customer.name}}</td><td>{{customer.phone}}</td><td>{{customer.email}}</td><td><router-link class="btn btn-default" v-bind:to="'/customer/' customer.id">详情</router-link></td></tr></tbody></table></div> </template><script> import alert from './alert' export default {name: 'customers',data () {return {customers:[],alert:"",filterinput:""}},methods:{fetchcustomers(){this.$http.get("http://localhost:3000/users").then(function(response){// console.log(response);this.customers = response.body;})},filterby(customers,value){return customers.filter(function(customer){return customer.name.match(value);})}},created(){if (this.$route.query.alert) {this.alert = this.$route.query.alert;}this.fetchcustomers();},updated(){this.fetchcustomers();},components:{alert} } </script><!-- add "scoped" attribute to limit css to this component only --> <style scoped></style>在凯发ag旗舰厅登录网址下载主页中,我决定添加详细页,来做用户的删除和修改功能。
于是新建一个组件,叫做customerdetail.vue,用的是about.vue的模板,就是改了一下name和class。
<template><div class="details container">details</div> </template> <script> export default {name: 'cumstomerdetails',data() {return {}} } </script> <!-- add "scoped" attribute to limit css to this component only --> <style scoped> </style>下面就在main.js中的template添加一个按钮和路由。main.js代码如下。定义的路由是/customer/:id
// the vue build version to load with the `import` command // (runtime-only or standalone) has been set in webpack.base.conf with an alias. import vue from 'vue' import vuerouter from 'vue-router' import vueresource from 'vue-resource' import app from './app' import customers from './components/customers.vue' import about from './components/about.vue' import add from './components/add.vue' import customerdetail from './components/customerdetail.vue'vue.config.productiontip = false vue.use(vuerouter) vue.use(vueresource) // 设置路由 const router = new vuerouter({mode:"history",base: __dirname,routes:[{path:'/',component:customers},{path:'/about',component:about},{path:'/add',component:add},{path:'/customer/:id',component:customerdetail}] }) /* eslint-disable no-new */ new vue({router,template: `<div id="app"><nav class="navbar navbar-default"><div class="container"><div class="navbar-header"><button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar"><span class="sr-only">toggle navigation</span><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span></button><a class="navbar-brand" href="#">用户管理系统</a></div><div id="navbar" class="collapse navbar-collapse"><ul class="nav navbar-nav"><li><router-link to="/">凯发ag旗舰厅登录网址下载主页</router-link></li><li><router-link to="/about">关于凯发ag旗舰厅登录网址下载</router-link></li></ul><ul class="nav navbar-nav navbar-right"><li><router-link to="/add">添加用户</router-link></li></ul></div></div></nav><router-view></router-view></div>` }).$mount("#app")最后就是实现customerdetail.vue的效果,定义一个函数fetchcustomers传入id属性,拿到api中的json数据,id直接通过点击链接参数取得即可。
<template><div class="details container"><router-link to="/" class="btn btn-default">返回</router-link><h1 class="page-header">{{customer.name}}</h1><ul class="list-group"><li class="list-group-item"><span class="glyphicon glyphicon-asterisk"> {{customer.phone}}</span></li><li class="list-group-item"><span class="glyphicon glyphicon-plus"> {{customer.email}}</span></li></ul><ul class="list-group"><li class="list-group-item"><span class="glyphicon glyphicon-asterisk">{{customer.education}}</span></li><li class="list-group-item"><span class="glyphicon glyphicon-plus"> {{customer.graduationschool}}</span></li><li class="list-group-item"><span class="glyphicon glyphicon-asterisk"> {{customer.profession}}</span></li><li class="list-group-item"><span class="glyphicon glyphicon-plus"> {{customer.profile}}</span></li></ul></div> </template><script> export default {name: 'cumstomerdetails',data () {return {customer:""}},methods:{fetchcustomers(id){this.$http.get("http://localhost:3000/users/"id).then(function(response){console.log(response);this.customer = response.body;})},},created(){this.fetchcustomers(this.$route.params.id);} } </script><!-- add "scoped" attribute to limit css to this component only --> <style scoped></style>具体效果如下所示。
总结
以上是凯发ag旗舰厅登录网址下载为你收集整理的四十、vue项目上手 | 用户管理系统 实现弹窗,搜索和详细页功能(下篇)的全部内容,希望文章能够帮你解决所遇到的问题。
如果觉得凯发ag旗舰厅登录网址下载网站内容还不错,欢迎将凯发ag旗舰厅登录网址下载推荐给好友。
- 上一篇:
- 下一篇: