1

I am trying with JSF + Primefaces to use a list of data that I retrieve from MySQL to build a Tree widget. My problem being that I don't know how make the Tree.

I have this data in my MySQL as an example:

   id   |       name      | class_id | fl_id
-----------------------------------------------
    1   |    MARKETING    |    0     |   1
    2   |   COMMERCIAL    |    0     |   1
    3   | TRADE-MARKETING |    1     |   1
    4   |      SALES      |    1     |   1
    5   |     RICHARD     |    1     |   2
    6   |      ROGER      |    1     |   3
    7   |      MARIA      |    2     |   4

Which should result in a tree display similar to:

MARKETING
   >TRADE MARKETING
   >RICHARD
      >>ROGER
COMMERCIAL
   >SALES
MARIA

As you notice, from the class id, the parent are 0 and the child are 1 (Which I observe is usually null for the father, but I can't change my database).

What I have coded so far is similar to this:

// Connection with MySQL, get result set, put all the data in my list and use a loop
// to count on that list. Then use an "if" condition to tell if 1 or 0, as child or
// father
for(count = 0, cont < rs.getString("class id").size(); count++)
    if(rs.getInt("class id") == 1) {
    }
    else if(rs.getInt("class id") == 2) {
    }
}
// Basically is wrong, because I can't tell what is the end, or leaf.

Basically its code that Ive conceptualized but not fully tested yet. I could post all the code, but I can't log in at work (somehow blocks it).

I've researched every page on Google I could find to find out how to do this, but it's pretty confusing how to make a tree with MySQL, by far static display is much simpler (from example on Primefaces). I am just a beginner in Java.

4

0 回答 0