123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- <!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="searchVal">
- <button @click="search">搜索</button>
- </div>
- <ul>
- <li v-for="(obj,index) in list" :class="{active: obj.isChecked}" v-show="obj.isShow">
- <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>
- <span>总价:{{total()}}</span>
- </div>
- </div>
- <script src="./vue.js"></script>
- <script>
- var app = new Vue({
- el: '#app',
- data: {
- list: [{
- name: '苹果',
- price: 5,
- isChecked: false,
- isShow: true
- }, {
- name: '香蕉',
- price: 8,
- isChecked: false,
- isShow: true
- }],
- name: '',
- price: '',
- searchVal: ''
- },
- methods: {
- add() {
- this.list.push({
- name: this.name,
- price: this.price,
- isChecked: false,
- isShow: true
- })
- this.name = ''
- this.price = ''
- },
- total(){
- var sum = 0
- this.list.forEach((obj)=>{
- if(obj.isChecked){
- sum += obj.price * 1
- }
- })
- return sum
- },
- search(){
- this.list.forEach((obj)=>{
- if(obj.name.includes(this.searchVal)){
- obj.isShow = true
- } else {
- obj.isShow = false
- }
- })
- this.searchVal = ''
- },
- del(index){
- this.list.splice(index,1)
- }
- }
- })
- </script>
- </body>
- </html>
|