elementui实现搜索下方展示最近搜索的五条数据。要求整体内容的风格为:尽量简单直接一点
使用ElementUI实现搜索下方展示最近搜索的五条数据可以通过以下步骤实现:
- 在页面中添加一个搜索框组件,例如:
<el-input placeholder="请输入搜索内容" v-model="searchValue" @keyup.enter.native="search"></el-input>
其中,searchValue为搜索框中的输入值,search为搜索函数。
- 在search函数中,将搜索框中的输入值加入到最近搜索的数组中,例如:
search() {
// 将搜索值加入到最近搜索数组中
if (this.searchValue) {
this.recentSearches.unshift(this.searchValue)
if (this.recentSearches.length > 5) {
this.recentSearches.pop()
}
}
// 其他搜索逻辑
}
其中,recentSearches为最近搜索的数组,unshift方法可以将值添加到数组头部,pop方法可以删除数组的最后一个元素,保证最近搜索数组中只有5个元素。
- 在页面中添加一个展示最近搜索的列表,例如:
<el-card class="recent-searches">
<div slot="header">最近搜索</div>
<el-tag v-for="(item, index) in recentSearches" :key="index">{{ item }}</el-tag>
</el-card>
其中,el-card和el-tag是ElementUI中的卡片和标签组件,可以用于展示最近搜索的列表。通过v-for指令可以循环展示最近搜索数组中的元素。
- 在页面中添加一些样式,使得最近搜索的列表风格简单直接,例如:
.recent-searches {
margin-top: 20px;
.el-tag {
margin-right: 10px;
margin-bottom: 10px;
padding: 4px 8px;
font-size: 14px;
border-radius: 4px;
}
}
其中,通过设置margin、padding、font-size等样式可以使得最近搜索的列表更加简单直接,同时通过border-radius属性可以使得标签的边角更加圆润。
原文地址: https://www.cveoy.top/t/topic/baeb 著作权归作者所有。请勿转载和采集!