JS organizational relationships are reflected by arrays.
In the body onload event, the initial array is used to obtain the maximum number of nodes in the hierarchy of the organizational relationship diagram and the maximum number of knots in the lower and link layers, to obtain the maximum width of the horizontal line.
The following is the original HTM code.
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> organization structure </title>
<Style>
. Dvitem
{
Position: absolute;
Width: 24px;
Border: 1px solid #999999;
Font-size: 12px;
Padding: 5px;
Height: 80px;
Z-index: 9;
Background-color: # ffffff;
Line-Height: 16px;
}
A
{
Text-Decoration: none;
Color: #333333;
}
. Dvhline
{
Position: absolute;
Z-index: 17;
Padding: 0px;
Margin: 0px;
Border-top: 1px solid #999999;
}
. Dvvline
{
Color: blue;
Position: absolute;
Background-color: #999999;
Width: 1px;
Z-index: 17;
}
</Style>
<SCRIPT>
VaR ditem = new array ();
VaR W = 600;
VaR H = 40;
VaR IW = 60;
VaR ih = 80;
VaR boxh = 80;
VaR startleft = 400;
VaR starttop = 40;
VaR hR = "<HR size =/" 1/"noshade>"
VaR labledv = "<Div class =/" dvitem/"style = /""
VaR HDV = "<Div class =/" dvhline/"style =/" width :";
VaR vdv = "<Div class =/" dvvline/"style =/" height: "+ H +" PX ;/"";
VaR endsdv = "/"> ";
VaR enddv = "</div> ";
VaR HTM = "";
VaR Len;
VaR maxn = 0; // depth
Function load_data ()
{
Ditem [0] = "1 | General Manager | 0 | ";
Ditem [1] = "2 | operation Director | 1 | ";
Ditem [2] = "3 | Technical Director | 1 | ";
Ditem [3] = "4 | customer service supervisor | 2 | ";
Ditem [4] = "5 | Business Director | 2 | ";
Ditem [5] = "6 | business representative | 5 | ";
Ditem [6] = "7 | operation planning | 2 | ";
Ditem [7] = "8 | programmer | 3 | ";
Ditem [8] = "9 | artist | 3 | ";
Len = ditem. length;
Set_item (0, 0 );
Set_max ();
Write_item (0, 0, 0, 1 );
VaR htm1 = "";
For (VAR I = 0; I <Len; I ++)
{
Htm1 = htm1 + ditem [I] + "<br> ";
}
// Document. getelementbyid ("load_dv"). innerhtml = htm1;
Document. getelementbyid ("list_dv"). innerhtml = HTM;
}
// Set the level
Function set_item (PID, Ni)
{
VaR n = Ni + 1;
VaR iary = new array ();
For (VAR I = 0; I <Len; I ++)
{
Iary = ditem [I]. Split ("| ");
If (iary [2] = PID)
{
Ditem [I] = ditem [I] + Ni;
If (maxn <Ni)
{
Maxn = ni;
}
Set_item (iary [0], n );
}
}
}
// Set the maximum number of subnodes in a node
Function set_max ()
{
VaR iary = new array ();
VaR childnum;
For (VAR I = 0; I <Len; I ++)
{
Iary = ditem [I]. Split ("| ");
Childnum = get_child_num (iary [0]);
If (childnum <= 1)
{
Ditem [I] = ditem [I] + "| 0 ";
}
Else
{
Ditem [I] = ditem [I] + "|" + get_max (iary [0], iary [3]);
}
}
}
Function get_max (PID, start)
{
VaR iary = new array ();
VaR m = 0;
VaR n = 0;
For (var j = start; j <= maxn; j ++)
{
For (VAR I = 0; I <Len; I ++)
{
Iary = ditem [I]. Split ("| ");
If (iary [3] = J)
{
If (get_rootid (PID, iary [0])
{
M = m + 1;
}
}
If (n <m)
{
N = m;
}
}
M = 0;
}
Return N;
}
Function get_rootid (PID, ID)
{
VaR iary = new array ();
For (VAR I = 0; I <Len; I ++)
{
Iary = ditem [I]. Split ("| ");
If (iary [0] = ID)
{
If (iary [2] = PID)
{
Return true;
Break;
}
Else
{
Return get_rootid (PID, iary [2]);
}
}
}
Return false;
}
// Obtain the array of IDS
Function get_item (ID)
{
VaR I;
VaR items;
VaR iary = new array ();
For (I = 0; I <Len; I ++)
{
Iary = ditem [I]. Split ("| ");
If (iary [0] = ID)
{
Items = ditem [I];
Break;
}
}
Return items;
}
// Obtain the number of subnodes
Function get_child_num (PID)
{
VaR I;
VaR rnum = 0;
VaR iary = new array ();
For (I = 0; I <Len; I ++)
{
Iary = ditem [I]. Split ("| ");
If (iary [2] = PID)
{
Rnum = rnum + 1;
}
}
Return rnum;
}
Function write_item (ipid, ltmp, wtmp, CNT)
{
VaR iary = new array ();
VaR ID;
VaR txt;
VaR PID;
VaR lens;
VaR maxnum;
VaR T;
VaR L;
VaR hline_width;
VaR dvline = "";
VaR childnum = 0;
VaR itxt;
VaR tmpcnt = 0;
For (VAR I = 0; I <Len; I ++)
{
Itxt = ditem [I];
Iary = itxt. Split ("| ");
If (iary [2] = ipid)
{
Id = iary [0];
TXT = "<a href = /"? Id = "+ ID +"/">" + iary [1] + "</a> ";
PID = iary [2];
Lens = iary [3];
Maxnum = iary [4];
Childnum = get_child_num (ID );
Hline_width = maxnum * IW;
If (pid = 0)
{
T = starttop;
L = startleft;
}
Else
{
T = starttop + 2 * lens * H + lens * ih;
// L = ltmp + wtmp/CNT * tmpcnt;
L = ltmp-wtmp/2 + (wtmp/2) * tmpcnt;
}
Dvline = "";
If (childnum> 1)
{
VaR T1;
VaR L1;
VaR T2;
VaR L2;
VaR W2;
T1 = T + ih;
L1 = L + 12;
W2 = hline_width/2;
T2 = t1 + h;
L2 = L-W2 + 10;
Dvline = "<Div class =/" dvvline/"style =/" height: "+ H +" PX; left: "+ l1 +" PX; top: "+ t1 +"/"> </div> ";
Dvline = dvline + "<Div class =/" dvhline/"style =/" width: "+ hline_width +" PX; left: "+ l2 +" PX; top: "+ T2 +"/"> </div> ";
For (var j = 0; j <childnum; j ++)
{
VaR T3;
VaR l3;
T3 = t1 + h;
L3 = l2 + (hline_width/(childnum-1) * J;
VaR tmpline = "<Div class =/" dvvline/"style =/" height: "+ H +" PX; left: "+ l3 +" PX; top: "+ T3 +"/"> </div> ";
Dvline = dvline + tmpline;
}
Dvline = dvline
}
Else if (childnum = 1)
{
VaR T4;
VaR L4;
L4 = L + 12;
Dvline = "<Div class =/" dvvline/"style =/" height: "+ H +" PX; left: "+ L4 +" PX; top: "+ (t + IH) +"/"> </div> ";
Dvline = dvline + "<Div class =/" dvvline/"style =/" height: "+ H +" PX; left: "+ L4 +" PX; top: "+ (t + ih + H) +"/"> </div> ";
}
// Dvline = HDV + hline_width + endsdv + HR + enddv; <HR size =/"1/" noshade>
// Var LDV = "";
// LDV = "<Div class =/" dvitem/"style =/" Left: "+ L +" PX; top: "+ T +" PX; /">" + TXT + "<div>"
// HTM = HTM + LDV + dvline;
Htm = HTM + labledv + "Left:" + L + "PX; top:" + T + "PX" + endsdv + TXT + enddv + dvline;
If (CNT % 2 = 0)
{
Tmpcnt = tmpcnt + 2;
}
Else
{
Tmpcnt = tmpcnt + 1;
}
Write_item (ID, L, hline_width, childnum );
} // If (iary [2] = ipid)
}
}
</SCRIPT>
</Head>
<Body onload = "load_data ()">
<Div id = "list_dv"> </div>
</Body>
</Html>