vue computed自动计算
<!DOCTYPE html>
<html>
<head>
<title>vue</title>
<meta charset="utf-8">
<script type="text/javascript" src="lodash.min.js"></script>
<script type="text/javascript" src="vue.js"></script>
<style type="text/css">
*{margin:0;padding:0;}
table,tr,th,td{border:1px solid #ccc;}
#page{width:1000px;margin:40px auto;}
#page table{width:100%;}
#page .box{
margin-top:30px;
}
</style>
</head>
<body>
<div id="page">
<table>
<tr>
<th>开始时间</th>
<th>结束时间</th>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
<th>操作</th>
</tr>
<tr v-for="item in list">
<td><input type="text" v-model="item.start" ></td>
<td><input type="text" v-model="item.end" ></td>
<td><input type="text" v-model="item.name" ></td>
<td><input type="text" v-model="item.age"></td>
<td><input type="text" v-model="item.sex"></td>
<td>
<button @click="add($index)">添加</button>
<button @click="remove($index)">删除</button>
</td>
</tr>
</table>
<div class="box">
{{calcRes}}
</div>
</div>
<script type="text/javascript">
new Vue({
el:'#page',
data:{
list:[
{start:24,end:3223,name:'zjs',age:23,sex:'boy'}
]
},
methods:{
add:function(parm){
var obj = {start:0,end:0,name:'',age:'',sex:''};
this.list.splice(parm+1,0,obj);
},
remove:function(parm){
if(this.list.length>1){
this.list.splice(parm,1);
}else{
alert("至少有一条")
}
}
},
computed:{
calcRes:function(){
var total = [];
var days = 0;
for(var i = 0; i < this.list.length; i++){
var arr = [];
if( parseInt(this.list[i].end) - parseInt(this.list[i].start) >= 0){
this.list[i].name ? arr.push(this.list[i].name) : '';
this.list[i].age ? arr.push(this.list[i].age) : '';
this.list[i].sex ? arr.push(this.list[i].sex) : '';
if(this.list[i].name || this.list[i].age || this.list[i].sex){
total.push(_.uniq(arr).length + 1);
}
}else{
alert("第"+(i+1)+"有问题");
continue;
}
}
for(var j = 0; j < total.length; j++){
days += total[j];
}
return days;
}
},
ready:function(){
}
})
</script>
</body>
</html>
<!DOCTYPE html> <html> <head> <title>vue</title> <meta charset="utf-8"> <script type="text/javascript" src="lodash.min.js"></script> <script type="text/javascript" src="vue.js"></script> <style type="text/css"> *{margin:0;padding:0;} table,tr,th,td{border:1px solid #ccc;} #page{width:1000px;margin:40px auto;} #page table{width:100%;} #page .box{ margin-top:30px; } </style> </head> <body> <div id="page"> <table> <tr> <th>开始时间</th> <th>结束时间</th> <th>姓名</th> <th>年龄</th> <th>性别</th> <th>操作</th> </tr> <tr v-for="item in list"> <td><input type="text" v-model="item.start" ></td> <td><input type="text" v-model="item.end" ></td> <td><input type="text" v-model="item.name" ></td> <td><input type="text" v-model="item.age"></td> <td><input type="text" v-model="item.sex"></td> <td> <button @click="add($index)">添加</button> <button @click="remove($index)">删除</button> </td> </tr> </table> <div class="box"> {{calcRes}} </div> </div> <script type="text/javascript"> new Vue({ el:'#page', data:{ list:[ {start:2423423,end:3223,name:'zjs',age:23,sex:'boy'} ] }, methods:{ add:function(parm){ var obj = {start:0,end:0,name:'',age:'',sex:''}; this.list.splice(parm+1,0,obj); }, remove:function(parm){ if(this.list.length>1){ this.list.splice(parm,1); }else{ alert("至少有一条") } } }, computed:{ calcRes:function(){ var total = []; var days = 0; for(var i = 0; i < this.list.length; i++){ var arr = []; if( parseInt(this.list[i].end) - parseInt(this.list[i].start) >= 0){ this.list[i].name ? arr.push(this.list[i].name) : ''; this.list[i].age ? arr.push(this.list[i].age) : ''; this.list[i].sex ? arr.push(this.list[i].sex) : ''; if(this.list[i].name || this.list[i].age || this.list[i].sex){ total.push(_.uniq(arr).length + 1); } } } for(var j = 0; j < total.length; j++){ days += total[j]; } return days; } }, ready:function(){ } }) </script> </body> </html>
vue computed自动计算的更多相关文章
- vue computed 原理
vue computed 主要依靠数据依赖来更新,这里不展示computed源代码,只展示核心思想. computed: { a(){ return this.b ++ } } data:{ b: 1 ...
- Vue computed props pass params
Vue computed props pass params vue 计算属性传参数 // 计算 spreaderAlias spreaderAlias () { console.log('this. ...
- vuex bug & vue computed setter
vuex bug & vue computed setter https://vuejs.org/v2/guide/computed.html#Computed-Setter [Vue war ...
- vue computed、methods、watch的区别
1.computed(计算属性)computed是计算属性,事实上和和data对象里的数据属性是同一类的(使用上), 2.methods(方法)写在html中的时候需要带()支持传参,且需要有触发条件 ...
- Vue computed属性
computed vs methods 我们可以使用Vue中的method计算出学科的总分,最终得到的总数结果是相同的. 在上例的基础上,我们把computed区块中的totalMarks函数整体移到 ...
- 深入理解 Vue Computed 计算属性
Computed 计算属性是 Vue 中常用的一个功能,我们今天来说一下他的执行过长 拿官网简单的例子来看一下: <div id="example"> <p> ...
- vue computed的执行问题
1.在new Vue()的时候,vue\src\core\instance\index.js里面的_init()初始化各个功能 function Vue (options) { if (process ...
- vue computed 可以使用getter和setter
var vm = new Vue({ data: { a: 1 }, computed: { // 仅读取 aDouble: function () { return this.a * 2 }, // ...
- Vue -computed传参数
vue 中computed想传递参数怎么办? 闭包在这里起到的重要的作用 <input v-model="newItem(key,val)" type="text& ...
随机推荐
- 使用dispatch_once实现单例
很多人实现单例会这样写: @implementation XXClass + (id)sharedInstance { static XXClass *sharedInstance = nil; @s ...
- 3.QT计算机实战
mainwindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> namespace Ui { c ...
- 关于javascript 与iOS交互的简单使用
关于导入 JavaScriptCore 这个就不说了 js交互我们首先用到webView与webView的代理 基本上是很简单,,不过后台10分的坑 以下是代码 在webView的加载完成里面 fun ...
- 获取xml字符串中的属性值
pagexml = @"<?xml version='1.0' encoding='utf-8'?> <DATAPACKET Version='2.0'> <M ...
- mysql索引的注意事项
索引的优点 大大加快数据的查询速度 使用分组和排序进行数据查询时,可以显著减少查询时分组和排序的时间 创建唯一索引,能够保证数据库表中每一行数据的唯一性 在实现数据的参考完整性方面,可以加速表和表之间 ...
- tabIndex-bootstrap中Get到的
网页键盘的无障碍访问性 其实加了这个,可以控制Tab键切换的顺序,聚焦等 这个属性,任何标签都可以添加,没有兼容性限制,属性值的范围:0-32767 当一个元素设置tabindex属性值为-1的时候, ...
- 阿里云ecs : Couldn't connect to host, port: smtp.aliyun.com, 25; timeout -1;
上传到服务器后javamail发邮件异常 链接 原来是ECS基于安全考虑,禁用了端口25. 改成465就可以发邮件了. p.setProperty("mail.smtp.socketFact ...
- activity(工作流)初步学习记录
1.概念 工作流(Workflow),就是“业务过程的部分或整体在计算机应用环境下的自动化”,它主要解决的是“使在多个参与者之间按照某种预定义的规则传递文档.信息或任务的过程自动进行,从而实现某个预期 ...
- passwd文件
1.查看/etc/passwd [admin@localhost /]$ cat -n /etc/passwd 1 root:x:0:0:root:/root:/bin/bash 2 bin:x:1: ...
- 解决IIS服务器部署 字体图标找不到的原因
引言 我们往往在IIS上部署Web项目,或者发布Web项目的时候,经常会遇到浏览器找不到字体文件(woff/woff2)产生的错误.这样会导致浏览器无法加载字体图标,在影响加载时间的同时,更无法显示对 ...