Updating jtree

6854933580_2c8b688306_z

In this article we will examine two models for dealing with hierarchical data in My SQL, starting with the traditional adjacency list model.

Typically the example categories shown above will be stored in a table like the following (I’m including full CREATE and INSERT statements so you can follow along): CREATE TABLE category( category_id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(20) NOT NULL, parent INT DEFAULT NULL ); INSERT INTO category VALUES(1,'ELECTRONICS', NULL),(2,'TELEVISIONS',1),(3,'TUBE',2), (4,'LCD',2),(5,'PLASMA',2),(6,'PORTABLE ELECTRONICS',1),(7,'MP3 PLAYERS',6),(8,'FLASH',7), (9,'CD PLAYERS',6),(10,'2 WAY RADIOS',6); SELECT * FROM category ORDER BY category_id; ------------- ---------------------- -------- | category_id | name | parent | ------------- ---------------------- -------- | 1 | ELECTRONICS | NULL | | 2 | TELEVISIONS | 1 | | 3 | TUBE | 2 | | 4 | LCD | 2 | | 5 | PLASMA | 2 | | 6 | PORTABLE ELECTRONICS | 1 | | 7 | MP3 PLAYERS | 6 | | 8 | FLASH | 7 | | 9 | CD PLAYERS | 6 | | 10 | 2 WAY RADIOS | 6 | ------------- ---------------------- -------- 10 rows in set (0.00 sec).

While the adjacency list model can be dealt with fairly easily in client-side code, working with the model can be more problematic in pure SQL.

The first common task when dealing with hierarchical data is the display of the entire tree, usually with some form of indentation.

updating jtree-5updating jtree-81

For our purposes, hierarchical data is a collection of data where each item has a single parent and zero or more children (with the exception of the root item, which has no parent).

If parent_node is empty, then the new node becomes a root node.

If you want to use this function, then your tree data should include an id property for each node.

So, when we attempt to convert this into actual working code, the repetitive part of getting the cell values one by one yields itself to a loop.

Also, since we are dealing with a 2 dimensional data, this means we would need 2 loops – a loop within a loop i.e an inner loop.

You can use this function in combination with set State to save and restore the tree state.

You must have an account to comment. Please register or login here!