前端常用方法

// 根据id去重,可改
private deWeight(arr: any) {
    for (let i = 0; i < arr.length - 1; i++) {
      for (let j = i + 1; j < arr.length; j++) {
        if (arr[i].id === arr[j].id) {
          arr.splice(j, 1);
          j--;
        }
      }
    }
     return arr;
  }
// 深拷贝
  private deepClone(obj: any) {
      const result: any = Array.isArray(obj) ? [] : {};
      for (const key in obj) {
        if (obj.hasOwnProperty(key)) {
          if (typeof obj[key] === 'object' && obj[key] !== null) {
            result[key] = this.deepClone(obj[key]);   // 递归复制
          } else {
            result[key] = obj[key];
          }
        }
      }
      return result;
  }
 
    // 一维数组转换为二维数组
    arrTrans(num, arr) {
      const iconsArr = [];
      arr.forEach((item, index) => {
        const page = Math.floor(index / num);
        if (!iconsArr[page]) {
          iconsArr[page] = [];
        }
        iconsArr[page].push(item);
      });
      return iconsArr;
    }
/**
 * 时间获取
 * 使用例子:
 *    获取本月 :getAppointedDate(2, 0)
 *    获取本周 getAppointedDate(1, 0)
 *    获取上周 getAppointedDate(1, 1)
 *    获取上月 :getAppointedDate(2, 1)
 *    获取下月 :getAppointedDate(2, -1)
 *    获取下周 getAppointedDate(1, -1)
 */
  // 获取时间
  private getAppointedDate(optType: any, optPageNum: any) {
    const now = new Date(); // 当前日期
    const nowDayOfWeek = now.getDay(); // 今天本周的第几天
    const nowDay = now.getDate();  // 当前日
    const nowMonth = now.getMonth(); // 当前月
    const nowYear = now.getFullYear(); // 当前年
    const lastMonth = nowMonth - 1;
    let startDate = '';
    let endDate = '';
    if (optType === 1) {
      startDate = this.formatDateFn(new Date(nowYear, nowMonth, nowDay - nowDayOfWeek + 1 - (7 * optPageNum)));
      endDate = this.formatDateFn(new Date(nowYear, nowMonth, nowDay + 7 - nowDayOfWeek - ( 7 * optPageNum)));
    } else if (optType === 2) {
      startDate = this.formatDateFn(new Date(nowYear, lastMonth + 1 - (1 * optPageNum), 1));
      endDate = this.formatDateFn(new Date(nowYear, lastMonth + 1 - (1 * optPageNum) , this.getMonthDays(nowYear, lastMonth + 1 - (1 * optPageNum))));
    }
    return {startDate, endDate};
  }
 
  private getMonthDays(nowYear: any, myMonth: any) {
    const monthStartDate: any = new Date(nowYear, myMonth, 1);
    const monthEndDate: any = new Date(nowYear, myMonth + 1, 1);
    const days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
    return days;
  }
 
  private formatDateFn(date: any) {
    const myyear = date.getFullYear();
    let mymonth = date.getMonth() + 1;
    let myweekday = date.getDate();
    if (mymonth < 10) {
      mymonth = '0' + mymonth;
    }
    if (myweekday < 10) {
    myweekday = '0' + myweekday;
    }
    return (myyear + '-' + mymonth + '-' + myweekday);
  }
 
  // 获取当前的日期
  private getDayTime() {
    const now = new Date();
    const year = now.getFullYear(); // 年
    let month: any = now.getMonth() + 1; // 月
    let day: any = now.getDate();  // 日
    month = month >= 10 ? month : '0' + month;
    day = day >= 10 ? day : '0' + day;
    return year + '-' + month + '-' + day;
  }

  

// 局部组件批量引入
const requireComponent  = require.context(
  './', // 组件所在目录的相对路径
  true, // 是否查询其子目录
  /\w+\.vue$/ // 匹配基础组件文件名的正则表达式
)
let comObj: any = {};
requireComponent.keys().forEach((fileName: any) => {
  const names = fileName
    .split("/")
    .pop()
    .replace(/\.\w+$/, "");
  const componentConfig = requireComponent(fileName);  // 获取组件配置
  // 若该组件是通过"export default"导出的,优先使用".default",否则退回到使用模块的根
  comObj[names] = componentConfig.default || componentConfig;
})


// 全局组件批量引入(main.js引入)
const requireComponent = require.context(
    './', //组件所在目录的相对路径
    false, //是否查询其子目录
    /Base[A-Z]\w+\.(vue|js)$/ //匹配基础组件文件名的正则表达式
)
requireComponent.keys().forEach(fileName=>{
    // 获取文件名
    var names = fileName.split("/").pop().replace(/\.\w+$/,"");//BaseBtn
    // 获取组件配置
    const componentConfig = requireComponent(fileName);
    // 若该组件是通过"export default"导出的,优先使用".default",
    // 否则退回到使用模块的根
    Vue.component(names,componentConfig.default || componentConfig);
})

 

// el-table 动态合并表格
data 里面定义
spanArr: [],
pos: 0,
contentSpanArr: [],
position: 0,
customerArr: [],
customer: 0   
// 需要合并的行和列 
test(){
   this.spanArr = [];
   this.contentSpanArr = [];
   this.customerArr = [];
   this.tableData2.forEach((item, index) => {
               if (index === 0) {
                    this.spanArr.push(1);
                    this.pos = 0;
                    this.contentSpanArr.push(1);
                    this.position = 0;
                    this.customerArr.push(1);
                    this.customer = 0;
                } else {
                    // 判断当前元素与上一个元素是否相同(第1列)
                    if (this.tableData2[index].customerName === this.tableData2[index - 1].customerName)     {
                        this.spanArr[this.pos] += 1;
                        this.spanArr.push(0);
                    } else {
                        this.spanArr.push(1);
                        this.pos = index;
                    }
                    // 判断当前元素与上一个元素是否相同(第2列)
                    if (this.tableData2[index].year === this.tableData2[index - 1].year && this.tableData2[index].customerName === this.tableData2[index - 1].customerName) {
                        this.contentSpanArr[this.position] += 1;
                        this.contentSpanArr.push(0);
                    } else {
                        this.contentSpanArr.push(1);
                        this.position = index;
                    }
                    // 判断当前元素与上一个元素是否相同(第3列)
                    if (this.tableData2[index].customerCode === this.tableData2[index - 1].customerCode && this.tableData2[index].customerName === this.tableData2[index - 1].customerName) {
                        this.customerArr[this.customer] += 1;
                        this.customerArr.push(0);
                    } else {
                        this.customerArr.push(1);
                        this.customer = index;
                    }
                }
            })
        },
// 合并单元格
        objectSpanMethod2({ row, column, rowIndex, columnIndex }) {
            if (columnIndex === 0) {
                const _row = this.spanArr[rowIndex];
                const _col = _row > 0 ? 1 : 0;
                return {
                    rowspan: _row,
                    colspan: _col
                };
            } else if (columnIndex === 1) {
                const _row = this.contentSpanArr[rowIndex];
                const _col = _row > 0 ? 1 : 0;
                return {
                    rowspan: _row,
                    colspan: _col
                };
            } else if (columnIndex === 2) {
                const _row = this.customerArr[rowIndex];
                const _col = _row > 0 ? 1 : 0;
                return {
                    rowspan: _row,
                    colspan: _col
                };
            }
        },

 

posted @ 2021-03-11 17:19  7七7  阅读(110)  评论(0)    收藏  举报