<!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>