SoFunction
Updated on 2025-03-06

Use js to realize the height and width adjustment of table cells, compatible with merged cells (compatible with IE6, 7, 8, FF) instances


//Note: Get the object. Example: $("objectId") is equivalent to ("objectId")
 if (typeof $ != "function") { var $ = function (ids) { return (ids) }; }
//Note: Get the coordinates, parentNode last node. Example: absPos(object).x
 function absPos(_node, parentNode) { var x = y = 0; var node = _node; do { if (parentNode && node == parentNode) { break; } x += ; y += ; } while (node = ); node = _node; return { 'x': x, 'y': y }; }
function addEvent(object, event, func) { if () { /* W3C method (DOM method) The false in the following statement means to be used for the bubble stage, and if true, it is used for the capture stage (IE does not support capture), so the reason for using false here is to unify */(event, func, false); return true; } else if () { /* MSIE method (IE method) */object['e' + event + func] = func; object[event + func] = function () { object['e' + event + func](); }; ('on' + event, object[event + func]); return true; } /*If neither method is available, return false */return false; }
//Note: determine whether it is triggered by child nodes in the object and onmouseout. Example: e = e || event;if (isMouseLeaveOrEnter(e, obj)) {}
 function isMouseLeaveOrEnter(e, handler) { if ( != 'mouseout' && != 'mouseover') return false; var reltg = ? : == 'mouseout' ? : ; while (reltg && reltg != handler) reltg = ; return (reltg != handler); }

 var table = $("mainTable");
var tabEditDiv; //The DIV overlay on the table
var cellHide = [];//Supplemented rowspan, cellspan grid
var moveMode = "";//Mouse Move Mode
var moveArgs = []; //Move mode parameters

  = function(){return false;};
 addEvent(window,"resize",function(){loadTable();});

 $("tabletitle").ondblclick = function(){
     if(("input").length > 0){return;}
= "<input type='text' value='" + ( == "Table title" ? "" : ) + "' />";
     var input = ("input")[0];
     var _this = this;
     ();
     = function(){_this.innerHTML = ;}
     = function (e) { var key = ? : ; if (key == 13) (); };
 }

 function loadTable(){
     var tabPos = absPos(table);
     if(!tabEditDiv){
         tabEditDiv = ("div");
         (tabEditDiv);
     }
     = "left:" + ( - 15) + "px;top:" + ( - 15) + "px;";
     = "tabEditDiv";

//Select all Table button
     if(!seltab){
         var seltab = ("div");
         = "width:15px;height:15px;left:" + ( - 15) + "px;top:" + ( - 15) + "px;";
         = "seltab";
         = function(){
             if(("selected") == "1"){
                 ("selected");
                 = "";
                 = "15px";
                 = "15px";
             }else{
                 ("selected","1");
                 = "#B6CAEB";
                 = ( + 15) + "px";
                 = ( + 15) + "px";
             }
         }
         (seltab);
     }
     loadTableEdit();
 } loadTable();

function loadTableEdit(){ //Load div with adjustable width and height
     var tabPos = absPos(table);
     = "";
     var cellcount = 0;
     var isadd = == 0;
     for(var i=0;i<;i++){
         for(var j=0;j<[i].;j++){
             var pos = absPos([i].cells[j],table);
if(!$("splitx_" + ( + [i].cells[j].clientHeight))){ //Load div with adjustable height
                 var split = ("div");
                 = "splitx_" + ( + [i].cells[j].clientHeight);
                 = "splitx";
                 = "width:" + + "px;left:15px;top:" + ( + [i].cells[j].clientHeight - 1 + 15) + "px";
                 = function(){
                     var index = ("index");
                     if(index == null){ index = 0; var divs = ("div"); var left = parseInt(("_")[1]); for(var k=0;k<;k++){ if(divs[k].("splitx_") < 0) continue; if(parseInt(divs[k].("_")[1]) < left) index++; } ("index",index);}else{index = parseInt(index);}
                     moveMode = "cellHeight";
                     moveArgs[] = index;
                 }
                 (split);
             }
if(j > 0){ //Load div with adjustable width
                 if(!$("splity_" + )){
                     var split = ("div");
                     = "splity_" + ;
                     = "splity";
                     = "height:" + + "px;top:15px;left:" + ( - 2 + 15) + "px";
                     = function(){
                         var index = ("index");
                         if(index == null){ index = 0; var divs = ("div"); var left = parseInt(("_")[1]); for(var k=0;k<;k++){ if(divs[k].("splity_") < 0) continue; if(parseInt(divs[k].("_")[1]) < left) index++; } ("index",index);}else{index = parseInt(index);}
                         moveMode = "cellWidth";
                         moveArgs[] = index;
                     }
                     (split);
                 }
             }
             if(isadd){
                 loadHide();
             }
             [i].cells[j].onmousedown = function(){
                 //alert("x");
             }
         }
     }
 }

 function loadHide(){
     cellHide = [];
     var tempHide = [];
     for(var i=0;i<;i++){
         for(var j=0;j<[i].;j++){
             for(var k=1;k<[i].cells[j].rowSpan;k++){
                 cellHide[] = [i+k,j];
                 tempHide[] = [i+k,j];
             }
         }
     }
     for(var i=0;i<;i++){
         for(var j=0;j<[i].;j++){
             for(var k=1;k<[i].cells[j].colSpan;k++){
                 var yc = 0;
                 for(var l=0;l<;l++){
                     if(tempHide[l][0]==i&&tempHide[l][1]<j){yc++;}
                 }
                 for(var l=0;l<j;l++){
                     if([i].cells[l].colSpan > 1){yc+=[i].cells[l].colSpan-1;}
                 }
                 cellHide[] = [i,j+k+yc];
             }
         }
     }
 }

 addEvent(document,"mousemove",function(e){
     e = e || ;
if(moveMode == "cellWidth"){ //Adjust the width

         var temp = moveArgs[0];
         var test = "";
         for(var i=0;i<;i++){
             var index = temp;
             for(var j=0;j<;j++){
                 if(i==cellHide[j][0] && temp>=cellHide[j][1]){index--;}
             }
             if(![i].cells[index] || index < 0 || [i].cells[index].colSpan > 1){continue;}
             if( > absPos([i].cells[index]).x)
                 [i].cells[index]. = - absPos([i].cells[index]).x + "px";
         }
         loadTableEdit();
}else if(moveMode == "cellHeight"){ //Adjust the height
         var index = moveArgs[0];
         for(var i=0;i<[index].;i++){
             if([index].cells[i].rowSpan > 1){continue;}
             if( > absPos([index].cells[i]).y)
                 [index].cells[i]. = - absPos([index].cells[i]).y + "px";
         }
         loadTableEdit();
     }
 });
 addEvent(document,"mouseup",function(e){
     moveMode = ""; moveArgs = [];
 });
 addEvent(document,"mouseout",function(e){
     e = e || event;
     if (!isMouseLeaveOrEnter(e, this)) { return; }
     moveMode = ""; moveArgs = [];
 });