console
<div id="vfor">
<ul>
<li v-for="(list, index) in items">{{list.name}}-{{list.age}}
<button @click="deletList(index)">删除</button>--
<button @click="update(index, {name: 'pp',age: 11})">更新</button>
</li>
</ul>
<h1>v-for遍历对象</h1>
<ul>
<li v-for="(val, key) in items[1]" :ke="key">{{val}}--{{key}}</li>
</ul>
<h1>v-for列表渲染</h1>
<div>
<input type="text" v-model="inputval" />
<ul>
<li v-for="(p, index) in filterItem">{{index}} -- {{p.name}} -- {{p.age}}</li>
</ul>
<button @click="orderType = 1">年龄升序</button>
<button @click="orderType = 2">年龄降序</button>
<button @click="orderType = 0">原本顺序</button>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
const vm = new Vue({
el: "#vfor",
data: {
inputval: "",
orderType: 0,
items: [
{name: 'haell', age: 12},
{name: 'aaell', age: 14},
{name: 'raell', age: 13}
]
},
methods: {
deletList(index) {
this.items.splice(index, 1)
},
update(index, newP) {
this.items.splice(index,1,newP);
}
},
computed: {
filterItem() {
const {inputval, items, orderType} = this;
let endList;
endList = items.filter(val => val.name.indexOf(inputval) !== -1);
if(orderType !== 0) {
endList.sort((val1, val2) => {
if(orderType == 1) {
return val1.age - val2.age
} else {
return val2.age - val1.age
}
})
}
return endList;
}
}
})
</script>