mixin.js 3.05 KB
let mixin = {
  data() {
    return {
      // 表格选择数据
      selectionChangeList: [],
      needMergeArr: [],
      rowMergeArrs: {},
    }
  },
  mounted() {
    this.rowMergeArrs = this.rowMergeHandle(this.needMergeArr, this.PageData.records); // 处理数据
  },
  methods: {
    tableRowClassName({ row, rowIndex }) {
      if ((rowIndex + 1) % 2 === 0) {
        return 'success-row';
      }
      return '';
    },
    // 表格多选操作
    handleSelectionChange(value) {
      this.selectionChangeList.length = 0
      value.map((item, index) => {
        this.selectionChangeList[index] = item.id
      })
    },
    // 导出数据
    getExportList(url, name = '列表数据') {
      this.$confirm('此操作将进行文件导出, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let loading = this.$loading({
          lock: true,
          text: '生在生成请稍后',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        })
        require.ensure([], () => {
          let { export_json_to_excel } = require('../../vendor/Export2Excel');
          let tHeader = ['id', '名称', '开始时间', '结束时间']
          let filterVal = ['id', 'title', 'startTime', 'endTime'];
          let data = this.formatJson(filterVal, this.PageData.records)
          export_json_to_excel(tHeader, data, name)
          loading.close();
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
      })
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]))
    },
    // 表格索引得问题
    indexMethod(index) {
      return index + 1 + (this.PageData.current - 1) * this.PageData.size;
    },
    handleCurrentChange(val) {
      this.initData.currentPage = val
      this._initData()
    },
    handleSizeChange(val) {
      console.log(val)
      this.initData.pageSize = val
      this._initData()
    },
    // 合并表格
    mergeAction(val, rowIndex, colData) {
      let _row = this.rowMergeArrs[val].rowArr[rowIndex];
      let _col = _row > 0 ? 1 : 0;
      return [_row, _col];
    },
    rowMergeHandle(arr, data) {
      if (!Array.isArray(arr) && !arr.length) return false;
      if (!Array.isArray(data) && !data.length) return false;
      let needMerge = {};
      arr.forEach((i) => {
        needMerge[i] = {
          rowArr: [],
          rowMergeNum: 0,
        };
        data.forEach((item, index) => {
          if (index === 0) {
            needMerge[i].rowArr.push(1);
            needMerge[i].rowMergeNum = 0;
          } else {
            if (item[i] === data[index - 1][i]) {
              needMerge[i].rowArr[needMerge[i].rowMergeNum] += 1;
              needMerge[i].rowArr.push(0);
            } else {
              needMerge[i].rowArr.push(1);
              needMerge[i].rowMergeNum = index;
            }
          }
        });
      });
      return needMerge;
    },
  }
}
export default mixin