vue 搜索框 demo

本文介绍了一个使用Vue.js实现的搜索功能案例,展示了如何通过双向数据绑定和计算属性实现动态搜索,以及如何从预设的数据集中筛选并显示匹配项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

主代码:

<form id="main" v-cloak>
 
    <div class="bar">
        <!-- searchString 模型与文本域创建绑定 -->
 
        <input type="text" v-model="searchString" placeholder="输入搜索内容" />
    </div>
 
    <ul>
        <!-- 循环输出数据 -->
             
        <li v-for="article in filteredArticles">
            <a v-bind:href="article.url"><img v-bind:src="article.image" /></a>
            <p>{{article.title}}</p>
        </li>
    </ul>
 
</form>

 

脚本:

var demo = new Vue({
    el: '#main',
    data: {
        searchString: "",
 
        // 数据模型,实际环境你可以根据 Ajax 来获取
 
        articles: [
            {
                "title": "What You Need To Know About CSS Variables",
                "url": "https://www.runoob.com/css/css-tutorial.html",
                "image": "https://static.runoob.com/images/icon/css.png"
            },
            {
                "title": "Freebie: 4 Great Looking Pricing Tables",
                "url": "https://www.runoob.com/html/html-tutorial.html",
                "image": "https://static.runoob.com/images/icon/html.png"
            },
            {
                "title": "20 Interesting JavaScript and CSS Libraries for February 2016",
                "url": "https://www.runoob.com/css3/css3-tutorial.html",
                "image": "https://static.runoob.com/images/icon/css3.png"
            },
            {
                "title": "Quick Tip: The Easiest Way To Make Responsive Headers",
                "url": "https://www.runoob.com/css3/css3-tutorial.html",
                "image": "https://static.runoob.com/images/icon/css3.png"
            },
            {
                "title": "Learn SQL In 20 Minutes",
                "url": "https://www.runoob.com/sql/sql-tutorial.html",
                "image": "https://static.runoob.com/images/icon/sql.png"
            },
            {
                "title": "Creating Your First Desktop App With HTML, JS and Electron",
                "url": "https://www.runoob.com/js/js-tutorial.html",
                "image": "https://static.runoob.com/images/icon/html.png"
            }
        ]
    },
    computed: {
        // 计算数学,匹配搜索
        filteredArticles: function () {
            var articles_array = this.articles,
                searchString = this.searchString;
 
            if(!searchString){
                return articles_array;
            }
 
            searchString = searchString.trim().toLowerCase();
 
            articles_array = articles_array.filter(function(item){
                if(item.title.toLowerCase().indexOf(searchString) !== -1){
                    return item;
                }
            })
 
            // 返回过滤后的数据
            return articles_array;;
        }
    }

效果:

 

源码:

https://github.com/xiamaocheng/vuedemo/tree/master/demo_searchPage

https://www.runoob.com/vue2/vue-examples.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

迅捷的软件产品制作专家

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值