====== Nested Set Model ====== As discussed in previous chapter [[:wiki:treegrid | Configuration]] one of the important part is the treeReader property. Configuring this properly and understanding the basic concept of the nested set model will make your life easy. ===== treeReader Configuration ===== The default treeReader Configuration when we use tree grid with nested set model is: treeReader : { level_field: "level", left_field:"lft", right_field: "rgt", leaf_field: "isLeaf", expanded_field: "expanded" } The treeReader automatically extends the colModel with these fields, added and hidden at end of the colModel. Data returned from the server now needs to include information for these fields for constructing the tree grid. The treeReader can be extended so that the fields match your requirements. ^Field^Type^Description^ |level_field|number|this field determines the level in the hierarchy of the element. Usually the root element will be at level 0.The first child of the root is at level 1 and so on. This information is needed for the grid to set the ident of every element.| |left_field|number|rowid of the field to the left| |right_field|number|rowid of the field to the right| |leaf_field|boolean|This field should tell the grid that the element is leaf. Possible values can be true and false. To the leaf element is attached diffrent image and this element can not be expanded or collapsed.| |expanded_field|boolean|Tells the grid whether this element should be expanded during the loading (true or false). If the element has no value, false is set. Note that the data can be empty for this element, but this element can not be removed from data set.| Another option that can be changed is tree_root_level. By default this has value 0. This option tell which level has the root element. ===== What we post? ===== After we configure the reader we need to know what we post to the server in order to load the child nodes properly? In case of auto lading tree nodes we post the following parameters - also the postData array is extended. See [[wiki:treegrid#cautions_and_limitations | here]] postData : { ... nodeid:rc.id, n_left:rc.lft, n_right:rc.rgt, n_level:rc.level, ... } * nodeid contain the id of the currently expanded record * n_left contain the left value of the currently expanded row * n_right contain the right value of the currently expanded row * n_level contain the level value of the currently expanded row ===== Example ===== In order to understand the the process of configuring the tree grid here we provide full example: ==== Data preparation ==== Let us suppose that we have an account table where some accounts are children of the main accounts and some accounts have no child account. In the Nested Set model the table can look like this account_id, name, account_number, Debit, Credit, Balance, lft, rgt where: * account_id is the uniquie id of the account (in our grid this should be the rowid) * lft indicates the left_field, and * rgt indicates the right_field In MySQL terms this table can be represented as CREATE TABLE accounts ( account_id int(11) NOT NULL auto_increment, name varchar(30) NOT NULL, acc_num varchar(10) NULL, debit decimal(10,2) default '0.00', credit decimal(10,2) default '0.00', balance decimal(10,2) default '0.00', lft int(11) NOT NULL, rgt int(11) NOT NULL, PRIMARY KEY (`account_id`) ); Let's add some data: INSERT INTO accounts VALUES (1, 'Cash', '100', 400.00, 250.00, 150.00, 1, 8); INSERT INTO accounts VALUES (2, 'Cash 1', '1', 300.00, 200.00, 100.00, 2, 5); INSERT INTO accounts VALUES (3, 'Sub Cash 1', '1', 300.00, 200.00, 100.00, 3, 4); INSERT INTO accounts VALUES (4, 'Cash 2', '2', 100.00, 50.00, 50.00, 6, 7); INSERT INTO accounts VALUES (5, 'Bank''s', '200', 1500.00, 1000.00, 500.00, 9, 14); INSERT INTO accounts VALUES (6, 'Bank 1', '1', 500.00, 0.00, 500.00, 10, 11); INSERT INTO accounts VALUES (7, 'Bank 2', '2', 1000.00, 1000.00, 0.00, 12, 13); INSERT INTO accounts VALUES (8, 'Fixed asset', '300', 0.00, 1000.00, -1000.00, 15, 16); With this information we can now construct the treeGrid. ==== Grid preparation ====