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& ...
随机推荐
- [ZJOJ2014] 力 解题报告 (FFT)
题目链接: https://www.luogu.org/problemnew/show/P3338 题目: 给出$n$个数$q_i$,令$F_j=\sum_{i<j}\frac{q_iq_j}{ ...
- 关于markdown的使用
首先: https://www.cnblogs.com/jordangong/p/9804777.html 注意:提交博客时需将 Markdown 源码粘贴到编辑器中,且编辑器没有实时预览,可以上传后 ...
- java监听多个组件
import java.awt.Color; import java.awt.FlowLayout; import java.awt.event.*; import javax.swing.*; pu ...
- PIC18F26K20
Clock Four Crystal modes, Two External clock modes, Two RC Oscillator, Internal oscillator, PLL
- CSS3中的transition
W3C标准中对CSS3的transition是这样描述的: CSS的transition允许CSS的属性值在一定的时间区间内平滑地过渡.这种效果可以在鼠标单击,获得焦点,被点击或对元素任何改变中触发, ...
- 不实例化一个 class 的时候使用它的property
class A: @property def name(self): " print(A.name) # <property object at 0x10d54cf98> cla ...
- The Node.js Event Loop, Timers, and process.nextTick() Node.js事件循环,定时器和process.nextTick()
个人翻译 原文:https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/ The Node.js Event Loop, Ti ...
- Vue学习之v-if与v-show的区别
v-if和v-show具有类似的功能,不过v-if才是真正的条件渲染,他会根据表达式适当的销毁或重建元素及绑定事件或子组件.若表达式初始值为false,则一开始元素或组件不会渲染,只有当第一次为真时, ...
- mysql 临时表和内存表
查看内存表的最大值: show variables like '%heap%'; mysql> show variables like '%heap%'; +------------------ ...
- 如何使用外部插件picker
近日有需求做一个职业选择弹框,在网上搜了半天也没合适的: 暴躁大佬协助我DIY一个插件,直接使用,顺滑流畅,随心所欲!特别鸣谢@一样菜 不多BB了,直接撸代码: 引用写在上面: /* 更改职业 */ ...