123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- <style>
- #app{
- width: 500px;
- }
- .close{
- float: right;
- }
- .active{
- background: red;
- }
- </style>
- </head>
- <body>
- <div id="app">
- <h2>todolist</h2>
- <hr>
- <div>
- 名称:<input type="text" v-model="name">
- 价格:<input type="text" v-model="price">
- <button @click="add">提交</button>
- </div>
- <div>
- <input type="text" v-model="seachVal">
- <button @click="search()">搜索</button>
- </div>
- <ul>
- <li v-for="(obj,index) in list" v-show="obj.isShow" :class="{active:obj.isChecked}">
- <input type="checkbox" v-model="obj.isChecked">
- <span>{{obj.name}}</span>
- <span>{{obj.price}}</span>
- <span class="close" @click="del(index)">[X]</span>
- </li>
- </ul>
- <div>
- <button @click="del1()">删除选择</button>
- <span>总价{{total()}}</span>
- </div>
- </div>
- <script src="vue.js"></script>
- <script>
- new Vue({
- el:"#app",
- data:{
- list:[{
- name:'苹果',
- price:5,
- isShow: true,
- isChecked: false
- },{
- name:'香蕉',
- price:6,
- isShow: true,
- isChecked: false
- }],
- name:'',
- price:'',
- seachVal:''
- },
- methods:{
- add(){
- this.list.push({
- name: this.name,
- price: this.price,
- isShow: true,
- isChecked: false
- })
- this.name = ''
- this.price = ''
- },
- del(index){
- this.list.splice(index,1)
- },
- search(){
- this.list.forEach((obj)=>{
- if(obj.name.includes(this.seachVal)){
- obj.isShow = true
- } else {
- obj.isShow = false
- }
- })
- this.seachVal = ''
- },
- total(){
- var sum = 0;
- this.list.forEach(function(obj){
- if(obj.isChecked){
- sum += obj.price*1
- }
- })
- return sum
- },
- del1(){
- this.list = this.list.filter((obj)=>{
- return !obj.isChecked
- })
- console.log(this.list)
- }
-
- }
- })
- </script>
- </body>
- </html>
|