Vue2.0 v-for filter

发布时间:2019-05-14 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了Vue2.0 v-for filter脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

Vue2.0 v-for filter列表过滤功能

习惯使用angularjs的一定知道angularjs有一个ng-repeat filter的例子,可以很简单的过滤一个循环列表。而在使用VUE2.0的时候发现不能在v-for中使用filter功能。所以这里就来讨论一下如何实现这个列表过滤功能。

解决思路使用计算属性

使用计算属性
app.js

var app5 = new Vue({
    el: '#app5',
    data: {
        shoppingList: [
            "Milk", "Donuts", "Cookies", "Chocolate", "Peanut Butter", "Pepto Bismol", "Pepto Bismol (Chocolate flavor)", "Pepto Bismol (Cookie flavor)"
        ],
        key: ""
    },
    computed: {
        filterShoppingList: function () {
            // `this` points to the vm instance
            var key = this.key;
            var shoppingList = this.shoppingList;
            return shoppingList.filter(function (item) {
                return item.toLowerCase().indexOf(key.toLowerCase()) != -1
            });;
        }
    }
})

app.html

<div id="app5">
        <h2>Vue-for</h2>
        <ul>
            <li v-for="item in shoppingList">
                {{ item }}
            </li>
        </ul>
        <h2>Vue-for filter实现</h2>
        <ul>
            Filter Key<input type="text" v-model="key">   
            <li v-for="item in filterShoppingList">
                {{ item }}
            </li>
        </ul>        
    </div>    

最终效果实现了根据关键字来过滤列表的功能。

Vue2.0 v-for filter

脚本宝典总结

以上是脚本宝典为你收集整理的Vue2.0 v-for filter全部内容,希望文章能够帮你解决Vue2.0 v-for filter所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:Vue