vue 列表渲染 filter

添加链接描述

filter()、concat() 和 slice()。不会变更原始数组,而总是返回一个新数组

我们想要显示一个数组经过过滤或排序后的版本,而不实际变更或重置原始数据
可以在原有数据属性的基础上,添加一个新属性,在渲染的时候使用的是这个新属性

    <div id="app">
        <button @click="chooseAll">all</button>
        <button @click="chooseUnderage">under age</button>
        <button @click="chooseGuangzhou">Guangzhou</button>
        <div v-for="item in showData">
            {{item.name}} is {{item.age}} and living in {{item.city}}.
        </div>
    </div>

       new Vue({
            el: '#app',
            data() {
                return {
                    showData: [],
                    studentMsg: [{
                        name: "Amy",
                        age: 12,
                        city: "Guangzhou"
                    }, {
                        name: "Cindy",
                        age: 22,
                        city: "Beijing"
                    }, {
                        name: "Linda",
                        age: 19,
                        city: "Guangzhou"
                    }, {
                        name: "Peter",
                        age: 45,
                        city: "Shanghai"
                    }, ]

                }
            },
            methods: {
                chooseAll: function() {
                    this.showData = this.studentMsg
                },
                chooseUnderage: function() {
                    this.showData = this.studentMsg.filter(student => {
                        return student.age < 18
                    })
                },
                chooseGuangzhou: function() {
                    this.showData = this.studentMsg.filter(student => {
                        return student.city == 'Guangzhou'
                    })
                },
            },

            //生命周期函数
            created: function() {
                this.showData = this.studentMsg
            },

            //计算属性
            computed: {

            },
            watch: {

            },

        })
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue中,filter()方法用于对数据进行过滤操作。它可以接收一个函数作为参数,该函数将应用于数组的每个元素,返回一个新的数组。 下面是一个例子: ```javascript // 定义一个数组 let numbers = [1, 2, 3, 4, 5]; // 定义一个函数,将数组中的每个元素乘以2 function double(value) { return value * 2; } // 使用filter()方法将数组中的每个元素乘以2 let doubledNumbers = numbers.filter(double); console.log(doubledNumbers); // [2, 4, 6, 8, 10] ``` 在Vue中,filter()方法可以用来对列表数据进行过滤、排序等操作,从而实现数据的动态渲染和展示。在Vue的模板中,可以使用过滤器来调用filter()方法,如下所示: ```html <template> <div> <ul> <li v-for="item in filteredItems" :key="item.id"> {{ item.name }} </li> </ul> </div> </template> <script> export default { data() { return { items: [ { id: 1, name: 'apple' }, { id: 2, name: 'banana' }, { id: 3, name: 'orange' }, { id: 4, name: 'pear' }, ], searchText: '', }; }, computed: { filteredItems() { return this.items.filter(item => item.name.includes(this.searchText)); }, }, }; </script> ``` 在上面的例子中,我们定义了一个items数组和一个searchText变量,用于存储用户输入的搜索关键字。使用computed属性,我们定义了一个filteredItems计算属性,它使用filter()方法对items进行了过滤操作,只返回包含搜索关键字的数据项。在模板中,我们使用v-for指令遍历filteredItems数组,并渲染每个数据项的名称。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值