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">
  <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, '' + this.searchText + '')
              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>

你可能感兴趣的:(vue.js,javascript,elementui,前端)