1 Star 0 Fork 50

绿树白云 / avue-doc

forked from 鹏鹏 / avue-doc 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
crud-big.md 6.75 KB
一键复制 编辑 原始数据 按行查看 历史
smallwei 提交于 2021-01-03 18:22 . v 2.7.5
<script> export default { data() { return { tableData: [], option:{ height:300, menu:false, column:[{ label:'姓名', prop:'name' }] }, currentStartIndex: 0, currentEndIndex: 20 }; }, directives:{ loadmore:{ componentUpdated: function (el, binding, vnode, oldVnode) { // 设置默认溢出显示数量 var spillDataNum = 20; // 设置隐藏函数 var timeout = false; let setRowDisableNone = function (topNum, showRowNum, binding) { if (timeout) { clearTimeout(timeout); } timeout = setTimeout(() => { binding.value.call(null, topNum, topNum + showRowNum + spillDataNum); }); }; setTimeout(() => { const dataSize = vnode.data.attrs['data-size']; const oldDataSize = oldVnode.data.attrs['data-size']; if (dataSize === oldDataSize) return; const selectWrap = el.querySelector('.el-table__body-wrapper'); const selectTbody = selectWrap.querySelector('table tbody'); const selectRow = selectWrap.querySelector('table tr'); if (!selectRow) { return; } const rowHeight = selectRow.clientHeight; let showRowNum = Math.round(selectWrap.clientHeight / rowHeight); const createElementTR = document.createElement('tr'); let createElementTRHeight = (dataSize - showRowNum - spillDataNum) * rowHeight; createElementTR.setAttribute('style', `height: ${createElementTRHeight}px;`); selectTbody.append(createElementTR); // 监听滚动后事件 selectWrap.addEventListener('scroll', function () { let topPx = this.scrollTop - spillDataNum * rowHeight; let topNum = Math.round(topPx / rowHeight); let minTopNum = dataSize - spillDataNum - showRowNum; if (topNum > minTopNum) { topNum = minTopNum; } if (topNum < 0) { topNum = 0; topPx = 0; } selectTbody.setAttribute('style', `transform: translateY(${topPx}px)`); createElementTR.setAttribute('style', `height: ${createElementTRHeight - topPx > 0 ? createElementTRHeight - topPx : 0}px;`); setRowDisableNone(topNum, showRowNum, binding); }) }); } } }, created() { this.getTableData(); }, computed: { filteredData() { let list = this.tableData.filter((item, index) => { if (index < this.currentStartIndex) { return false; } else if (index > this.currentEndIndex) { return false; } else { return true; } }); return list } }, methods: { handelLoadmore(currentStartIndex, currentEndIndex) { this.currentStartIndex = currentStartIndex; this.currentEndIndex = currentEndIndex; }, getTableData() { let cont = 0; let tableData = []; while (cont < 1000) { cont = cont + 1; let object = { name: '王小虎' + cont } tableData.push(object); } setTimeout(() => { this.tableData = tableData; }, 0); } } } </script>

大数据解决方案

:::tip 2.0.0+ ::::

普通用法

:::demo

<avue-crud :data="filteredData" v-loadmore="handelLoadmore" :option="option" :data-size="tableData.length" ></avue-crud>
<script>
export default {
  data() {
    return {
      tableData: [],
      option:{
        height:300,
        menu:false,
        column:[{
          label:'姓名',
          prop:'name'
        }]
      },
      currentStartIndex: 0,
      currentEndIndex: 20
    };
  },
  directives:{
    loadmore:{
      componentUpdated: function (el, binding, vnode, oldVnode) {
        // 设置默认溢出显示数量
        var spillDataNum = 20;

        // 设置隐藏函数
        var timeout = false;
        let setRowDisableNone = function (topNum, showRowNum, binding) {
          if (timeout) {
            clearTimeout(timeout);
          }
          timeout = setTimeout(() => {
            binding.value.call(null, topNum, topNum + showRowNum + spillDataNum);
          });
        };
        setTimeout(() => {
          const dataSize = vnode.data.attrs['data-size'];
          const oldDataSize = oldVnode.data.attrs['data-size'];
          if (dataSize === oldDataSize) return;
          const selectWrap = el.querySelector('.el-table__body-wrapper');
          const selectTbody = selectWrap.querySelector('table tbody');
          const selectRow = selectWrap.querySelector('table tr');
          if (!selectRow) {
            return;
          }
          const rowHeight = selectRow.clientHeight;
          let showRowNum = Math.round(selectWrap.clientHeight / rowHeight);

          const createElementTR = document.createElement('tr');
          let createElementTRHeight = (dataSize - showRowNum - spillDataNum) * rowHeight;
          createElementTR.setAttribute('style', `height: ${createElementTRHeight}px;`);
          selectTbody.append(createElementTR);

          // 监听滚动后事件
          selectWrap.addEventListener('scroll', function () {
            let topPx = this.scrollTop - spillDataNum * rowHeight;
            let topNum = Math.round(topPx / rowHeight);
            let minTopNum = dataSize - spillDataNum - showRowNum;
            if (topNum > minTopNum) {
              topNum = minTopNum;
            }
            if (topNum < 0) {
              topNum = 0;
              topPx = 0;
            }
            selectTbody.setAttribute('style', `transform: translateY(${topPx}px)`);
            createElementTR.setAttribute('style', `height: ${createElementTRHeight - topPx > 0 ? createElementTRHeight - topPx : 0}px;`);
            setRowDisableNone(topNum, showRowNum, binding);
          })
        });
      }
    }
  },
  created() {
    this.getTableData();
  },
  computed: {
    filteredData() {
      let list = this.tableData.filter((item, index) => {
        if (index < this.currentStartIndex) {
          return false;
        } else if (index > this.currentEndIndex) {
          return false;
        } else {
          return true;
        }
      });
      return list
    }
  },
  methods: {
    handelLoadmore(currentStartIndex, currentEndIndex) {
      this.currentStartIndex = currentStartIndex;
      this.currentEndIndex = currentEndIndex;
    },
    getTableData() {
      let cont = 0;
      let tableData = [];
      while (cont < 10000) {
        cont = cont + 1;
        let object = {
          name: '王小虎' + cont
        }
        tableData.push(object);
      }
      setTimeout(() => {
        this.tableData = tableData;
      }, 0);
    }
  }
}
</script>

:::

马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/greentreecloud/avue-doc.git
git@gitee.com:greentreecloud/avue-doc.git
greentreecloud
avue-doc
avue-doc
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891