Vue过滤器

2019-03-13  本文已影响0人  莫以有

过滤器不改变原始数据,只是对数据进行加工处理后返回过滤后的数据再进行调用处理

过滤器简单实例--搜索页面

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>computed练习-搜索页面的实现</title>
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
        <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
        <style type="text/css">
            .container {
                flex-direction: column;
                width: 60%;
                margin: 0 auto;
            }

            .inputbox {
                width: 45%;
                margin: 0 auto;
                height: 30px;
                border-radius: 10px;
            }

            .article {
                display: flex;
                height: 130px;
                border: 1px solid darkgray;
                border-radius: 8px;
                margin-bottom: 8px;
                align-items: center;
                font-family: 华文行楷;
                font-size: 20px;
            }

            .article-title {
                flex: 1 1 80%;
                margin-left: 10px;
            }

            .article-thumbnail {
                flex: 1 1 20%;
            }

            .article-thumbnail img {
                border-radius: 10px;
                width: 80%;
                height: 60%;
            }
            .sousuo {
                color: white;
                background-color: skyblue;
                width: 100px;
                height: 30px;
                border-radius: 10px;
            }
            a {
                text-decoration: none;
                color: #333333;
            }
            a:hover {
                text-decoration: underline;
            }
        </style>
    </head>
    <body>
        <div id="app">
            <div class="container">
                <hr>
                <input type="text" v-model="name" placeholder="请输入" class="inputbox" />
                <button type="button" @click="search" class="sousuo">搜索</button>
                <div class="article" v-for="article in filterArticles">
                    <a :href="article.url" class="article-title">
                        {{article.title}}
                    </a>
                    <div class="article-thumbnail">
                        <a :href="article.url">
                            <img :src="article.image">
                        </a>
                    </div>
                </div>
            </div>
        </div>
        <script type="text/javascript">
            var app = new Vue({
                el: '#app',
                data: {
                    show: false,
                    name: '',
                    searchString: "",
                    articles: [{
                            "title": "堪称神器的3款在线工具,你一定用得上!",
                            "url": "https://www.jianshu.com/p/e83e7999346b",
                            "image": "https://img.haomeiwen.com/i11438996/56b25f32c9307b4b?imageMogr2/auto-orient/strip%7CimageView2/2/w/640/format/webp"
                        },
                        {
                            "title": "经典面试题:从 URL 输入到页面展现到底发生什么?",
                            "url": "https://www.jianshu.com/p/45ba3e0d0c7e",
                            "image": "https://img.haomeiwen.com/i3973862/d90954249a6f6ccd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp"
                        },
                        {
                            "title": "如何免翻墙使用谷歌搜索和Chrome应用商店",
                            "url": "https://www.jianshu.com/p/484f8e6c88f6",
                            "image": "https://img.haomeiwen.com/i858154/015a4b083685a3d1.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/800/format/webp"
                        },
                        {
                            "title": "四款前所未有好用的黑科技APP,绝对的良心实用,赶紧告诉家人",
                            "url": "https://www.jianshu.com/p/2aec84d269fe",
                            "image": "https://img.haomeiwen.com/i16042993/168b2cb17fd7ec0c?imageMogr2/auto-orient/strip%7CimageView2/2/w/640/format/webp"
                        },
                        {
                            "title": "坚持学英语的方法有哪些",
                            "url": "https://www.jianshu.com/p/0a6a61b0933c",
                            "image": "https://img.haomeiwen.com/i3525704/c7293758fc59e56b.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/960/format/webp"
                        }
                    ]
                },
                methods: {
                    search: function() {
                        this.searchString = this.name;
                    }
                },
                computed: {
                    filterArticles: function() {
                        var filterArray = this.articles;
                        var searchString = this.searchString;
                        //若搜索关键词为空,则返回原始数据集
                        if (!searchString) {
                            return filterArray;
                        } else {
                            //去除一些无用空格,并转为小写
                            searchString = searchString.trim().toLowerCase();
                            //过滤数组内容
                            filterArray = filterArray.filter(function(article) {
                                //找到了标题含有searchString的文章
                                if (article.title.toLowerCase().indexOf(searchString) != -1) {
                                    return article;
                                }
                            })
                        } //返回最终的过滤数组
                        return filterArray;
                    }
                }
            })
        </script>
    </body>
</html>
上一篇 下一篇

猜你喜欢

热点阅读