国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

vue中實(shí)現(xiàn)el-table點(diǎn)選和鼠標(biāo)框選功能

這篇具有很好參考價(jià)值的文章主要介紹了vue中實(shí)現(xiàn)el-table點(diǎn)選和鼠標(biāo)框選功能。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

vue 表格框選,JS,vue,web前端,vue.js,elementui,javascript

?

實(shí)現(xiàn)思路: 項(xiàng)目有兩個(gè)需求,既能在el-table實(shí)現(xiàn)點(diǎn)選又能實(shí)現(xiàn)鼠標(biāo)框選

一. 點(diǎn)選實(shí)現(xiàn)思路: 使用el-table的cellClick方法,

????????1.直接給點(diǎn)擊的cell添加類名,cell.classList.add("blue-cell");然后把獲取的數(shù)據(jù)存入數(shù)組,

???????????設(shè)置樣式:

::v-deep .el-table td.blue-cell {
  border: 1px solid blue !important;
}

? ? ?方法2.如果不添加類名,可以在cellStyle方法里通過存儲(chǔ)的數(shù)組添加邊框,如果是普通滾動(dòng)可以使用行索引,如果是虛擬滾動(dòng),這里需要使用id更為準(zhǔn)確

cellStyle({ row, column, rowIndex, columnIndex }) {
      // 對(duì)xqArr選擇選區(qū)的單元格加藍(lán)邊框
      let matchObj = this.xqArr.find(
        item =>
          item.column === column.index &&
          // item.row === row.index &&
          item.rowId === row.id &&
          item.sampleTime === row.sampleTime
      );
      if (matchObj) {
        return { border: "1px solid #5E99FD" };
      }
}

二.鼠標(biāo)框選實(shí)現(xiàn)思路: 利用鼠標(biāo)按下和抬起事件,計(jì)算框的范圍,框住的cell可以通過類名添加邊框或者依然通過數(shù)組形式. 需要注意的是因?yàn)閑l-table在頁面的右下方,并且數(shù)據(jù)量大,可以滾動(dòng),所以需要在計(jì)算距離的時(shí)候需要減去容器偏移的距離和滾動(dòng)的高度.文章來源地址http://www.zghlxwxcb.cn/news/detail-592729.html

              <el-table
                border
                :lazy="true"
                v-loading="loading"
                @cell-click="cellClick"
                :cell-class-name="tableCellClassName"
                @row-contextmenu="rightClick"
                @row-click="clickTableRow"
                @mousedown.native="down($event)"
                @mousemove.native="move($event)"
                @mouseup.native="up($event)"
                :data="historyDataTables"
                :highlight-current-row="true"
                :stripe="true"
                :header-cell-style="{
                  background: '#cff7ff',
                  fontWeight: 'bold',
                  color: '#080809'
                }"
                :row-height="30"
                :total="totalCount"
                :cell-style="cellStyle"
                :max-height="maxHeight1"
                @selection-change="handleSelectionChange"
                ref="multipleTable"
                :row-key="row => row.id"
                id="table"
              >
<script>
  export default {
    data() {
      return {
      // 列表集合
      historyDataTables: [],
      select: false,
      isMouseDown: true, // 是否需要(允許)處理鼠標(biāo)的移動(dòng)事件

      // 定義移動(dòng)元素div
      rect: null,
      // 記錄鼠標(biāo)按下時(shí)的坐標(biāo)
      downX: 0,
      downY: 0,
      // 記錄鼠標(biāo)抬起時(shí)候的坐標(biāo)
      mouseX2: this.downX,
      mouseY2: this.downY,
      // 表格dom元素
      TableDom: null,
      tableHeaderHeight: 0,
      selectedData: [], // 鼠標(biāo)框選選中的數(shù)據(jù)

      selectedCellTop: 0, // 選中單元格距離el-table頂部的距離
      selectedCellLeft: 0, // 選中單元格距離el-table左側(cè)的距離

      tableRectTop: 0, // el-table距離window頂部的距離
      tableRectLeft: 0, // el-table距離window左側(cè)的距離
      tableScrollTop: 0, // el-table滾動(dòng)的距離
      }
  },
  mounted() {
    this.TableDom = this.$refs.multipleTable.$el; // 獲取table元素

    // 獲取table的位置,監(jiān)聽窗口變化,table的距離變化
    this.getTableMarginLeft();
    window.addEventListener("resize", this.getTableMarginLeft);

    this.clientWidth =
      document.documentElement.clientWidth || document.body.clientWidth;
    this.clientHeight =
      document.documentElement.clientHeight || document.body.cientHeight;

    this.otherHeight =
      Math.ceil($(".is-always-shadow").outerHeight()) +
      Math.ceil($(".is-top").outerHeight());

    this.maxHeight1 = this.clientHeight - this.otherHeight - 150 + "px";

    var that = this;
    window.onresize = () => {
      return (() => {
        window.clientHeight =
          document.documentElement.clientHeight || document.body.clientHeight;
        that.clientHeight = window.clientHeight;
      })();
    };
  },

  beforeDestroy() {
    window.removeEventListener("resize", this.getTableMarginLeft);
  },
  methods: {
    // 獲取table距離頁面左側(cè)和上方的距離
    getTableMarginLeft() {
      const tableRect = this.TableDom.getBoundingClientRect(); // 獲取el-table元素的位置信息
      this.tableRectTop = Math.ceil(tableRect.top);
      this.tableRectLeft = Math.ceil(tableRect.left);
    },

    down(event) {
      // 當(dāng)允許鼠標(biāo)按下時(shí),才允許處理鼠標(biāo)的移動(dòng)事件,這里結(jié)合項(xiàng)目其他問題所以設(shè)置了判斷條件
      if (this.isMouseDown) {
        this.select = true;
        this.rect = document.createElement("div");
        // 框選div 樣式
        this.rect.style.cssText =
          "position:absolute;width:0px;height:0px;font-size:0px;margin:0px;padding:0px;border:1px solid #0099FF;background-color:#C3D5ED;z-index:1000;filter:alpha(opacity:60);opacity:0.6;display:none;";
        this.rect.id = "selectDiv";
        this.getTableMarginLeft();

        const container = document.querySelector(".el-table__body-wrapper"); // 獲取table容器元素
        this.TableDom.appendChild(this.rect); // 添加到table元素下

        // 取得鼠標(biāo)按下時(shí)的坐標(biāo)位置
        this.downX =
          event.x || event.clientX + container.scrollLeft - this.tableRectLeft; // 鼠標(biāo)按下時(shí)的x軸坐標(biāo) event.x 兼容火狐瀏覽器, event.clientX 兼容谷歌瀏覽器
        this.downY =
          event.y || event.clientY + container.scrollTop - this.tableRectTop; // 鼠標(biāo)按下時(shí)的y軸坐標(biāo)

        this.rect.style.left = this.downX + "px"; // 設(shè)置你要畫的矩形框的起點(diǎn)位置
        this.rect.style.top = this.downY + "px"; // 設(shè)置你要畫的矩形框的起點(diǎn)位置
        //設(shè)置你要畫的矩形框的起點(diǎn)位置
        this.rect.style.left = this.downX; // 因?yàn)樵诨鸷鼮g覽器下,上面的代碼不起作用,所以在這里再寫一遍,為什么火狐瀏覽器不起作用,因?yàn)榛鸷鼮g覽器下,我們的div是絕對(duì)定位的,所以我們要加上px,為什么這里沒加px,因?yàn)槲覀兿旅嬉由蟨x,所以這里不用加
        this.rect.style.top = this.downY;
      } else {
        return;
      }
    },
    move(event) {
      /*
      這個(gè)部分,根據(jù)你鼠標(biāo)按下的位置,和你拉框時(shí)鼠標(biāo)松開的位置關(guān)系,可以把區(qū)域分為四個(gè)部分,根據(jù)四個(gè)部分的不同,
      我們可以分別來畫框,否則的話,就只能向一個(gè)方向畫框,也就是點(diǎn)的右下方畫框.
      */
      if (this.select && this.isMouseDown) {
        // 取得鼠標(biāo)移動(dòng)時(shí)的坐標(biāo)位置
        this.mouseX2 = event.clientX; // 鼠標(biāo)移動(dòng)時(shí)的x軸坐標(biāo)
        this.mouseY2 = event.clientY; // 鼠標(biāo)移動(dòng)時(shí)的y軸坐標(biāo)
        // 框選元素的顯示與隱藏
        if (this.rect.style.display == "none") {
          this.rect.style.display = "";
        }
        // 框選元素的位置處理
        this.rect.style.left =
          Math.min(this.mouseX2, this.downX) - this.tableRectLeft + "px";

        this.rect.style.top =
          Math.min(this.mouseY2, this.downY) - this.tableRectTop + "px"; // 取得鼠標(biāo)拉框時(shí)的起點(diǎn)坐標(biāo)

        this.rect.style.width = Math.abs(this.mouseX2 - this.downX) + "px"; // 取得鼠標(biāo)拉框時(shí)的寬度

        this.rect.style.height = Math.abs(this.mouseY2 - this.downY) + "px"; // 取得鼠標(biāo)拉框時(shí)的高度
        // A part
        if (this.mouseX2 < this.downX && this.mouseY2 < this.downY) {
          this.rect.style.left = this.mouseX2 + this.tableRectLeft;
          this.rect.style.top = this.mouseY2 + this.tableRectTop;
        }

        // B part
        if (this.mouseX2 > this.downX && this.mouseY2 < this.downY) {
          this.rect.style.left = this.downX + this.tableRectLeft;
          this.rect.style.top = this.mouseY2 + this.tableRectTop;
        }

        // C part
        if (this.mouseX2 < this.downX && this.mouseY2 > this.downY) {
          this.rect.style.left = this.mouseX2 + this.tableRectLeft;
          this.rect.style.top = this.downY + this.tableRectTop;
        }

        // D part
        if (this.mouseX2 > this.downX && this.mouseY2 > this.downY) {
          this.rect.style.left = this.downX + this.tableRectLeft;
          this.rect.style.top = this.downY + this.tableRectTop;
        }
      } else {
        return;
      }

      this.stopEvent(event);
    },
    // 阻止默認(rèn)事件
    stopEvent(event) {
      if (event.stopPropagation) {
        // 標(biāo)準(zhǔn)瀏覽器
        event.stopPropagation(); // 阻止事件冒泡
        event.preventDefault(); // 阻止默認(rèn)事件
      } else {
        // IE瀏覽器
        event.cancelBubble = true;
        event.returnValue = false;
      }
    },
    // 鼠標(biāo)抬起事件
    up() {
      if (this.select && this.isMouseDown) {
        const container = document.querySelector(".el-table__body-wrapper"); // 獲取table容器元素
        const scrollTop = container.scrollTop; // 獲取el-table的scrollTop和scrollLeft
        const scrollLeft = container.scrollLeft;

        const headerWrapper = this.TableDom.querySelector(
          ".el-table__header-wrapper"
        );
        const tableHeaderHeight = Math.ceil(
          headerWrapper.getBoundingClientRect().height
        );

        const columns = this.$refs.multipleTable.columns; // 表格的標(biāo)題

        const rectLeft = this.rect.offsetLeft + scrollLeft - this.tableRectLeft;
        const rectTop =
          this.rect.offsetTop +
          scrollTop -
          this.tableRectTop -
          tableHeaderHeight;

        const tableBody = document.querySelector(".el-table__body");

        tableBody.children[1].childNodes.forEach(element => {
          for (let index = 0; index < element.childNodes.length; index++) {
            // 獲取當(dāng)前單元格
            const cell = element.childNodes[index];

            if (
              // 判斷選中的單元格是否在鼠標(biāo)拉框的范圍內(nèi)
              rectLeft <
                cell.offsetLeft - this.tableRectLeft + cell.offsetWidth &&
              rectLeft + this.rect.offsetWidth >
                cell.offsetLeft - this.tableRectLeft &&
              rectTop <
                cell.offsetTop - this.tableRectTop + cell.offsetHeight &&
              rectTop + this.rect.offsetHeight >
                cell.offsetTop - this.tableRectTop &&
              index >= 3 // 選中的單元格所在列的索引大于等于3
            ) {
              if (cell.className.indexOf("add") == -1) {
                // cell.style.border = "1px solid red";
                const cellText = cell.innerText;
                const rowData = this.historyDataTables[element.rowIndex]; // 獲取當(dāng)前單元格所在的行數(shù)據(jù)
                // 獲取表格的列名的屬性名property
                let columnProperty = undefined;
                // 遍歷第一行數(shù)據(jù)
                 // console.log(index, '--index--'); // 框選數(shù)據(jù)所在列的索引

                for (const item of columns) {
                  if (item.index === index) {
                    columnProperty = item.property;
                    break;
                  }
                }
                // const rowIndex = element.rowIndex; // 將當(dāng)前單元格所在的行數(shù)據(jù)加入到該列數(shù)據(jù)中
                const columnIndex = index;
                const time = rowData.sampleTime;

                // 選擇要添加到選中行數(shù)組中的屬性
                const selected = {
                  rowId: rowData.id,
                  // row: rowIndex,
                  column: columnIndex,
                  sampleTime: time,
                  factor: columnProperty,
                  tag: rowData[columnProperty + "_tag"] || "",
                  tagStatus: rowData[columnProperty + "_tag_status"] || "",
                  mark: rowData[columnProperty + "_mark"] || ""
                };

                // 將選中數(shù)據(jù)加入到狀態(tài)中已有的數(shù)據(jù)中,如果已有相同的數(shù)據(jù),則不加入
                if (
                  !this.selectedData.some(data => this.isEqual(data, selected))
                ) {
                  this.selectedData.push(selected);
                }
                // 將選中數(shù)據(jù)加入到 xqArr 中
                this.selectedData.forEach(item => {
                  // 如果 xqArr 中已有相同數(shù)據(jù),則不加入
                  if (!this.xqArr.some(data => this.isEqual(data, item))) {
                    this.xqArr.push(item);
                  }
                });
                this.selectedData = [];
              }
            }
          }
        });

        //鼠標(biāo)抬起,就不允許在處理鼠標(biāo)移動(dòng)事件
        this.select = false;
        //隱藏圖層
        if (this.rect) {
          this.TableDom.removeChild(this.rect);
        }
      } else {
        return;
      }
    },
    // 定義方法 isEqual 來比較兩個(gè)選中數(shù)據(jù)對(duì)象是否相同
    isEqual(data1, data2) {
      return (
        data1.rowId === data2.rowId &&
        data1.column === data2.column &&
        data1.sampleTime === data2.sampleTime &&
        data1.factor === data2.factor
      );
    }
  }
}

到了這里,關(guān)于vue中實(shí)現(xiàn)el-table點(diǎn)選和鼠標(biāo)框選功能的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • el-table 設(shè)置行背景顏色 鼠標(biāo)移入高亮問題處理

    el-table 設(shè)置行背景顏色 鼠標(biāo)移入高亮問題處理

    后端返回表格數(shù)據(jù),有特定行數(shù)需要用顏色標(biāo)識(shí)。類似于以下需求: 方式 區(qū)別 :row-class-name=“tableRowClassName” 已返回類名的形式設(shè)置樣式,代碼整潔,但是會(huì)鼠標(biāo)高亮,導(dǎo)致背景顏色失效 :cell-style=“cellStyle” 以返回樣式的形式設(shè)置,無鼠標(biāo)高亮問題 2.1 表格代碼 2.2 :row-cla

    2024年02月15日
    瀏覽(20)
  • elementUI el-table實(shí)現(xiàn)鼠標(biāo)懸浮某一行,在鼠標(biāo)右側(cè)展示提示信息

    elementUI el-table實(shí)現(xiàn)鼠標(biāo)懸浮某一行,在鼠標(biāo)右側(cè)展示提示信息

    背景 el-table組件中,可以通過勾選某條數(shù)據(jù)來創(chuàng)建單據(jù),但是有些數(shù)據(jù)沒有權(quán)限使用,就需要禁用掉勾選的功能,然后當(dāng)鼠標(biāo)懸浮在這一行的時(shí)候,展示類似于toolTip的提示框。 除了當(dāng)鼠標(biāo)懸浮在某一行,展示類似于toolTip的提示框這一條el-table是沒有提供配置項(xiàng)的,其他的都

    2024年02月08日
    瀏覽(27)
  • Element plus el-table 鼠標(biāo)滾動(dòng)失靈的問題及解決辦法

    Element plus el-table 鼠標(biāo)滾動(dòng)失靈的問題及解決辦法

    Bug:ElementUI el-table 鼠標(biāo)滾輪下滑動(dòng)失靈的情況 我測(cè)出來的這個(gè)問題條件很苛刻,需要達(dá)到以下幾個(gè)條件才會(huì)觸發(fā): 1.element plus(其他版本沒試) 2.el-table-column組件有fixed屬性時(shí) 3.template標(biāo)簽中有el-button,并且el-button有size=“small”時(shí) 4.我的瀏覽器縮放(Ctrl+滾輪)達(dá)到110%時(shí) 會(huì)

    2024年02月13日
    瀏覽(41)
  • Element-UI 解決el-table合并行+固定列鼠標(biāo)浮動(dòng)高亮問題

    Element-UI 解決el-table合并行+固定列鼠標(biāo)浮動(dòng)高亮問題

    今天在搬磚的時(shí)候發(fā)現(xiàn)了一個(gè)問題,當(dāng)用el-table組件畫表格,并且存在合并行時(shí),鼠標(biāo)浮動(dòng)的高亮樣式有些問題,如下圖。 可以看到雖然已經(jīng)合并成為了一行但是,高亮部分的顯示樣式仍然是分家狀態(tài)。由于我畫的表格需要有固定列,雖然百度了一些大神的方法,但是仍然沒

    2024年02月11日
    瀏覽(20)
  • [element-ui] el-table表格頭添加圖標(biāo)-鼠標(biāo)移入顯示el-tooltip提示信息

    [element-ui] el-table表格頭添加圖標(biāo)-鼠標(biāo)移入顯示el-tooltip提示信息

    只是單純的想在table中添加圖標(biāo)和tooltip 在el-table-column中綁定:render-header=“renderPrice” (此方法無法使tooltip換行) 方法二、 使用組件插槽,elementui已封裝好 elementUI表格頭添加圖標(biāo)-鼠標(biāo)移入顯示el-tooltip提示信息

    2024年02月11日
    瀏覽(28)
  • vue2/3 - 基于element(ui/plus)實(shí)現(xiàn)el-table表格每行可拖動(dòng)換位置排序,表格列(表頭)可拖動(dòng)交換位置功能效果(table表格可拖曳排序的行和列,用鼠標(biāo)動(dòng)態(tài)拖拽排序表格行列)

    vue2/3 - 基于element(ui/plus)實(shí)現(xiàn)el-table表格每行可拖動(dòng)換位置排序,表格列(表頭)可拖動(dòng)交換位置功能效果(table表格可拖曳排序的行和列,用鼠標(biāo)動(dòng)態(tài)拖拽排序表格行列)

    在vue2、vue3項(xiàng)目開發(fā)中,element餓了么組件庫實(shí)現(xiàn)表格el-table組件支持【行和列可拖曳排序、換位置】功能,每行數(shù)據(jù)可拖拽進(jìn)行排序調(diào)換位置,每列數(shù)據(jù)可以自由拖動(dòng)進(jìn)行調(diào)換位置。 提供詳細(xì)示例代碼,復(fù)制源碼換個(gè)數(shù)據(jù)就能用了。

    2024年04月13日
    瀏覽(29)
  • 【詳解|徹底搞懂el-table和列表過濾】vue列表過濾和el-table的實(shí)現(xiàn)

    【詳解|徹底搞懂el-table和列表過濾】vue列表過濾和el-table的實(shí)現(xiàn)

    vue列表過濾 el-table的理解 先來看一段代碼: chatGPT 的理解真的很6: 這段代碼使用了 Element UI 的組件,創(chuàng)建了一個(gè)表格列組件 el-table-column,并為它設(shè)置了一些屬性和插槽。 各種屬性: 名稱 作用 prop 指定了該列綁定的數(shù)據(jù)對(duì)象的屬性名為 warehouseName label 指定了該列的列名為 “

    2024年02月11日
    瀏覽(20)
  • Vue3 el-table 單選

    解決方式:給每個(gè)表格加上唯一key值

    2024年02月11日
    瀏覽(24)
  • vue封裝el-table表格組件

    vue封裝el-table表格組件

    先上效果圖: 本文包含了具名插槽、作用域插槽、jsx語法三種: Render.vue( 很重要,必須有 ): Table.vue 使用Table組件

    2024年02月20日
    瀏覽(29)
  • vue el-table實(shí)現(xiàn)動(dòng)態(tài)表頭

    vue el-table實(shí)現(xiàn)動(dòng)態(tài)表頭

    眾所周知,element-ui中有一個(gè)表格組件el-table,用于展示多條結(jié)構(gòu)類似的數(shù)據(jù)。之前遇到過一個(gè)需求,要手動(dòng)控制el-table的表頭顯示。就是假如table表格一共有10列數(shù)據(jù),可以通過設(shè)置勾選,決定顯示多少列。 為了代碼的復(fù)用性,將配置頁面單獨(dú)抽成了組件,所以代碼中會(huì)有組件

    2024年02月12日
    瀏覽(25)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包