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& ...
随机推荐
- 【React Native开发】React Native控件之ProgressBarAndroid进度条解说(12)
),React Native技术交流4群(458982758).请不要反复加群! 欢迎各位大牛,React Native技术爱好者增加交流!同一时候博客左側欢迎微信扫描关注订阅号,移动技术干货,精彩文 ...
- 87.node.js操作mongoDB数据库示例分享
转自:https://www.cnblogs.com/mracale/p/5845148.html 连接数据库 var mongo=require("mongodb"); va ...
- 从 MVC 到微服务,技术演变的必经之路
架构模式演进 CGI 模式 图 1 CGI 出现于 1993 年,图 1 是 CGI 模式比较简单的结构图. MVC 模式 开源电商软件等都是采用 MVC 模式,MVC 模式是做软件开发必学和必经历的 ...
- POJ 3278 Catch That Cow【BFS】
题意:给出n,k,其中n可以加1,可以减1,可以乘以2,问至少通过多少次变化使其变成k 可以先画出样例的部分状态空间树 可以知道搜索到的深度即为所需要的最小的变化次数 下面是学习的代码----@_@ ...
- img标签IE下有边距——2017/7/21
设置css 在全局变量的是和给img标签设置 img{ border:0;} 1,img{float:left}2,img{display:block}
- jQuery基本操作以及与js的一些比较
jQuery和js主要区别在DOM操作 用jQuery必须先引进jQuery.js文件 js和jQuery写在哪: 1.标签里面 常用就是方法调用 2.写在script标签里面 3.js文件 dom操 ...
- EasyUI Combotree只选择叶子节点
EasyUI Combotree的方法拓展自Combo和Tree.而Tree有一个onBeforSelect事件来帮助我们实现只选择叶子节点的功能. Tree事件需要 'node' 参数,它包括下列属 ...
- POJ-1743 Musical Theme 字符串问题 不重叠最长重复子串
题目链接:https://cn.vjudge.net/problem/POJ-1743 题意 给一串整数,问最长不可重叠最长重复子串有多长 注意这里匹配的意思是匹配串的所有元素可以减去或者加上某个值 ...
- [codevs1048]石子归并&[codevs2102][洛谷P1880]石子归并加强版
codevs1048: 题目大意:有n堆石子排成一列,每次可合并相邻两堆,代价为两堆的重量之和,求把他们合并成一堆的最小代价. 解题思路:经典区间dp.设$f[i][j]$表示合并i~j的石子需要的最 ...
- Java将WKT格式的Geomotry转换成GeoJSON
一.Meven添加依赖 <!-- 引入json处理包 --> <dependency> <groupId>com.alibaba</groupId> & ...