el-table实现搜索高亮展示并滚动到元素位置的操作代码
更新时间:2024年01月19日 11:35:34 作者:木叶随风而逝
这篇文章主要介绍了el-table实现搜索高亮展示并滚动到元素位置,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
效果展示:
代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css" rel="external nofollow" > <style> *{padding: 0;margin: 0;} #app{padding: 30px;} .searchBox{ width: 100%; display: flex; align-items: center; margin: 20px 0; position: fixed; z-index: 9; } </style> </head> <body> <div id="app"> <div class="searchBox"> <el-input v-model="searchText" style="width:200px;" placeholder="请输入关键字" prefix-icon="el-icon-search" clearable @clear="search" size="small" @keyup.enter.native="search" > </el-input> <el-button type="primary" size="small" icon="el-icon-search" @click="search" ></el-button> </div> <el-table :data="tableData" style="width: 100%;margin-top: 70px;" border> <el-table-column label="序号" width="50px" align="center"> <template slot-scope="scope"> {{scope.$index+1}} </template> </el-table-column> <el-table-column prop="date" label="日期" width="180"> </el-table-column> <el-table-column prop="name" label="姓名" width="180"> <template slot-scope="scope"> <div :dataText="scope.row.name" class="keywordName">{{scope.row.name}}</div> </template> </el-table-column> <el-table-column prop="address" label="地址"> </el-table-column> </el-table> </div> <!-- 记得引入vue2 --> <script src="./vue.min.js"></script> <script src="https://unpkg.com/element-ui/lib/index.js"></script> <script> new Vue({ el:'#app', data(){ return{ tableData: [ {date: '2016-05-02',name: '王小虎',address: '上海市普陀区金沙江路 1518 弄'}, {date: '2016-05-04',name: '王小虎', address: '上海市普陀区金沙江路 1517 弄'}, {date: '2016-05-01',name: '王小虎',address: '上海市普陀区金沙江路 1519 弄'}, {date: '2016-05-03',name: '王小虎',address: '上海市普陀区金沙江路 1516 弄'}, {date: '2016-05-03',name: '张三',address: '上海市普陀区金沙江路 1516 弄'}, {date: '2016-05-03',name: '李四',address: '上海市普陀区金沙江路 1516 弄'}, {date: '2016-05-03',name: '王五',address: '上海市普陀区金沙江路 1516 弄'}, {date: '2016-05-03',name: '王五',address: '上海市普陀区金沙江路 1516 弄'}, {date: '2016-05-03',name: '王五',address: '上海市普陀区金沙江路 1516 弄'}, {date: '2016-05-03',name: '王五',address: '上海市普陀区金沙江路 1516 弄'}, {date: '2016-05-03',name: '王五',address: '上海市普陀区金沙江路 1516 弄'}, {date: '2016-05-03',name: '王五',address: '上海市普陀区金沙江路 1516 弄'}, {date: '2016-05-03',name: '王五',address: '上海市普陀区金沙江路 1516 弄'}, {date: '2016-05-03',name: '王五',address: '上海市普陀区金沙江路 1516 弄'}, {date: '2016-05-03',name: '王五',address: '上海市普陀区金沙江路 1516 弄'}, {date: '2016-05-03',name: '王五',address: '上海市普陀区金沙江路 1516 弄'}, {date: '2016-05-03',name: '王五',address: '上海市普陀区金沙江路 1516 弄'}, {date: '2016-05-03',name: '王五',address: '上海市普陀区金沙江路 1516 弄'}, {date: '2016-05-03',name: '王五',address: '上海市普陀区金沙江路 1516 弄'}, {date: '2016-05-03',name: '王五',address: '上海市普陀区金沙江路 1516 弄'}, {date: '2016-05-03',name: '王五',address: '上海市普陀区金沙江路 1516 弄'}, {date: '2016-05-03',name: '王五',address: '上海市普陀区金沙江路 1516 弄'}, {date: '2016-05-03',name: '王五',address: '上海市普陀区金沙江路 1516 弄'}, {date: '2016-05-03',name: '王五',address: '上海市普陀区金沙江路 1516 弄'}, {date: '2016-05-03',name: '王五',address: '上海市普陀区金沙江路 1516 弄'}, {date: '2016-05-03',name: '王五',address: '上海市普陀区金沙江路 1516 弄'}, {date: '2016-05-03',name: '王五',address: '上海市普陀区金沙江路 1516 弄'}, {date: '2016-05-03',name: '王五',address: '上海市普陀区金沙江路 1516 弄'}, {date: '2016-05-03',name: '王五',address: '上海市普陀区金沙江路 1516 弄'}, {date: '2016-05-03',name: '赵六',address: '上海市普陀区金沙江路 1516 弄'}, {date: '2016-05-03',name: '王五',address: '上海市普陀区金沙江路 1516 弄'}, {date: '2016-05-03',name: '王五',address: '上海市普陀区金沙江路 1516 弄'}, {date: '2016-05-03',name: '王五',address: '上海市普陀区金沙江路 1516 弄'}, {date: '2016-05-03',name: '王五',address: '上海市普陀区金沙江路 1516 弄'}, {date: '2016-05-03',name: '王五',address: '上海市普陀区金沙江路 1516 弄'}, {date: '2016-05-03',name: '王五',address: '上海市普陀区金沙江路 1516 弄'}, {date: '2016-05-03',name: '王五',address: '上海市普陀区金沙江路 1516 弄'}, {date: '2016-05-03',name: '王五',address: '上海市普陀区金沙江路 1516 弄'}, {date: '2016-05-03',name: '王五',address: '上海市普陀区金沙江路 1516 弄'}, {date: '2016-05-03',name: '王五',address: '上海市普陀区金沙江路 1516 弄'}, {date: '2016-05-03',name: '赵六',address: '上海市普陀区金沙江路 1516 弄'}, {date: '2016-05-03',name: '王五',address: '上海市普陀区金沙江路 1516 弄'}, {date: '2016-05-03',name: '王五',address: '上海市普陀区金沙江路 1516 弄'}, {date: '2016-05-03',name: '王五',address: '上海市普陀区金沙江路 1516 弄'}, {date: '2016-05-03',name: '王五',address: '上海市普陀区金沙江路 1516 弄'}, {date: '2016-05-03',name: '王五',address: '上海市普陀区金沙江路 1516 弄'}, {date: '2016-05-03',name: '王五',address: '上海市普陀区金沙江路 1516 弄'}, {date: '2016-05-03',name: '王五',address: '上海市普陀区金沙江路 1516 弄'}, {date: '2016-05-03',name: '王五',address: '上海市普陀区金沙江路 1516 弄'}, {date: '2016-05-03',name: '赵六',address: '上海市普陀区金沙江路 1516 弄'}, ], // searchText: '', } }, mounted() { // scroll代表滚动条距离页面顶部距离 // window.addEventListener('scroll', this.dataScroll) }, methods:{ search(){ let main = document.querySelectorAll('.keywordName') var num = 0 main.forEach(item => { //重置上次搜索的背景色 item.innerHTML = item.getAttribute('dataText') //便利找到匹配项,加背景颜色 if(item.innerHTML.indexOf(this.searchText) != -1 && num === 0){ num++ const reg = new RegExp(this.searchText, 'g') let dom = item.innerHTML.replace(reg, '<span class="searchTexts" style="background-color: yellow">' + this.searchText + '</span>') item.innerHTML = dom } }) // 替换配置 this.getSearchList() }, getSearchList() { var dom = document.querySelectorAll('.searchTexts') //滚动到屏幕位置 if(dom.length){ dom[0].scrollIntoView({ block: 'start', behavior: 'smooth' }) } }, dataScroll() { this.scroll = document.documentElement.scrollTop || document.body.scrollTop }, } }) </script> </body> </html>
到此这篇关于el-table实现搜索高亮展示并滚动到元素位置的操作代码的文章就介绍到这了,更多相关el-table搜索高亮展示内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
vue-resource post数据时碰到Django csrf问题的解决
这篇文章主要介绍了vue-resource post数据时碰到Django csrf问题的解决,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2020-03-03
最新评论