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)
先前做的图书管理登录页面虽然有那个页面,在你登录之后他会在数据库中查找值,然后验证,最后跳转到指定页面,,可是当你直接访问那个指定页面的时候不用登录也可以登录那个指定的页面: 由于前段时间已经做过图书 ...
随机推荐
- jquery之选项卡效果
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- asp.net-mvc验证码 asp.net-mvc c#验证码
验证码看不清换一组 写一个类 public class ValidateCode { public ValidateCode() { } /// <summary> /// 验证码的最大长 ...
- Tessnet2图片识别
验证码识别据说可以用C#图像识别类库Tessnet2来实现,Tessnet2源于目前Google维护的开源项目Tesseract2.本文将对此传说进行验证,含验证结果与验证方法. 1. 验证结果 —— ...
- Java经典案例之-“最大公约数和最小公倍数”
/** * 描述:输入两个正整数m和n,求其最大公约数和最小公倍数.(最大公约数:最大公约数, * 也称最大公因数.最大公因子,指两个或多个整数共有约数中最大的一个.) * (最小公倍数:几个数共有的 ...
- WinAPI: GetClassName - 获取指定窗口的类名
WinAPI: GetClassName - 获取指定窗口的类名 //声明: GetClassName( hWnd: HWND; {指定窗口句柄} lpClassName: PChar; {缓冲区} ...
- QT第六天学习
基本事件: 鼠标事件 键盘事件 绘制事件 1.QT中的事件: 事件是对各应用程序需要知道的由应用程序内部或外部产生的事情或动作的通称. QT中事件的处理: 在QT中使用一个对象来表示一个事件,继承自Q ...
- 使用ProgressDialog创建进度对话框
ProgressDialog代表了进度对话框,程序只要创建ProgressDialog实例,并将它显示出来就是一个进度对画框.使用ProgressDialog创建进度对话框有如下两种方式. ①如果只是 ...
- BNU Online Judge-29140
题目链接 http://www.bnuoj.com/bnuoj/problem_show.php?pid=29140 看到这样的题,应该想到an 与an-1 的关系,他们是会有关系的你要去找 代码 ...
- P2P之UDP穿透NAT的原理与实现
首先先介绍一些基本概念: NAT(Network Address Translators),网络地址转换:网络地址转换是在IP地址日益缺乏的情况下产生的,它的主要目的就是为了能够地址重用.NAT分为两 ...
- jQuery replaceWith replaceAll end的用法
jQuery replaceWith replaceAll end的用法 <%@ page language="java" import="java.util.*& ...