Happy-Coding-Clans / vue-easytable

A powerful data table based on vuejs. You can use it as data grid、Microsoft Excel or Google sheets. It supports virtual scroll、cell edit etc.
https://happy-coding-clans.github.io/vue-easytable/
MIT License
3.63k stars 725 forks source link

[Feature Request] 单元格选择,选中时,点击表格以外的地方,被选中的那div,可否不要隐藏? #570

Open kaiking01 opened 1 year ago

kaiking01 commented 1 year ago

I am opening an issue for

vue-easytable

Issue Type

Feature

Issue Title

单元格选择,选中时,点击表格以外的地方,蓝色被选中的那div,可否不要隐藏?

What problem does this feature solve?

实际应用中,款选了一部分单元格,但需要点击其他地方后,框选的样式就没了

What does the proposed API look like?

希望能通过配置的形式实现,谢谢

kittors commented 8 months ago
//移出类数组第一个元素
    removeFirstElement(arrayLike) {
      const arr = Array.from(arrayLike); // 将类数组转换为数组
      arr.shift(); // 移除第一个元素
      return arr;
    },
    //获取所选单元格或区域的DOM对象 数组
    getElementArr() {
      const selection = this.lastNewVal;
      const endColIndex = selection.selectionRangeIndexes.endColIndex;
      const endRowIndex = selection.selectionRangeIndexes.endRowIndex;
      const startColIndex = selection.selectionRangeIndexes.startColIndex;
      const startRowIndex = selection.selectionRangeIndexes.startRowIndex;
      const tbodyChildNodes = this.removeFirstElement(
        document.getElementsByClassName("ve-table-body")[0].childNodes
      );
      const arr = [];
      if (this.onlyOneCell) {
        console.log(selection);
      } else {
        for (let i = startRowIndex; i <= endRowIndex; i++) {
          for (let j = startColIndex - 1; j <= endColIndex - 1; j++) {
            arr.push(this.removeFirstElement(tbodyChildNodes[i].childNodes)[j]);
          }
        }
      }
      return arr;
    },
    //给单元格涂上背景颜色
    applyColor(color) {
      this.updateSeletion();
      const selectedCell = this.getElementArr();
      selectedCell.forEach((item) => {
        item.style.backgroundColor = color;
      });
    },
    //当脱离table后也选中对应的区域
    updateSeletion() {
      const selection = this.lastNewVal;
      if (!selection) return;
      const startRowKey = selection.selectionRangeKeys.startRowKey;
      const startColKey = selection.selectionRangeKeys.startColKey;
      const endRowKey = selection.selectionRangeKeys.endRowKey;
      const endColKey = selection.selectionRangeKeys.endColKey;
      if (startRowKey === endRowKey && startColKey === endColKey) {
        //选择了单个单元格
        this.onlyOneCell = true;
        this.setCellSelection(startRowKey, startColKey);
      } else {
        this.onlyOneCell = false;
        //选择了多个单元格
        this.setRangeCellSelection(
          startRowKey,
          startColKey,
          endRowKey,
          endColKey
        );
      }
    },
    //选择区域
    setRangeCellSelection(startRowKey, startColKey, endRowKey, endColKey) {
      this.$refs["tableRef"].setRangeCellSelection({
        startRowKey,
        startColKey,
        endRowKey,
        endColKey,
        // isScrollToStartCell: true, //滚动到选中的区域位置
      });
    },
    //选择单个单元格
    setCellSelection(rowKey, colKey) {
      this.$refs["tableRef"].setCellSelection({
        rowKey,
        colKey,
      });
    },
    //添加监听
    addEvent() {
      const tableRef = this.$refs["tableRef"];
      this.$watch(
        () => tableRef.getRangeCellSelection(),
        (newVal, oldVal) => {
          // 当currentValue发生变化时执行的操作
          if (newVal) {
            this.lastNewVal = newVal;
          }
        }
      );
    },

可以用我写的这个方案实现