Vue.js -- 过滤器

VueJs中的过滤器基础

过滤器是一个通过输入数据,能够及时对数据进行处理并返回一个数据结果的简单函数。Vue有很多很便利的过滤器,可以参考官方文档, http://cn.vuejs.org/api/#过滤器 ,过滤器通常会使用管道标志 “ | ”, 比如:

{{ msg | capitalize }}
// 'abc' => 'ABC'

uppercase过滤器 : 将输入的字符串转换成大写字母的过滤器。

VueJs允许你链式调用过滤器,简单的来说,就是一个过滤器的输出成为下一个过滤器的输入,然后再次过滤。接下来,我们可以想象一个比较简单的例子,使用了Vue的 filterBy + orderBy 过滤器来过滤所有商品products。过滤出来的产品是属于水果类商品。

filterBy过滤器 : 过滤器的值必须是一个数组,filterBy + 过滤条件。过滤条件是:'string || function'+ in 'optionKeyName'

orderBy过滤器 : 过滤器的值必须是一个数组,orderBy + 过滤条件。过滤条件是:'string || array ||function' + 'order ≥ 0 为升序 || order < 0 为降序'

接下来,我们可以看下下面这个例子:我们有一个商品数组products,我们希望遍历这个数组,并把他们打印成一张清单,这个用v-for很容易实现。

<ul class="product">
    <li v-for="product in products|filterBy '水果' in 'category' |orderBy 'price' 1">
        {{product.name}}-{{product.price | currency}}
    </li>
</ul>

上面的例子,就是用filterBy 过滤在 'category'中含有'水果' 关键字的列表,返回的列表就是只含有 '水果' 关键字的列表,而orderBy过滤器是根据价格做了一个升序,如果想要降序,只需要加一个小于0的参数;

自定义过滤器

虽然VueJs给我们提供了很多强有力的过滤器,但有时候还是不够。值得庆幸的,Vue给我们提供了一个干净简洁的方式来定义我们自己的过滤器,之后我们就可以利用管道 “ | ” 来完成过滤。

定义一个全局的自定义过滤器,需要使用Vue.filter()构造器。这个构造器需要两个参数。

Vue.filter() Constructor Parameters:

1.filterId: 过滤器ID,用来做为你的过滤器的唯一标识;

2.filter function: 过滤器函数,用一个function来接收一个参数,之后再将接收到的参数格式化为想要的数据结果。

上面的例子中,我们要实现商品价格打5折该怎么做呢?其实就是实现的一个自定义的过滤器,表示将商品的价格打了5折;而要实现它,需要完成的是:

a、使用Vue.filter()构造器创建一个过滤器叫做discount

b、输入商品的原价,能够返回其打五折之后的折扣价

代码见下面:

Vue.filter('discount', function(value) {
    return value * .5;
});
var product = new Vue({
    el: '.product',
    data: {
        products: [
            {name: '苹果',price: 25,category: "水果"}, 
            {name: '香蕉',price: 15,category: "水果"}, 
            {name: '雪梨',price: 65,category: "水果"}, 
            {name: '宝马',price: 2500,category: "汽车"},
            {name: '奔驰',price: 10025,category: "汽车"}, 
            {name: '柑橘',price: 15,category: "水果"}, 
            {name: '奥迪',price: 25,category: "汽车"}
        ]
    },
})

现在就可以像使用Vue自带的过滤器一样使用自定义过滤器了

<ul class="product">
    <li v-for="product in products|filterBy '水果' in 'category' |orderBy 'price' 0">
        {{product.name}}-{{product.price|discount | currency}}
    </li>
</ul>

上面代码实现的商品打5折,而如果要实现价格打任意折扣呢?应该在discount过滤器里增加一个折扣数值参数,改造一下我们的过滤器。

Vue.filter('discount', function(value, discount) {
    return value * (discount / 100);
});

然后重新调用我们的过滤器

<ul class="product">
    <li v-for="product in products|filterBy '水果' in 'category' |orderBy 'price' 0">
        {{product.name}}-{{product.price|discount 25 | currency}}
    </li>
</ul>

我们也可以在我们Vue实例里构造我们的过滤器,这样构造的好处是,这样就不会影响到其他不需要用到这个过滤器的Vue实例。

/*定义在全局 
Vue.filter('discount', function(value,discount) {
    return value  * ( discount / 100 ) ;
});
*/
var product = new Vue({
    el: '.product',
    data: {
        products: [
            {name: '苹果',price: 25,category: "水果"}, 
            {name: '香蕉',price: 15,category: "水果"}, 
            {name: '雪梨',price: 65,category: "水果"}, 
            {name: '宝马',price: 2500,category: "汽车"},
            {name: '奔驰',price: 10025,category: "汽车"}, 
            {name: '柑橘',price: 15,category: "水果"}, 
            {name: '奥迪',price: 25,category: "汽车"}
        ]
    },
    //自定义在实例
    filters: {
        discount: function(value, discount) {
            return value * (discount / 100);
        }
    }
})

定义在全局就能在所有的实例中调用过滤器,如果定义在了实例里就在实例里调用过滤器。

posted @ 2016-09-06 17:21  Jone_chen  阅读(31471)  评论(2编辑  收藏  举报