Sodium: Türkiye'nin Programlama Dili
  • Sodium Home Page
  • Getting Started
    • Examples
      • Hello World Example
      • Datalist & Select Element Usage
  • Installation & Configuration
  • Frequently Asked Questions
  • How to
    • How to: use Recordset variable and to_json function for populating a tree node
    • How to: get selected tree node id
    • How to: create master detail relationship between data blocks
    • How to: use data list element for populating select elements
    • How to: define a lookup element for a select element
    • How to: use database sequence in data block element
  • Contributers
  • Roadmap
  • About Me
  • Version History
    • Change Log
    • Things To Do
    • Known Issues
  • LANGUAGE REFERENCE
    • Program Structure
      • Form File
      • Code Behind File
      • Controller File
    • Built-in Functions
      • Sodium Built-in Functions
        • Database Related Functions
          • commit
          • rollback
          • delete
          • create_postgresql_connection
          • create_oracle_connection
          • create_mysql_connection
          • get_database_type
          • get_database_name
          • run_sql_file
          • set_active_database_connection
        • REDIS Functions
          • create_redis_connection
          • close_redis_connection
          • (redis-variable-name).set
          • (redis-variable-name).get
          • (redis-variable-name).del
          • (redis-variable-name).ping
          • (redis-variable-name).incr
          • (redis-variable-name).incrBy
          • (redis-variable-name).decr
          • (redis-variable-name).decrBy
          • (redis-variable-name).strlen
          • (redis-variable-name).append
        • Other Functions
          • disable_column
          • enable_column
          • hide_block
          • hide_column
          • message
          • populate_datalist
          • prompt
          • refresh_block
          • show_block
          • show_column
          • show_page
          • populate_tree
          • to_json
          • refresh_tree_node
          • set_datablock_property
      • Built-in String Functions & Operands
        • instr
        • strlen
        • Concatanation Operator
        • replace
        • substr
        • The "sizeof" Operator
        • Like & Not Like Operators
      • File IO functions
      • Date/time Functions
    • Built-in Triggers
      • "item_modified" trigger
      • "connection_not_found" trigger
      • "page.access" trigger
      • "page_load" trigger
      • "post_query" trigger
      • "row_selected" trigger
      • "button_item_clicked" trigger
      • "pre_insert" Trigger
      • "pre_delete" Trigger
      • "pre_update" Trigger
      • "post_insert" Trigger
      • "post_delete" Trigger
      • "post_update" Trigger
      • "tree_node_expanded" Trigger
      • "tree_node_selected" Trigger
      • "user_session_end" trigger
    • TAGs
      • Data Block
        • Data Block: Form View Mode
        • Data Block: Grid View Mode
        • Data Block: Mix View Mode
      • Control Block
      • Data List
      • Tree Element
      • Table TAG
      • Inputs
        • Text Item
        • Radio Item
        • Select Item
        • Checkbox Item
        • Button Item
        • Image Item
        • Textarea Item
        • Magic Buttons
    • Native SQL support
    • Lexical Elements
      • Identifiers
      • Keywords
      • Constants
        • The Null Statement
        • String Constant
      • Operators
      • Separators
      • White Space
      • Variables
        • Variable Type: int
        • Variable Type: char
        • Variable Type: bool
        • Local variables
        • Page variables
        • Session variables
        • Predefined Variables
          • :Session.authenticated
          • :Session.user
          • :Session.Id
          • :Session.user
          • :Row.Id
      • Functions
        • Function Declarations
        • Calling Functions
        • Function Parameters
        • Recursive Functions
      • Statements
        • The "if" Statement
        • The while Statement
        • The do Statement
        • Code Blocks
        • The break Statement
        • The return Statement
      • Expressions And Operators
        • Expressions
        • Assignment Operators
        • Arithmetic Operators
        • Comparison Operators
        • Logical Operators
        • Function Calls as Expressions
        • Operator Precedence
        • Order of Evaluation
      • Escape Character
    • CSS Themes
    • Scopes
    • Connection
      • Active Database Connection
      • Connection Types
        • Database Connections
        • REDIS Connection
  • Development
    • IDE
    • Debugging
  • SODIUM DEVELOPMENT
    • Sodium Development Home Page
    • Getting Started
      • Sodium Architecture
      • Development Environment
        • Source Code
        • Applications Required
        • Compiling C Projects
        • Compiling NodeJs Project
        • Troubleshooting
Powered by GitBook
On this page
  • Example:
  • See also:

Was this helpful?

  1. How to

How to: use Recordset variable and to_json function for populating a tree node

In order to generate json data from database, record set variable and to_json function can be used as shown in the example below,

Record set must ;

  • have at least 3 columns: "id", "text", "parent".

  • If actual table column name is different from "id", "text" and "parent", column alias can be used as showed above.

Record set can optionally have;

  • "children" column. As stated in jsTree documentation, recorset's column value must be either "true" or "false". True means the node has child(s) node, false means node is a leaf node and cannot be expanded.

Example:

SQLX File:

Form File:

<!DOCTYPE html>
  
 <html>
  
         <head>
                 <title>HT/SQL SHOWCASE (LOCATIONS TREE VIEW)</title>
         </head>
  
         <body>
                 <center>
  
                         <controlblock control-block-name="controlblock">
                                 <input name="show_all" type="button" value="All in one"/>
                                 <input name="show_deps" type="button" value="Departments"/>
                                 <input name="show_grid" type="button" value="Locations (Grid View)"/>
                 <input name="show_tree" type="button" value="Locations (Tree View)"/>
                                 <input name="show_colors" type="button" value="Colors"/>
                                 <input name="show_users" type="button" value="Users"/>
                                 <br />
                                 <br />
                                 <input name="commit" type="button" value="Commit"/>
                                 <input name="rollback" type="button" value="Rollback"/>
                                 |-|
                                 <input name="create-record" type="button" value=" + "/>
                                 <input name="delete-record" type="button" value=" - "/>
                                 |-|
                                 <input name="first-page" type="button" value="<<"/>
                                 <input name="prev-page" type="button" value=" < "/>
                                 <input name="next-page" type="button" value=" > "/>
                                 <input name="last-page" type="button" value=" >> "/>
                                 |-|
                                 <input name="enter-query" type="button" value="Enter Query"/>
                                 <input name="execute-query" type="button" value="Execute Query"/>
                                 <input name="cancel-query" type="button" value="Cancel Query"/>
                                 |-|
                                 <input name="kill-session" type="button" value="Log Out"  redirect-url="/site/pages/deps/logon.frmx"/>
                                 <br />
                                 <br />
                         </controlblock>
  
                         <table style="width: 850px">
                                 <tr>
                                         <td style="vertical-align: top; width: 275px">
                                                 <controlblock control-block-name="cbTree">
                                                         <tree id="data"></tree>
                                                 </controlblock>
                                         </td>
  
                                         <td style="vertical-align: top; width: 250px">
  
                                                 <datablock data-block-name="countries" data-schema-name="htsql" data-source-name="countries"
                                                         order-by-clause="country_name" record-count="10" show-hide-mode="display" master-data-block-name="Session">
                                                         <table>
                                                                 <thead>
                                                                         <tr>
                                                                                 <td>COUNTRY ID</td>
                                                                                 <td>COUNTRY NAME</td>
                                         <td>REGION ID</td>
                                                                         </tr>
                                                                 </thead>
                                                                 <tbody>
                                                                         <tr>
                                                                                 <td>
                                                                                         <input name="country_id" data-type="char" column-name="country_id" style="width: 50px"
                                                                                                 type="text" />
                                                                                 </td>
                                                                                 <td>
                                                                                         <input name="country_name" data-type="char" style="width: 150px"
                                                                                                 column-name="country_name" type="text"/>
                                                                                 </td>
                                         <td>
                                                                                         <input name="region_id" data-type="char" style="width: 50px"
                                                                                                 column-name="region_id" type="text" master-item-name="selectedNodeId"/>
                                                                                 </td>
                                                                         </tr>
                                                                 </tbody>
                                                         </table>
                                                 </datablock>
  
                                                 <datablock data-block-name="provinces" data-schema-name="htsql" data-source-name="provinces"
                                                         order-by-clause="province_name" record-count="10" show-hide-mode="display" master-data-block-name="Session">
                                                         <table>
                                                                 <thead>
                                                                         <tr>
                                         <td>PROVINCE ID</td>
                                                                                 <td>PROVINCE NAME</td>
                                         <td>COUNTRY ID</td>
                                                                         </tr>
                                                                 </thead>
                                                                 <tbody>
                                                                         <tr>
                                         <td>
                                                                                         <input name="province_id" column-name="province_id"     sequence-name="htsql.htsql"
                                                 type="text" style="width: 50px"/>
                                                                                 </td>
                                                                                 <td>
                                                                                         <input name="province_name" column-name="province_name" type="text" style="width: 150px"/>
                                                                                 </td>
                                         <td>
                                                                                         <input name="country_id" column-name="country_id"       type="text" style="width: 50px"
                                                 master-item-name="selectedNodeId"/>
                                                                                 </td>
                                                                         </tr>
                                                                 </tbody>
                                                         </table>
                                                 </datablock>
  
                         <datablock data-block-name="counties" data-schema-name="htsql" data-source-name="counties"
                                                         order-by-clause="county_name" record-count="10" show-hide-mode="display" master-data-block-name="Session">
                                                         <table>
                                                                 <thead>
                                                                         <tr>
                                         <td>COUNTY ID</td>
                                                                                 <td>COUNTY NAME</td>
                                         <td>PROVINCE ID</td>
                                                                         </tr>
                                                                 </thead>
                                                                 <tbody>
                                                                         <tr>
                                         <td>
                                                                                         <input name="county_id" column-name="county_id" sequence-name="htsql.htsql"
                                                 type="text" style="width: 50px"/>
                                                                                 </td>
                                                                                 <td>
                                                                                         <input name="county_name" column-name="county_name"     type="text" style="width: 150px"/>
                                                                                 </td>
                                         <td>
                                                                                         <input name="province_id" column-name="province_id"     type="text" style="width: 50px"
                                                 master-item-name="selectedNodeId"/>
                                                                                 </td>
                                                                         </tr>
                                                                 </tbody>
                                                         </table>
                                                 </datablock>
                                                 
                                         </td>
                                 </tr>
                         </table>
  
                 </center>
         </body>
 </html>

SQLX File:

void page.load() {
         populate_tree('cbTree.data');
 }
  
 void provinces.post_delete() {
         refresh_tree_node('cbTree.data', :Session.selectedNodeParentId);
 }
 void provinces.post_insert() {
         refresh_tree_node('cbTree.data', :Session.selectedNodeId);
 }
 void provinces.post_update() {
         refresh_tree_node('cbTree.data', :Session.selectedNodeParentId);
 }
  
 void countries.post_delete() {
         refresh_tree_node('cbTree.data', :Session.selectedNodeParentId);
 }
 void countries.post_insert() {
         refresh_tree_node('cbTree.data', :Session.selectedNodeId);
 }
 void countries.post_update() {
         refresh_tree_node('cbTree.data', :Session.selectedNodeParentId);
 }
  
 void counties.post_delete() {
         refresh_tree_node('cbTree.data', :Session.selectedNodeParentId);
 }
 void counties.post_insert() {
         refresh_tree_node('cbTree.data', :Session.selectedNodeId);
 }
 void counties.post_update() {
         refresh_tree_node('cbTree.data', :Session.selectedNodeParentId);
 }
  
 void cbTree.data.tree_node_selected(char node_id, char parent_id, char node_type) {
         :Session.selectedNodeId = node_id;
         :Session.selectedNodeParentId = parent_id;
  
     /* message('node_id : ' || node_id || '. node_type : ' || node_type);  */
  
     if (node_id == 'no_region') then
         show_page('locations_grid.frmx');
     end if;
  
         hide_block('countries');
         hide_block('provinces');
         hide_block('counties');
         
         if (node_type == 'R') then
                 refresh_block('countries', 'region_id = :node_id');
                 show_block('countries');
         end if;
         
         if (node_type == 'C') then
                 refresh_block('provinces', 'country_id = :node_id');
                 show_block('provinces');
         end if;
  
         if (node_type == 'P') then
                 refresh_block('counties', 'province_id = :node_id');
                 show_block('counties');
         end if;
 }
  
 char cbTree.data.tree_node_expanded(char node_id, char node_type) {
         char json;
         
         /* message('node_id : ' || node_id || '. node_type : ' || node_type); */
         
         if (node_id == '#') then
                 
                 rs =    select 
                                           region_id             as      id,
                                           region_name   as      text,
                                           '#'                   as      parent,
                                           'R'                   as      type,
                                           case (select count(*) from htsql.countries c where c.region_id = r.region_id)
                                                 when 0 then
                                                         'false' 
                                                 else
                                                         'true'
                                                 end                     children
                                         from
                                                 htsql.regions r;
         if (rs) then
             /*  */
             json = to_json(rs);
         else
             json = '[ { "id" : "no_region", "parent" : "#", "text" : "Click here to add new region" } ]';
         end if;
         end if;
                 
         if (node_type == 'R') then              
                 rs =    select 
                                   country_id    as      id,
                                   country_name  as      text,
                                   cc.region_id  as      parent,
                                   'C'                   as      type,
                                   case (select count(*) from htsql.countries c where c.region_id = cc.region_id)
                                         when 0 then
                                                 'false' 
                                         else
                                                 'true'
                                         end                     children
                                   
                                 from
                                         htsql.countries cc
                                 where
                                         cc.region_id = :node_id;
                 json = to_json(rs);
         end if;
         
         if (node_type == 'C') then
         
                  rs = select 
                                   PROVINCE_ID   as  id,
                                   PROVINCE_name as      text,
                                   country_id    as      parent,
                                   'P'                   as      type,
                                   case (select count(*) from htsql.counties c where c.province_id = cc.province_id)
                                         when 0 then
                                                 'false' 
                                         else
                                                 'true'
                                         end                     children
                                 from
                                         htsql.provinces cc
                                 where
                                         cc.country_id = :node_id;
         
                 json = to_json(rs);
         end if;
         
         if (node_type == 'P') then
         
              rs =       select 
                                 county_id     as        id,
                                 county_name   as        text,
                                 province_id   as        parent,
                                 'false'       as        children
                         from
                                 htsql.counties c
                         where
                                 c.province_id = :node_id;
  
                 json = to_json(rs);
         end if;
         
         return json;
 }

See also:

PreviousHow toNextHow to: get selected tree node id

Last updated 5 years ago

Was this helpful?

For detail on json data format :

https://www.jstree.com/docs/json/