模式excel表格,在指定列插入一列。遇到跨列跨行怎么解决

图片描述

    //  在指定列后面插入一列
   var tab = document.getElementById("SignFrame");
          //表格行数
    var rows = tab.rows.length ;
          //表格列数
    var cells = tab.rows.item(0).cells.length ;
    var colIndex = Number($(obj).parent().attr("col-data"));      // 当前点击列索引
    var colspan_start = 0;                                         //全局变量记录 跨列起始列索引
    var colspan_end   = 0;                                        //全局变量记录 跨列结束列索引
    var increasCols   = 0;                                        // 全局变量记录递增列

    var rowspan_start = 0;                                        //全局变量记录 跨行起始行索引
    var rowspan_end   = 0;                                        //全局变量记录 跨行结束行索引
    if(cells==26) return;
    $("#SignFrame tr").eq(0).children('th').eq(colIndex).after("<th class='list-col'></th>");
    for(var i=1;i<rows;i++){
       
       if(i>rowspan_start && i<=rowspan_end) {
                if(colspan_end==0) {
                    $("#SignFrame tr").eq(i).children('td').each(function(index){
                        if(index==colIndex-1) {
                            $("#SignFrame tr").eq(i).children('td').eq(index).after("<td>444</td>");
                        }
                    });
                }else{
                     // 写到这里再也不写下去,我发现还有很多种的情况(比如:在插入列的前面,既有跨列又有跨行的)
                }
        }else{
            colspan_end = 0;  //循环新的一行时。刷新跨列结束列索引 
            increasCols = 0;
            $("#SignFrame tr").eq(i).children('td').each(function(index){

                if($(this).attr("rowspan")!==undefined) {
                    var rowspan = Number($(this).attr("rowspan")); // 跨行数
                    if((i+(rowspan-1)) > rowspan_end) {
                        rowspan_end  = i+(rowspan-1);                  // 跨行,结束行索引
                        rowspan_start = i;                             // 跨行,起始行索引 
                    }

                }

                if($(this).attr("colspan")===undefined) {

                    if(colspan_end==0) {
                        if(index == colIndex) {
                           $("#SignFrame tr").eq(i).children('td').eq(index).after("<td>444</td>");
                           return false; 
                        }
                    }else {
                        ++increasCols;   //当前点击列之前有跨列时,实时计算列的索引
                        if(colspan_end==colIndex) {

                            $("#SignFrame tr").eq(i).children('td').eq(index).after("<td>444</td>");
                            return false;
                        }
                    }
                   
                }else {
                    var colspan       = Number($(this).attr("colspan")); // 跨列数
                        colspan_end   = index+(colspan-1);               // 跨列结束列索引
                        colspan_start = index;                            
                        increasCols   = colspan_end;
                    if(colIndex >= index && colIndex <=colspan_end) { // 跨列区间之内
                       $(this).attr("colspan",colspan+1);
                       return false;   
                    }
               }

            })

        }
    }
阅读 2.6k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题