vue实现图书管理demo
年后公司的项目要求用到vue.js知识,我angular没有学,node.js和react也只是了解了一点点,所以学起来比较困难。如果你想学vue.js的知识,推荐网址:http://vuejs.org/
详细内容如下:
一、图书管理demo用的知识点
1、bootstrap http://getbootstrap.com/
2、vuejs http://getbootstrap.com/
具体代码如下:
html部分
<div id="app" class="container">
<table class="table table-bordered">
<div v-for = "book in books">
<tr>
<th>书名</th>
<th>书的价格</th>
<th>书的数量</th>
<th>小计</th>
<th>操作</th>
</tr>
<tr v-for = "(index,book ) in books">
<td>{{book.name}}</td>
<td>{{book.price}}</td>
<td><button @click="book.count&&book.count--">-</button><input type="text" v-model="book.count" /><button @click="book.count++">+</button></td>
<td>{{book.price*book.count}}</td>
<td><button class="btn btn-danger" @click="deleteBook(book)">删除</button></td>
</tr>
<tr>
<td colspan="5">
总价{{total}}
</td>
</tr>
</div>
</table>
<div class="form-group">
<label for="bookname" id="bookname">书名</label>
<input type="text" v-model="list.name" class="form-control"/>
</div>
<div class="form-group">
<label for="bookprice" id="bookprice">价格</label>
<input type="text" v-model="list.price" class="form-control"/>
</div>
<div class="form-group">
<label for="bookcount" id="bookcount">数量</label>
<input type="text" v-model="list.count" class="form-control"/>
</div>
<div class="form-group">
<button class="btn btn-primary" @click="add">添加</button>
</div>
</div>
脚本部分
<script src="js/vue.js"></script>
<script>
var vm = new Vue({
el:"#app",
data:{
books:[
{name:'VUE js',price:40,count:1},
{name:'NODE js',price:20,count:1},
{name:'REACT js',price:30,count:1},
{name:'ANGULAR js',price:100,count:1},
{name:'JQUERY js',price:50,count:1},
],
list:{
name:'',
price:'',
count:''
}
},
methods:{
deleteBook(book){
// vue 给我们提供了一个 $remove的方法,在数组中删除
this.books.$remove(book);
/*this.books = this.books.filter((item)=>{
return item != book
})*/
},
add(){
this.books.push(this.list);
this.list = {
name:'',
price:'',
count:''
}
}
},
computed:{
total(){
var sum = 0;
this.books.forEach(item =>{
sum += item.price*item.count;
})
return sum;
}
}
});
</script>
遇到难点总结

当数量小于0时,判断方法,解决方法有很多种,下面总结有3中方法
(一)最简单的方法
根据逻辑判断,这里要注意逻辑判断的顺序,代码如下:
<td><button @click="book.count&&book.count--">-</button><input type="text" v-model="book.count" /><button @click="book.count++">+</button></td>
(二)这里要注意this指向问题
<td><button @click="min(index,book.count)">-</button><input type="text" v-model="book.count" /><button @click="book.count++">+</button></td>
methods:{
min(index){
if(this.books[index].count>0){
this.books[index].count = parseInt(this.books[index].count) - 1;
}
},
deleteBook(book){
// vue 给我们提供了一个 $remove的方法,在数组中删除
this.books.$remove(book);
/*this.books = this.books.filter((item)=>{
return item != book
})*/
},
add(){
this.books.push(this.list);
this.list = {
name:'',
price:'',
count:''
}
}
}
(三) v-on执行时传参问题
<td><button @click="min(book)">-</button><input type="text" v-model="book.count" /><button @click="book.count++">+</button></td>
min(book){
if(book.count>0){
book.count = parseInt(book.count) - 1;
}
}
总结:
v-on执行方法的时候需要传递参数的时候添加(),如果不需要传递参数就不用加上()
如果需要传递参数,我们需要手动传入事件源
建议:
1、如果您有充足的时间来学习vue,务必要把js基础打好,学习下angular.js
2、学会在网上找资料。
作者:白开水
出处:http://www.cnblogs.com/hongxp/
本文以学习、总结和分享为主,如需转载,请联系本人,标明作者和出处,非商业用途!
vue实现图书管理demo的更多相关文章
- vue.js快速搭建图书管理平台
前 言 上一期简单讲解了vue的基本语法,这一次我们做一个小项目,搭建一个简单的图书管理平台,能够让我们更深刻的理解这门语言的妙用. 1.DEMO样式 首先我们需要搭建一个简单的demo样式 ...
- vue入门:用户管理demo
该demo纯前端实现 使用到vue技术点: 1.在该demo中使用到的vue指令:{{}}. v-if. v-model. @click v-for 2.在该demo中使用到的事件修饰符: .prev ...
- EasyUI+MVC+EF简单用户管理Demo(问题及解决)
写在前面 iframe-src EntityFramework版本 connectionStrings View.Action.页面跳转 EasyUI中DataGrid绑定 新增.修改和删除数据 效果 ...
- java图书管理的一个小模块(增删改查,不使用数据库)
图书管理模块:某图书管需要对图书进行信息化管理,要求管理员能够进行新增图书,能按照书名进行模糊查看图书能进行价格统计 系统实现如下:1.新增2.查询3.统计价格 1请输入新书:图书号,书名,作者,价格 ...
- 图书管理之HTML5压缩旋转裁剪图片总结
整体思路 : 在移动端压缩图片并且上传主要用到filereader.canvas 以及 formdata 这三个h5的api.逻辑并不难.整个过程就是: (1)用户使用input file上传图片的 ...
- mvc 权限管理 demo
http://blog.csdn.net/zht666/article/details/8529646 new http://www.cnblogs.com/fengxing/archive/2012 ...
- 【形式化方法:VDM++系列】3.基于VDM++的图书管理系统需求定义
接前文:http://www.cnblogs.com/Kassadin/p/4091040.html 1.Before We Start: 在开始图书管理系统需求定义之前,需要先进行一些说明. 1.1 ...
- 66、django之模型层(model)--多表相关操作(图书管理小练习)
前面几篇随笔的数据库增删改查操作都是在单表的操作上的,然而现实中不可能都是单表操作,更多的是多表操作,一对一,一对多,多对多的表结构才是我们经常需要处理的,本篇将带我们了解多表操作的一些相关操作.也会 ...
- 图书管理系统设置登录验证(cookies)
先前做的图书管理登录页面虽然有那个页面,在你登录之后他会在数据库中查找值,然后验证,最后跳转到指定页面,,可是当你直接访问那个指定页面的时候不用登录也可以登录那个指定的页面: 由于前段时间已经做过图书 ...
随机推荐
- JDBC连接数据库以及简单的操作
package com.zhiyuan.jdbc.util; import java.sql.Connection;import java.sql.DriverManager;import java. ...
- 中国产品众筹NO.1诞生
中国产品众筹NO.1诞生 淘宝众筹打响新拐点之战 http://bbs.taobao.com/catalog/thread/508895-317240623.htm?spm=1.7274553.199 ...
- IOS开发中UITableView(表视图)的滚动优化及自定义Cell
IOS开发中UITableView(表视图)的滚动优化及自定义Cell IOS 开发中UITableView是非常常用的一个控件,我们平时在手机上看到的联系人列表,微信好友列表等都是通过UITable ...
- centos 软件库安装
./configure: error: the HTTP rewrite module requires the PCRE library. You can either disable the mo ...
- GoLang(第一篇 安装)
golang官网:https://golang.org 中文文档:docscn.studygolang.com/doc/ 一:环境变量设置 导入环境变量GOROOT:export GOROOT=/us ...
- CSS BUG 总结
1.IE7 容器使用了滚动条 其子元素中使用 position:relative ,position变成了fixed,从而不随容器的滚动条滚动; 解决: 在其容器元素的属性中也加入 position ...
- 关于ExtJS必输框,多选项
必填项: //页面内传值用ID,和后台联系用name <div class="col-xs-4"> <div class= ...
- Linux笔记(三) - 文件搜素
(1)文件搜索:find-name 根据文件名, *匹配任意字符 ,?单个字符-iname 根据文件名, 不区分大小写-size 根据文件大小查找 (+ 大于 -小于)(-a并且 -o或者)-us ...
- 动态加载css方法实现和深入解析
一.方法引用来源和应用 此动态加载css方法 loadCss,剥离自Sea.js,并做了进一步的优化(优化代码后续会进行分析). 因为公司项目需要用到懒加载来提高网站加载速度,所以将非首屏渲染必需 ...
- Nancy简单实战之NancyMusicStore(六):写在最后
前言 由于公司搬家后,住的地方离上班的地方远了N倍,以前是走路十多分钟就可以到公司的,上班时间也从9:00提早到8:30 现在每天上班都是先坐公交,然后再坐地铁,在这段路上比较浪费时间而且每天都是要6 ...