Vue2.0實現1.0的搜索過濾器功能


  Vue2.0刪除了很多1.0的比較實用的過濾器,如filterBy,orderBy。官方文檔給了通過計算屬性實現1.0搜索過濾器功能,自己又加入了大小寫通用檢索功能,比較簡單,學一下。

<body>
    <div class="app">
        <input type="text" v-model="name">
        <ul>
            <li v-for="user in newUsers" >
                {{ user.name }}
            </li>
        </ul>
    </div>
    <script>
        new Vue({
            el: '.app',
            data: {
                name: '',
                users: [
                    { name: 'Bruce' },
                    { name: 'Chuck' },
                    { name: 'Jackie' },
                    { name: '' }
                ] 
            },
            computed: {
                newUsers: function () {
                    var that = this;
                    return that.users.filter(function (user) {
                        return user.name.toLowerCase().indexOf(that.name.toLowerCase()) !== -1;
                    })
                }

            }
        })
    </script>
</body>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM