使用element-ui 組件動態合併table的行/列(第二次修改)

    這是第二次修改,在經過調用後臺接口返回來的時候,發現了代碼中的問題;如今將博客中錯誤的地方改過來,添加備註git

              文章需求:動態實現table表格中行/列的自動合併github

    使用框架及UI類庫:Vue+Element-ui算法

    代碼地址:https://github.com/dreamITGirl/VueProgect數組

    最終實現的表格數據框架

    

  實現思路:ui

    在table組件中,提供了一個屬性:span-method,它是一個Function,自己有4個參數,分別是row,rowIndex,colum,columIndex;這四個值能夠直接拿到。先處理連續相同的列的值,作標記,而後,在合併的方法中,根據咱們處理好的數組,去對應的合併行或列this

  實現代碼: spa

//處理table數據的方法,將連續相同的數據彙總 
  merge() {
      let OrderObj = {};
      let provinceObj = {};
      this.tableData.forEach((element, index) => {
        element.rowIndex = index;
        if (OrderObj[element.operators]) {
      //若是下一個數據中的operators和這一列的數據相同,則合併,反之,不合並;(增長這一個判斷的目的是爲了防止隔行出現相同時,列合併);
      //二次修改的地方,在nextItem中,index取不到當前值,由於這時候循環過來的index已是+1以後的值了,因此,nextItem是不須要再+1的 let nextItem
= this.tableData[index + 1].operators ? this.tableData[index + 1].operators : undefined;
      改成
      let nextItem = this.tableData[idndex].operators ? this.tableData[index].operators : undefined
       let prevItem = this.tableData[index - 1].operators ? this.tableData[index - 1].operators : undefined; 
    if (element.operators == nextItem) {
        OrderObj[element.operators].push(index);
        }
else if (element.operators == prevItem) {
          OrderObj[element.operators].push(index); }
      }
else {
        OrderObj[element.operators]
= [];
        OrderObj[element.operators].push(index);
       }
    
if (provinceObj[element.province]) {
let nextPro
= this.tableData[index + 1] ? this.tableData[index + 1].province : undefined;
       let prevPro
= this.tableData[index - 1].province ? this.tableData[index - 1].province : undefined;
if (element.province == nextPro) {
provinceObj[element.province].push(index);
       }
else if (element.province == prevPro) {
        provinceObj[element.province].push(index); }

       }
else {
          provinceObj[element.province]
= [];
          if(index != 0){
            provinceObj[element.province].push(index);
        }
          
       }
  });
// 將數組長度大於1的值 存儲到this.OrderIndexArr(也就是須要合併的項)
    
for (let k in OrderObj) { if (OrderObj[k].length > 1) { this.OrderIndexArr.push(OrderObj[k]); } } for (let i in provinceObj) { if (provinceObj[i].length > 1) { this.provinceArr.push(provinceObj[i]); } }
}
//合併的方法
objectSpanMethod({ row, rowIndex, column, columnIndex }) {
      if (columnIndex === 0) {
        for (let i = 0; i < this.OrderIndexArr.length; i++) {
          let element = this.OrderIndexArr[i];
          for (let j = 0; j < element.length; j++) {
            let item = element[j];
            if (rowIndex === item) {
              if (j === 0) {
                return {
                  rowspan: element.length,
                  colspan: 1
                };
              } else if (j !== 0) {
                return {
                  rowspan: 0,
                  colspan: 0
                };
              }
            }
          }
        }
      }
    //由於需求是前兩列中有相同的就合併,因此須要再次判斷出來第一列中哪些行須要合併;
if (columnIndex === 1) { for (let j = 0; j < this.provinceArr.length; j++) { let element = this.provinceArr[j]; for (let k = 0; k < element.length; k++) { let item = element[k]; if (rowIndex === item) { if (k === 0) { return { rowspan: element.length, colspan: 1 }; } else if (k !== 0) { return { rowspan: 0, colspan: 0 }; } } } } } }

 =============================================code

上面的代碼會致使一個問題,就是當上述數據中,當運營商是移動時,且也有省份和其餘運營商的省份出現相同且連續時,即table的數據以下時,也就是移動中有和其餘的省份出現相同且連續的時候,代碼算法就會出現問題。這時算出的出現廣州的數組位置index數組就是[0,1,2,8,9],這樣的話,table的合併行就會出現錯誤。後面是修改後的代碼blog

 
 
tableData: [
{
operators: "其餘",
province: "廣東",
room: "廣州雙線1",
abbreviation: "GZ_DB2",
bandwidth: "10G",
flow:
"第一條1G (no latest data,no latest data)第二條1G (no latest data,no latest data)"
},
{
operators: "其餘",
province: "廣東",
room: "廣州雙線2",
abbreviation: "GZ_DB2",
bandwidth: "10G",
flow:
"第一條1G (no latest data,no latest data)第二條1G (no latest data,no latest data)"
},
{
operators: "其餘",
province: "廣東",
room: "廣州雙線3",
abbreviation: "GZ_DB2",
bandwidth: "10G",
flow:
"第一條1G (no latest data,no latest data)第二條1G (no latest data,no latest data)"
},
{
operators: "電信",
province: "浙江",
room: "廣州雙線2",
abbreviation: "GZ_DB2",
bandwidth: "10G",
flow:
"第一條10G (no latest data,no latest data) 第二條10G (no latest data,no latest data)  第三條10G (no latest data,no latest data)  第四條10G"
},
{
operators: "電信",
province: "浙江",
room: "杭州雙線2",
abbreviation: "GZ_DB2",
bandwidth: "10G",
flow:
"第一條10G (no latest data,no latest data) 第二條10G (no latest data,no latest data)  第三條10G (no latest data,no latest data)  第四條10G"
},
{
operators: "電信",
province: "浙江",
room: "杭州雙線2",
abbreviation: "GZ_DB2",
bandwidth: "10G",
flow:
"第一條10G (no latest data,no latest data) 第二條10G (no latest data,no latest data)  第三條10G (no latest data,no latest data)  第四條10G"
},
{
operators: "聯通",
province: "江西",
room: "杭州雙線2",
abbreviation: "GZ_DB2",
bandwidth: "10G",
flow:
"第一條1G (no latest data,no latest data)第二條1G (no latest data,no latest data)"
},
{
operators: "聯通",
province: "浙江",
room: "浙江雙線2",
abbreviation: "GZ_DB2",
bandwidth: "10G",
flow:
"第一條10G (no latest data,no latest data) 第二條10G (no latest data,no latest data)  第三條10G (no latest data,no latest data)  第四條10G"
},
{
operators: "移動",
province: "江西",
room: "江西雙線2",
abbreviation: "GZ_DB2",
bandwidth: "10G",
flow:
"第一條1G (no latest data,no latest data)第二條1G (no latest data,no latest data)"
},
{
operators: "移動",
province: "廣東",
room: "廣州雙線2",
abbreviation: "GZ_DB2",
bandwidth: "10G",
flow:
"第一條1G (no latest data,no latest data)第二條1G (no latest data,no latest data)"
},
{
operators: "移動",
province: "廣東",
room: "廣州雙線2",
abbreviation: "GZ_DB2",
bandwidth: "10G",
flow:
"第一條1G (no latest data,no latest data)第二條1G (no latest data,no latest data)"
},
{
operators: "移動",
province: "廣東",
room: "廣州雙線2",
abbreviation: "GZ_DB2",
bandwidth: "10G",
flow:
"第一條1G (no latest data,no latest data)第二條1G (no latest data,no latest data)"
},
]
 merge() {
      let OrderObj = {};
      let provinceObj = {};
      this.tableData.forEach((element, index) => {
        element.rowIndex = index;
        if (OrderObj[element.operators]) {
          let nextItem = this.tableData[index + 1] //爲防止報錯,先判斷this.tableData[index+1]項是否存在,不然js會報錯
            ? this.tableData[index + 1].operators
            : undefined;
          let prevItem = this.tableData[index - 1].operators
            ? this.tableData[index - 1].operators
            : undefined;
          if (element.operators == nextItem) {
            OrderObj[element.operators].push(index);
          } else if (element.operators == prevItem) {
            OrderObj[element.operators].push(index);
          }
        } else {
          OrderObj[element.operators] = [];
          OrderObj[element.operators].push(index);
        }
        
        if (provinceObj[element.province]) {
          let nextPro = this.tableData[index + 1]
            ? this.tableData[index + 1].province
            : undefined;
          let prevPro = this.tableData[index - 1].province
            ? this.tableData[index - 1].province
            : undefined;
          if (element.province == nextPro) {
            provinceObj[element.province].push(index);
          } else if (element.province == prevPro) {
            provinceObj[element.province].push(index);
          }
        } else {
         
          provinceObj[element.province] = [];
          provinceObj[element.province].push(index);
        }
      });
      
      // 將數組長度大於1的值 存儲到this.OrderIndexArr(也就是須要合併的項)
      for (let k in OrderObj) {
        if (OrderObj[k].length > 1) {
          this.OrderIndexArr.push(OrderObj[k]);
        }
      }
     //修改的地方
      for (let i in provinceObj) {
        if (provinceObj[i].length > 1) {
          this.handleData(provinceObj[i])
        }
      }
    },
//新增的方法,處理當數組的長度大於2的時候 handleData(data){ let temp
= data; let itemArr = []; let itemArrNew = []; let resArr = []; if (data.length>2) { for (let i = 0; i < data.length; i++) { if (data[i+1]) { if (data[i+1] - data[i] > 1) { itemArr = data.slice(0,i+1) itemArrNew = temp.slice(i+1,temp.length) break; }else{
            resArr = data
         }
              }
          }
          if (itemArr.length>0 || itemArrNew.length>0) {
            this.provinceArr.push(itemArr)
            this.provinceArr.push(itemArrNew)
          }else{
        this.provinceArr.push(data)
      }
      }else{
        this.provinceArr.push(data)
      }
    },
相關文章
相關標籤/搜索