<!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自动计算的更多相关文章

  1. vue computed 原理

    vue computed 主要依靠数据依赖来更新,这里不展示computed源代码,只展示核心思想. computed: { a(){ return this.b ++ } } data:{ b: 1 ...

  2. Vue computed props pass params

    Vue computed props pass params vue 计算属性传参数 // 计算 spreaderAlias spreaderAlias () { console.log('this. ...

  3. vuex bug & vue computed setter

    vuex bug & vue computed setter https://vuejs.org/v2/guide/computed.html#Computed-Setter [Vue war ...

  4. vue computed、methods、watch的区别

    1.computed(计算属性)computed是计算属性,事实上和和data对象里的数据属性是同一类的(使用上), 2.methods(方法)写在html中的时候需要带()支持传参,且需要有触发条件 ...

  5. Vue computed属性

    computed vs methods 我们可以使用Vue中的method计算出学科的总分,最终得到的总数结果是相同的. 在上例的基础上,我们把computed区块中的totalMarks函数整体移到 ...

  6. 深入理解 Vue Computed 计算属性

    Computed 计算属性是 Vue 中常用的一个功能,我们今天来说一下他的执行过长 拿官网简单的例子来看一下: <div id="example"> <p> ...

  7. vue computed的执行问题

    1.在new Vue()的时候,vue\src\core\instance\index.js里面的_init()初始化各个功能 function Vue (options) { if (process ...

  8. vue computed 可以使用getter和setter

    var vm = new Vue({ data: { a: 1 }, computed: { // 仅读取 aDouble: function () { return this.a * 2 }, // ...

  9. Vue -computed传参数

    vue 中computed想传递参数怎么办? 闭包在这里起到的重要的作用 <input v-model="newItem(key,val)" type="text& ...

随机推荐

  1. 2015多校联合训练赛hdu 5301 Buildings 2015 Multi-University Training Contest 2 简单题

    Buildings Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Tota ...

  2. 汇编 -- Hook API (MessageBoxW)

    说到HOOK.我看了非常多的资料和教程.无奈就是学不会HOOK.不懂是我的理解能力差.还是你们说的 不够明确,直到我看了下面这篇文章,最终学会了HOOK: http://blog.sina.com.c ...

  3. fieldset 标签 -- 对表单进行分组

    转自:https://xhmaomy-163-com.iteye.com/blog/1066977 fieldset——一个不常用的HTML标签 fieldset 标签 -- 对表单进行分组 在for ...

  4. shiro动态控制url资源

    怎么利用shiro权限动态控制每个url资源呢?主要包括jsp(html)页面.action的url访问,而静态资源和登录资源则可直接访问. 所谓动态控制url就是url的权限控制不是手动写死在配置文 ...

  5. BootStrap学习(一)——BootStrap入门

    1.环境搭建 中文官网下载地址:http://www.bootcss.com/ 右击选中的WEB项目,点击导入,选择文件系统,然后下一步,选择BootStrap文件目录路径,如下: 完成后,WEB项目 ...

  6. 【转载】犀利的 oracle 注入技术

    介绍一个在web上通过oracle注入直接取得主机cmdshell的方法. 以下的演示都是在web上的sql plus执行的,在web注入时 把select SYS.DBMS_EXPORT_EXTEN ...

  7. Uva 1605 Building for UN【构造法】

    题意:给出n个国家,给它们分配办公室,使得任意两个国家都有一对相邻的格子 看的紫书,最开始看的时候不理解 后来还是搜了题解--- 发现是这样的 比如说5个国家 应该输出 AAAA BBBB CCCC ...

  8. JS自定义全局Error

    <script> ///自定义错误 onerror=handleErr; function handleErr(msg,url,l) { var txt=""; txt ...

  9. hadoop从wordCount开始

    最近一段时间大数据很火,我有稍微有点java基础,自然选择了由java编写的hadoop框架,wordCount是hadoop中类似于java中helloWorld的存在,自然不能错过. packag ...

  10. BZOJ 3325 [SCOI2013]密码 (逆模拟Manacher+构造)

    题目大意:给你一个字符串每个位置和相邻两个位置为回文中心的最长回文串长度,让你构造一个合法的字典序最小的字符串 挺有意思的构造题 首先按照$Manacher$的思想还原$p$数组 定义$f_{ij}$ ...