当前位置:   article > 正文

vue 使用iView组件中的Table实现定时自动滚动

vue 使用iView组件中的Table实现定时自动滚动
封装Table

要在css中设置table的高度,使数据过多时出现滚动条,将纵向设置为overflow-y: auto;横向设置隐藏 overflow-x: hidden;

<template>
  <div class="table_container">
    <Table :loading="tableLoading" :columns="columns" :data="dataList" ref="tableL"></Table>
  </div>
</template>

<script>
export default {
  name: "tableList",
  props: {
    columns: {
      type: Array,
      default: () => []
    },
    dataList: {
      type: Array,
      default: () => []
    },
  },
  data () {
    return {
      showContentHeight: 0,
      tableBodyHeight: 0,
      tableLoading: false,
    }
  },
  methods: {
    //动态滚动
    dynamicScroll() {
      let that = this
      this.$nextTick(() => {
        clearInterval(this.timer)

        const table = this.$refs.tableL;
        let tableBody = table.$el.__vue__.$refs.body;

        if (tableBody) {
          let showContentHeight = tableBody.offsetHeight;

          let tableBodyHeight = tableBody.scrollHeight;

          that.showContentHeight = showContentHeight
          that.tableBodyHeight = tableBodyHeight
          if(tableBodyHeight > showContentHeight) {
            that.timerScroll()
          }
        }
      });
    },
    //定时滚动
    timerScroll() {
      let that = this
      const tmpTimer = setInterval(() => {
        const table = that.$refs.tableL;
        let tableBody = table.$el.__vue__.$refs.body;
        if (tableBody) {
          let canScrollHeight = that.tableBodyHeight - that.showContentHeight
          let scrollTop = tableBody.scrollTop
          console.log('scrollTop', scrollTop)
          scrollTop += that.showContentHeight;
          if(scrollTop > canScrollHeight) {
            scrollTop = canScrollHeight
          }

          tableBody.scrollTop = scrollTop;
        }
      }, 5 * 1000);

      this.timer = tmpTimer

      this.$once("hook:beforeDestroy", () => {
        clearInterval(tmpTimer);
      });
    }
  },
}
</script>

<style scoped lang="less">
.table_container {
  height: 100%;
}

.table_container /deep/ .ivu-table-wrapper {
  height: 100%;
  border: none;
  border-bottom: 0;
  border-right: 0;
}

.table_container /deep/ .ivu-table-body {
  height: calc(100% - 40px);	//减掉表头的高度
  overflow-x: hidden;
  overflow-y: auto;
}

.table_container /deep/ .ivu-table-column-center {
  background-color: #39698D;
  color: white;
}

.table_container /deep/ tbody .ivu-table-column-center {
  color: #89D5EA;
}

.table_container /deep/ .ivu-table {
  background-color:rgba(255,255,255, 0);
  color: #89D5EA;
}

.table_container /deep/ .ivu-table td {
  background-color:rgba(255,255,255, 0);
  border-bottom: 1px solid #496893;
}

.table_container /deep/ .ivu-table-tip {
  color: #89D5EA;
}

.table_container /deep/ .ivu-table:before,.table_container /deep/ .ivu-table:after {
  background-color: rgba(255,255,255, 0);
}

.table_container /deep/ .ivu-table th {
  border-bottom: none;
}


/** .ivu-table-body 滚动条样式*/
.table_container /deep/ .ivu-table-body::-webkit-scrollbar {
  /*滚动条整体样式*/
  width: 5px; /*高宽分别对应横竖滚动条的尺寸*/
  height: 3px;
}
.table_container /deep/ .ivu-table-body::-webkit-scrollbar-thumb {
  /*滚动条里面小方块*/
  border-radius: 10px;
  height: 20px;
  -webkit-box-shadow: inset 0 0 5px black;
}
.table_container /deep/ .ivu-table-body::-webkit-scrollbar-track {
  /*滚动条里面轨道*/
  -webkit-box-shadow: inset 0 0 5px #6B90B6;
  border-radius: 10px;
  background: #ffffff;
}
</style>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
在引用组件的页面调用定时滚动方法
<template>
  <div class="layout">
    <table-list ref="tableList" :columns="columns" :data-list="warehouseList"/>
  </div>
</template>

<script>
import { columns } from './config'
import tableList from "@/components/tableList";

export default {
  name: "board",
  components: {
    tableList,
  },
  data () {
    return {
      columns,
      warehouseList: [],
      resultData: {},
    }
  },
  mounted() {
    this.getData()
  },
  methods: {
    getData() {
      getWarehouseList({}).then(res => {
        console.log('getWarehouseList', res)
        if(res.success) {
          this.resultData = res.result
          this.warehouseList = res.result.warehouseList
          const tableList = this.$refs.tableList;
           //动态滚动
          tableList.dynamicScroll()
        }
      })
    }
  }
}
</script>

<style scoped lang="less">
.layout {
  width: 100%;
  height: 100%;
  background:url("../../../assets/prod_board.png") no-repeat center -2px;
  background-size: 100% 100%;
  color: #fff;
}
</style>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/612592
推荐阅读
相关标签
  

闽ICP备14008679号