便利贴--22{Avue表格自定义以及适应高度}

16 篇文章 0 订阅
6 篇文章 0 订阅

store-只需要给自己的表格加上class

<avue-crud
class=“tablesss”
这加上class

// import {getStore, setStore} from '@/util/store'

// import {getDictionary} from '@/api/system/dict'
let screenSizes = {
  w: window.screen.width,
  h: window.screen.height
};
let innerWH = {
  innerWidth: window.innerWidth,
  innerHeight: window.innerHeight
};
const control = {
  state: {
    clearOtherBut: {
      refreshBtn: false,
      columnBtn: false,
      searchShowBtn: false,
      filterBtn: false,
      addBtn: false,
      editBtn: false,
      viewBtn: false,
      delBtn: false,
      searchIndex: 6, //收缩展示数量
      searchIcon: true, //搜索是否收缩
      height: window.screen.height //初始高度
    },
    changePageSize: {
      pageSize: 15,
      currentPage: 1,
      total: 0,
      pageSizes: [15, 30, 50, 100]
    },
    windowWidth: innerWH.innerWidth,
    windowHeight: innerWH.innerHeight,
    tableWindowHeight: innerWH.innerHeight,
    heightTimesOut: "",
    table_height: "",
    butState: "",
    oldOpens: "",
    screenSize: screenSizes.w <= 1366 && screenSizes.h <= 768,
    screenHeight: 1000,

    searchOpen: {
      searchIndex: 6, //收缩展示数量
      searchIcon: true, //搜索是否收缩
      height: window.screen.height //初始高度
    }
  },
  actions: {
    WHchangeHight({
      state,
      commit
    }, data) {
      if (data.fn) {
        if (!state.heightTimesOut) {
          data.fn(state.windowHeight);
        } else {
          state.heightTimesOut = setTimeout(() => {
            clearTimeout(state.heightTimesOut);
            state.heightTimesOut = "";
            data.fn(state.windowHeight);
          }, 500);
        }
      }
    },

    TABLE_CHANGEBUTPOSITION({
      state,
      commit,
      dispatch
    }, data) {
      let table = document.getElementsByClassName("tablesss")[0];
      let heard = table.getElementsByClassName("avue-crud__search")[0]; //获取搜索的长度
      let buts = table.getElementsByClassName("avue-form__menu--center")[0];
      let Open = buts.getElementsByClassName("el-button--text")[0];
      let butsheight = heard.clientHeight;
      let other = data.otherclass || [];
      // buts.style.transition = "all 0.1s";
      if (buts) {
        buts.style.top = butsheight - 3 + "px";
      }
      if (other && state.windowWidth >= 1024) {
        for (let k in other) {
          let otherbuts = document.getElementsByClassName(other[k])[0];
          // otherbuts.style.transition = "all 0.1s";
          if (otherbuts) {
            otherbuts.style.top = butsheight + 26 + "px";
          }
        }
      }
      // Open.style.transition = "all 0.1s";
      // Open.style.top = -(butsheight + 4) + "px";
      // 添加事件
      if (Open) {
        Open.style.top = (state.screenSize ? 125 : 140) + "px";
        Open.onclick = function (e) {
          // changedom(e.srcElement.innerText);
          //固定高度
          //调用改变高度
          if (e.srcElement.innerText == "展 开") {
            state.tableWindowHeight += 1;
          } else {
            state.tableWindowHeight -= 1;
          }
          // commit("setButOpens", e.srcElement.innerText);
        };
      }
    },
    TABLE_CHANGEHEIGHT({
      state,
      commit
    }, data) {
      let h = +state.windowHeight - (+data.searchHeight + +data.flowHeight);
      console.log(h);
      state.table_height = h;
      // commit("settable_height", h);
    },
    changeZoom({
      state,
      commit,
      dispatch
    }, data) {
      if (window.innerWidth <= 1396 && window.innerHeight <= 798) {
        // var devicewidth = document.documentElement.clientWidth; //获取当前分辨率下的可是区域宽度
        // console.log(devicewidth)
        let devicewidth = 1143;
        var scale = devicewidth / 1366; // 分母——设计稿的尺寸
        document.body.style.zoom = scale; //放大缩小相应倍数
        // console.log("应用1366*768")
      } else {
        if (document.body.style.zoom != 1) {
          document.body.style.zoom = 1;
          // console.log("还原缩放")
        }
      }
      // return;
      let usheight = 0;
      //监听可视高度
      Object.defineProperty(state, "tableWindowHeight", {
        set: function (key, value) {
          //   //此处拦截了设置请求
          // window.console.log(key, value, "tableWindowHeight");
          usheight = key;
          let d = {
            fn: function (h) {
              let doit = (height, val) => {
                let table = document.getElementsByClassName("tablesss");
                // console.log(table[0]);
                if (table[0]) {
                  //有需要调整的表格
                  let heard = table[0].getElementsByClassName(
                    "avue-crud__search"
                  );
                  let tip = document.getElementsByClassName("avue-crud__tip")[0];
                  let usNum = state.screenSize ? 165 : 302;
                  if (!tip) {
                    // console.log("没有tip")
                    usNum = usNum - 50;
                  }
                  let pagination = document.getElementsByClassName("avue-crud__pagination")[0];
                  if (!pagination) {
                    // console.log("没有分页")
                    usNum = usNum - 45;
                  }
                  let nav_scroll = document.getElementsByClassName("el-tabs__nav-scroll");
                  if (nav_scroll[1]) {
                    // console.log("有额外标签栏")
                    usNum = usNum + 55;
                  }
                  let hs = height || h;
                  let overHeight = +hs - (+heard[0].clientHeight + +usNum);
                  // console.log(overHeight, heard[0].clientHeight, usNum);
                  let fromDom = document.getElementsByClassName(
                    "el-table--fit"
                  );
                  if (fromDom) {
                    let ifsize = fromDom.length;
                    // console.log("页面表格数量:", ifsize)
                    for (let ifrom = 0; ifrom < ifsize; ifrom++) {
                      // fromDom[ifrom].style.transition = "height 0.1s";
                      fromDom[ifrom].style.maxHeight = overHeight + "px";
                      fromDom[ifrom].style.height = overHeight + "px";
                    }
                  }
                  let d = {
                    otherclass: ["downSFZ"],
                    haveOpen: true
                  };
                  dispatch("TABLE_CHANGEBUTPOSITION", d);
                }
                return;
              };
              // if (that.onceTableChange == 1) {
              //   //解决第一次未渲染成功会报错的问题
              //   that.onceTableChange++;
              //   console.log(that.onceTableChange);
              //   commit("checkDomHeight", {
              //     domName: "getElementsByClassName",
              //     name: "el-table--fit",
              //     fn: function() {
              //       that.$nextTick(() => {
              //         setTimeout(() => {
              // doit(window.innerHeight, "once");
              //         }, 200);
              //       });
              //     }
              //   });
              // } else {
              doit(window.innerHeight);
              // }
            }
          };
          dispatch("WHchangeHight", d);
          return
          // tablesss
          // class="tablesss"
          $store.state.control.screenSize == 1366 ? 'smallSize' : 'normalSize',
            $store.state.control.windowWidth >= 1024 ? 'tooRowSearch1' : '',

            // searchIndex: 6, //收缩展示数量
            // searchIcon: true, //搜索是否收缩
            // ...this.$store.state.control.searchOpen,
            // height: "auto",
            // 初始化位置, 放入mounted, 以及放入onload的获取数据后
            this.$store.commit("setWindowSizeHeightAdd");
        },
        get: function () {
          // console.log('get!', usheight);
          return usheight;
        }
      });
    },
    MOUNTED_TABLE_SIZE({
      state,
      commit,
      dispatch
    }, data) {
      let d = {
        otherclass: ["downSFZ"],
        haveOpen: true,
        from: "mounted"
      };
      // 开始就适应高度
      commit("setWindowSizeHeightAdd");
      // dispatch("TABLE_CHANGEBUTPOSITION", d);
    }
  },
  mutations: {
    setWindowSize: (state, data) => {
      state.windowWidth = data.innerWidth;
      state.windowHeight = data.innerHeight;
      // console.log(state.windowHeight, "state.windowHeight")
    },
    setWindowSizeHeight: (state, data) => {
      state.tableWindowHeight = data;
      // console.log(state.tableWindowHeight, "state.tableWindowHeight")
    },
    setWindowSizeHeightAdd: (state, data) => {
      setTimeout(() => {
        state.tableWindowHeight += 1;
      }, 0);
    },
    setButOpens(state, data) {
      state.butState = data;
    },
    setOldOpens(state, data) {
      state.oldOpens = data;
      state.oldOpens = data;
    },
    setScreenSize(state, data) {
      state.screenSize = data.w <= 1366 && data.h <= 768 ? 1366 : false;
      state.screenHeight = data.h;
    },
    checkDomHeight(state, data) {
      //  声明定时器
      var timer = null;
      //  检查dom是否执行完成
      function checkDom() {
        let dom = document[data.domName](data.name);
        console.log(dom[0].style.height, "height");
        if (dom[0].style.height) {
          //  执行dom加载完成后的操作
          //  清除定时器
          if (!timer) {
            clearTimeout(timer);
          }
          if (data.fn) {
            //回调函数
            data.fn(dom);
            return;
          } else {
            return dom;
          }
        } else {
          //  自我调用
          timer = setTimeout(checkDom, 200);
        }
      }
      //  首次执行
      checkDom();
    }
  }
};

export default control;

全局监听尺寸–在系统初始化的时候运行

import _this from "../main";

// 设置 init 函数
function init() {
  if (_this) {
    let innerWH = {
      innerWidth: window.innerWidth,
      innerHeight: window.innerHeight
      // innerWidth: document.documentElement.clientWidth,
      // innerHeight: document.documentElement.clientHeight,
    };
    _this.$store.commit("setWindowSize", innerWH);
    _this.$store.commit("setWindowSizeHeight", innerWH.innerHeight);
    _this.$store.commit("setScreenSize", {
      w: window.screen.width,
      h: window.screen.height
    });

    // console.log("第下次判断屏幕分辨率");
    // console.log(' window.innerWidth', window.innerWidth);
    // console.log(' window.innerHeight', window.innerHeight);
    _this.$store.dispatch("changeZoom");
  }
}

// 节流 ms 触发间隔毫秒
var ms = 300;
var lastClick = Date.now() - ms;
// 初始化
setTimeout(() => {
  init();
}, 500);
// 改变窗口大小时重新设置 rem
// window.onresize = function () {
//     // 节流
//     if (Date.now() - lastClick >= ms) {
//         init();
//         lastClick = Date.now();
//     }
// }
window.addEventListener(
  "resize",
  () => {
    // 节流
    if (Date.now() - lastClick >= ms) {
      init();
      lastClick = Date.now();
    }
  },
  false
);

我使用的样式

.tooRowSearch1 {
  position: relative;

  // .avue-form__group {
  //   position: relative;
  //   z-index: 100;
  // }
}
.tooRowSearch1
  .avue-crud__search
  .avue-form__group.avue-form__group--flex
  .avue-form__menu--center {
  width: 222px !important;
  // width: 264px !important;
  // position: absolute !important;
  // right: 0 !important;
  // top: 126px !important;
  // z-index: 100 !important;
  // width: 264px;
  position: absolute;
  right: -8px;
  top: 68px;
  z-index: 100;
  .el-button.el-button--text.el-button--small {
    position: fixed;
    top: 128px;
    right: 46px;
  }
}
.tooRowSearch1 .el-card.is-never-shadow.avue-crud__search {
  overflow: visible;
}

.smallSize {
  //设置1366*768中的表格边距
  //搜索框
  .el-collapse .el-collapse-item {
    padding-top: 0px !important;
    .el-col {
      margin-top: 2px;
      margin-bottom: 8px;
    }
    .el-form-item--small.el-form-item {
      margin-bottom: 10px;
      .el-form-item__label {
        width: auto !important;
      }
    }
  }
  .avue-crud__menu {
    margin-bottom: 0 !important;
  }
  //选择选项横条
  .avue-crud__tip {
    .avue-crud__tip {
      margin: 3px 0;
    }
  }
  .el-card__body {
    //卡片减少padding
    padding: 8px !important;
    .el-card__body {
      padding: 0 8px 8px 8px !important;
    }
  }
  //分页器
  .avue-crud__pagination {
    padding: 10px 0 5px 5px;
  }
}

.normalSize {
  //设置正常的表格边距
  //搜索框
  .el-collapse .el-collapse-item {
    padding-top: 0px !important;
    .el-col {
      margin-bottom: 8px !important;
    }
    .el-form-item--small.el-form-item {
      margin-bottom: 15px;
      .el-form-item__label {
        width: auto !important;
      }
    }
  }
  //选择选项横条
  .avue-crud__tip {
    .avue-crud__tip {
      margin: 6px 0;
    }
  }
  .el-card__body {
    //卡片减少padding
    padding: 8px !important;
  }
  //分页器
  .avue-crud__pagination {
    padding: 15px 0 5px 5px;
  }
}

vue页面中watch备份

windowHeight() {
      return;
      let that = this;
      that.option.height = that.screenHeight;
      let d = {
        fn: function (h) {
          let doit = (height, val) => {
            let table = document.getElementsByClassName("tablesss");
            let heard = table[0].getElementsByClassName("avue-crud__search");
            let usNum =
              that.windowWidth <= 1376 || that.windowHeight <= 778 ? 170 : 302;
            let hs = height || h;
            let overHeight = +hs - (+heard[0].clientHeight + +usNum);
            console.log(overHeight);
            let fromDom = document.getElementsByClassName("el-table--fit")[0];
            fromDom.style.transition = "height 0.1s";
            fromDom.style.height = overHeight + "px";
            let d = {
              that: this,
              otherclass: ["downSFZ"],
              haveOpen: true,
            };
            that.$store.dispatch("TABLE_CHANGEBUTPOSITION", d);
          };
          if (that.onceTableChange == 1) {
            //解决第一次未渲染成功会报错的问题
            that.onceTableChange++;
            console.log(that.onceTableChange);
            that.$store.commit("checkDomHeight", {
              domName: "getElementsByClassName",
              name: "el-table--fit",
              fn: function () {
                that.$nextTick(() => {
                  setTimeout(() => {
                    doit(window.innerHeight, "once");
                  }, 200);
                });
              },
            });
          } else {
            doit();
          }
        },
      };
      this.$store.dispatch("WHchangeHight", d); //为了节流
      return
          $store.state.control.screenSize == 1366 ? 'smallSize' : 'normalSize',
            $store.state.control.windowWidth >= 1024 ? 'tooRowSearch1' : '',

            // searchIndex: 6, //收缩展示数量
            // searchIcon: true, //搜索是否收缩
            // ...this.$store.state.control.searchOpen,
            // height: "auto",
            // 初始化位置, 放入mounted, 以及放入onload的获取数据后
            this.$store.commit("setWindowSizeHeightAdd");
    },

提示,由于avue封装,第一次设置的高度要最高,因为后面改高度只会响应在第一次高度范围内,导致不生效

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

轻动琴弦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值