美文网首页
Vue过滤器

Vue过滤器

作者: 莫以有 | 来源:发表于2019-03-13 22:51 被阅读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>
  • 结果如下图所示:
    结果是个短视频,由视频转成动态图片,我还不会,等我会了,我再补上

相关文章

  • tool.js

    vue时间格式过滤器(今天,昨天,周几,年月日) vue金额过滤器 手机横屏签名功能(vue)

  • 6.Vue过滤器

    Vue过滤器: vue过滤器使用管道 | 进行调用,如:{{name | myFilter}},如果需要传入参数...

  • Vue-04

    过滤器:对显示在页面上的数据进行筛选 全局过滤器 和Vue同级 Vue.filter(“过滤器名称”,func...

  • vue自定义过滤器

    Vue的自定义过滤器有两种:全局过滤器和内部过滤器全局过滤器定义在vue实例化之前 内部过滤器注册在实例内部,仅在...

  • 9.自定义vue全局过滤器

    1.Vue.filter('过滤器名字',过滤器函数):

  • vue 过滤器做字数限制并显示省略号

    定义过滤器 使用vue中的 过滤器filters

  • 关于angular与vue在过滤器方面的不同

    目前看来,angular与vue在过滤器方面差异较大,总的来说angular的过滤器较简单方便,vue的过滤器更像...

  • vue 过滤器filter中this为undefined

    vue过滤器filters 中this为undefined 可以使用computed解决: vue中的过滤器更偏向...

  • Vue过滤器和vue-resource

    过滤器 之前我们学习了Vue的 vue基本指令 进阶学习,我们需要了解Vue的过滤器:Vue.js允许你自定义过滤...

  • vue之自定义过滤器(六)

    一、过滤器介绍:1、在Vue中会通过过滤器(Filters)来渲染数据,使视图可读性更加优雅。2、Vue中的过滤器...

网友评论

      本文标题:Vue过滤器

      本文链接:https://www.haomeiwen.com/subject/gxbxmqtx.html