Tree-like Java model, hierarchical relational Java model, hierarchical relational Java model and HTML page display
First, the business prototype: the company's organizational structure, marketing Network
Second, the database model
Very simple, create ID and PID relationship can be. (pid:parent_id)
Third, Java model
( We scatter this net in a table of HTML.) In fact, with UL to show simple n many, their own thinking why LZ will choose to put in the table )
Private class Table {
Private Long ID; ID of the current object
private int x; Abscissa
private int y; Ordinate
private list<table> tables; The subordinate collection of the current object
Public Table (Long ID) {
This.id = ID; Quick and easy creation of objects
}
}
1. Assemble Java model data
1) Find all data from the table at once
2) Create the following map
The correspondence between PID and list<id> ancestor ID and subordinate object
Map<long, list<table>> psmap = new hashmap<> (); Simple, not detailed description of the
3) Create a top-level node
Use the ID of the top-level object to new a table, and then remove its subordinates from the above psmap based on the ID to the table's tables.
4) Recursive completion of all child nodes
Loop the Tables collection of the Table object, repeating the previous step
5) Calculates the horizontal (x) vertical (y) coordinates of each Table object
Create an object here again
Private class Max {
private int x = 1; Row of Table
private int y = 1; Column
}
//Calculate ordinate
Private voidCounty (list<table> tables,inty, Max Max) { if(Tables! =NULL) for(Table table:tables) {table.y=y; County (table.tables, y+ 1, Max); if(Y >max.y) max.y=y; } }
Calculate the horizontal axis
Private voidCountx (list<table> tables,intx, Max Max) { if(Tables! =NULL) for(Table table:tables) {table.x=max.x; if(Table.tables! =NULL) {max.x++; } countx (Table.tables, max.x, Max); } Elsemax.x= x + 1; }
2, create a two-dimensional array, corresponding to the table in the HTML page to show
t[][] ts = new T[MAX.X][MAX.Y]; T corresponds to the object in the database table
The above Java model already calculates the horizontal (x) vertical (y) coordinates of each object, and fills the two-dimensional array to
Iv. HTML page display
1. Draw a table based on the previous Max object, dimension coordinates
2. Fill the table with the above two-dimensional array
Five, the form to draw well, next you can not fold means (this is why I do not use UL)
1. Count the data for each column in each row
2, set a variety of disorderly 87 bad style
Tree-like Java model, hierarchical relational Java model, hierarchical relational Java model and HTML page display