Sample 2-3. Advanced DataGrid code.

    1. All modes (Add/Edit/Details/Delete/View).
    2. All features.
    3. One DataGrid splited on 2 parts with different styles.
Show Plain Text »
  1. <?php
  2.  
  3. $pr_rid = isset($_REQUEST["pr_rid"]) ? $_REQUEST["pr_rid"] : "";
  4. $pr_mode = isset($_REQUEST["pr_mode"]) ? $_REQUEST["pr_mode"] : "";
  5.  
  6. if(($pr_mode == "edit") || ($pr_mode == "details") || ($pr_mode == "add") || ($pr_mode == "cancel")){
  7.     $_REQUEST["pr_mode"] = "view";
  8.     $_REQUEST["pr_rid"] = "";
  9.     if(($pr_mode == "cancel") && ($pr_rid == "-1")){
  10.       $_REQUEST["pr_page_size"] = "";      
  11.     }
  12. }
  13.  
  14. ## +---------------------------------------------------------------------------+
  15. ## | 1. Creating & Calling:                                                    |
  16. ## +---------------------------------------------------------------------------+
  17. ##  *** define a relative (virtual) path to datagrid.class.php file
  18. ##  *** directory (relatively to the current file)
  19. ##  *** RELATIVE PATH ONLY ***
  20. define ("DATAGRID_DIR", "datagrid/");                    
  21. require_once(DATAGRID_DIR."datagrid.class.php");
  22.  
  23. ##  *** creating variables that we need for database connection    
  24. $DB_USER = "db_user";
  25. $DB_PASS = "db_password";      
  26. $DB_HOST = "db_host";      
  27. $DB_NAME = "db_name";      
  28.  
  29. ##  *** put a primary key on the first place
  30. $sql = "
  31.    SELECT
  32.        demo_presidents.id,
  33.        demo_presidents.country_id,
  34.        demo_presidents.name,
  35.        demo_presidents.birth_date,
  36.        demo_presidents.status,
  37.        demo_countries.name as country_name,
  38.        'Edit' as lnk_edit,
  39.        'Details' as lnk_details      
  40.    FROM demo_presidents
  41.    INNER JOIN demo_countries ON demo_presidents.country_id=demo_countries.id ";
  42.  
  43. ##  *** set needed options and create a new class instance
  44. $debug_mode = false;        /* display SQL statements while processing */    
  45. $messaging = true;          /* display system messages on a screen */
  46. $unique_prefix = "pr_";     /* prevent overlays - must be started with a letter */
  47. $dgrid = new DataGrid($debug_mode, $messaging, $unique_prefix);
  48.  
  49. ##  *** set data source with needed options
  50. $default_order = array("id"=>"ASC");
  51. $dgrid->DataSource("PDO", "mysql", $DB_HOST, $DB_NAME, $DB_USER, $DB_PASS, $sql, $default_order);            
  52.  
  53. ## +---------------------------------------------------------------------------+
  54. ## | 2. General Settings:                                                      |
  55. ## +---------------------------------------------------------------------------+
  56. ## +-- PostBack Submission Method ---------------------------------------------+
  57. ##  *** defines postback submission method for DataGrid: AJAX, POST(default) or GET
  58. $postback_method = "post";
  59. $dgrid->SetPostBackMethod($postback_method);
  60.  
  61. $modes = array(
  62.   "add"  =>array("view"=>true, "edit"=>false, "type"=>"link", "show_add_button"=>"outside"),
  63.   "edit"         =>array("view"=>false, "edit"=>true,  "type"=>"link", "byFieldValue"=>""),
  64.   "details" =>array("view"=>false, "edit"=>false, "type"=>"link"),
  65.   "delete"  =>array("view"=>true, "edit"=>true,  "type"=>"image")
  66. );
  67. $dgrid->SetModes($modes);
  68. ##  *** allow mulirow operations
  69. $multirow_option = false;
  70. $dgrid->AllowMultirowOperations($multirow_option);
  71.  
  72. ##  *** set DataGrid caption
  73. $dg_caption = "Presidents";
  74. $dgrid->SetCaption($dg_caption);
  75.  
  76. ## +---------------------------------------------------------------------------+
  77. ## | 3. Printing & Exporting Settings:                                         |
  78. ## +---------------------------------------------------------------------------+
  79. ##  *** set printing option: true(default) or false
  80. $printing_option = false;
  81. $dgrid->AllowPrinting($printing_option);
  82.  
  83. ## +---------------------------------------------------------------------------+
  84. ## | 4. Sorting & Paging Settings:                                             |
  85. ## +---------------------------------------------------------------------------+
  86. ##  *** set paging option: true(default) or false
  87. $paging_option = true;
  88. $rows_numeration = false;
  89. $numeration_sign = "N #";      
  90. $dgrid->AllowPaging($paging_option, $rows_numeration, $numeration_sign);
  91. ##  *** set paging settings
  92. $bottom_paging = array("results"=>true, "results_align"=>"left", "pages"=>true, "pages_align"=>"center", "page_size"=>true, "page_size_align"=>"right");
  93. $top_paging = array();
  94. $pages_array = array("10"=>"10", "25"=>"25", "50"=>"50", "100"=>"100", "250"=>"250", "500"=>"500", "1000"=>"1000");
  95. $default_page_size = 10;
  96. $dgrid->SetPagingSettings($bottom_paging, $top_paging, $pages_array, $default_page_size);
  97.  
  98. ## +---------------------------------------------------------------------------+
  99. ## | 5. Filter Settings:                                                       |
  100. ## +---------------------------------------------------------------------------+
  101. ##  *** set filtering option: true or false(default)
  102. $filtering_option = true;
  103. $show_search_type = false;
  104. $dgrid->AllowFiltering($filtering_option, $show_search_type);
  105. ##  *** set aditional filtering settings
  106. $filtering_fields = array(
  107.   "Name"=>array("table"=>"demo_presidents", "field"=>"name", "source"=>"self", "show_operator"=>false, "default_operator"=>"like%", "order"=>"ASC", "type"=>"textbox", "case_sensitive"=>false, "comparison_type"=>"string"),
  108. );
  109. $dgrid->SetFieldsFiltering($filtering_fields);
  110.  
  111. ## +---------------------------------------------------------------------------+
  112. ## | 6. View Mode Settings:                                                    |
  113. ## +---------------------------------------------------------------------------+
  114. ##  *** set view mode table properties
  115. $vm_table_properties = array("width"=>"95%");
  116. $dgrid->SetViewModeTableProperties($vm_table_properties);  
  117. $vm_columns = array(
  118.    "name" => array("header"=>"Name", "type"=>"label", "align"=>"left", "width"=>"", "wrap"=>"nowrap", "text_length"=>"-1", "tooltip"=>false, "tooltip_type"=>"simple", "case"=>"normal", "summarize"=>"false", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),
  119.    "m_birth_date" => array("header"=>"Date of Birth", "type"=>"label", "align"=>"left", "width"=>"", "wrap"=>"nowrap", "text_length"=>"-1", "tooltip"=>false, "tooltip_type"=>"simple", "case"=>"normal", "summarize"=>"false", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),
  120.    "status" => array("header"=>"Status", "type"=>"label", "align"=>"left", "width"=>"", "wrap"=>"nowrap", "text_length"=>"-1", "tooltip"=>false, "tooltip_type"=>"simple", "case"=>"normal", "summarize"=>"false", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),
  121.    "country_name" => array("header"=>"Country", "type"=>"label", "align"=>"left", "width"=>"", "wrap"=>"nowrap", "text_length"=>"-1", "tooltip"=>false, "tooltip_type"=>"simple", "case"=>"normal", "summarize"=>"false", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),
  122.    "lnk_edit" => array("header"=>" ", "type"=>"link", "align"=>"center", "width"=>"", "wrap"=>"nowrap", "text_length"=>"-1", "tooltip"=>false, "tooltip_type"=>"simple", "case"=>"normal", "summarize"=>"false", "sort_by"=>"", "visible"=>"true", "on_js_event"=>"", "field_key"=>"id", "field_data"=>"lnk_edit", "rel"=>"", "title"=>"", "target"=>"", "href"=>"javascript:pr__doPostBack('edit','{0}','');"),
  123.    "lnk_details" => array("header"=>" ", "type"=>"link", "align"=>"center", "width"=>"", "wrap"=>"nowrap", "text_length"=>"-1", "tooltip"=>false, "tooltip_type"=>"simple", "case"=>"normal", "summarize"=>"false", "sort_by"=>"", "visible"=>"true", "on_js_event"=>"", "field_key"=>"id", "field_data"=>"lnk_details", "rel"=>"", "title"=>"", "target"=>"", "href"=>"javascript:pr__doPostBack('details','{0}','');"),
  124. );
  125. $dgrid->SetColumnsInViewMode($vm_columns);
  126.  
  127.  
  128. ## +---------------------------------------------------------------------------+
  129. ## | 7. Add/Edit/Details Mode Settings:                                        |
  130. ## +---------------------------------------------------------------------------+
  131. ##  *** set add/edit mode table properties
  132. $em_table_properties = array("width"=>"95%");
  133. $dgrid->SetEditModeTableProperties($em_table_properties);
  134. ##  *** set details mode table properties
  135. $dm_table_properties = array("width"=>"95%");
  136. $dgrid->SetDetailsModeTableProperties($dm_table_properties);
  137. ##  ***  set settings for add/edit/details modes
  138. $table_name  = "demo_presidents";
  139. $primary_key = "id";
  140. $condition   = "";
  141. $dgrid->SetTableEdit($table_name, $primary_key, $condition);
  142. ##  *** set columns in edit mode
  143. $em_columns = array(
  144.   "name" => array("header"=>"Name", "type"=>"textbox",  "align"=>"left", "req_type"=>"rt", "width"=>"120px", "title"=>"Name", "readonly"=>false, "maxlength"=>"-1", "default"=>"", "unique"=>false, "unique_condition"=>"", "visible"=>"true", "on_js_event"=>""),
  145.   "country_id" => array("header"=>"Country", "type"=>"textbox", "align"=>"left",  "req_type"=>"rt", "width"=>"120px", "title"=>"Country", "readonly"=>false, "maxlength"=>"-1", "default"=>"", "unique"=>false, "unique_condition"=>"", "visible"=>"true", "on_js_event"=>""),
  146.   "birth_date" => array("header"=>"Birth Date", "type"=>"date",  "req_type"=>"st", "width"=>"187px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "calendar_type"=>"floating"),
  147.   "status" => array("header"=>"Status", "type"=>"enum",  "align"=>"left",    "req_type"=>"st", "width"=>"120px", "title"=>"Status", "readonly"=>false, "maxlength"=>"-1", "default"=>"", "unique"=>false, "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "source"=>"self", "view_type"=>"dropdownlist", "multiple"=>false, "multiple_size"=>"1"),
  148. );
  149. $dgrid->SetColumnsInEditMode($em_columns);
  150.  
  151. $foreign_keys = array(
  152.    "country_id"=>array("table"=>"demo_countries", "field_key"=>"id", "field_name"=>"name", "view_type"=>"dropdownlist", "condition"=>"", "order_by_field"=>"name", "order_type"=>"ASC", "on_js_event"=>""),
  153. );
  154. $dgrid->SetForeignKeysEdit($foreign_keys);
  155.  
  156. ## +---------------------------------------------------------------------------+
  157. ## | 8. Bind the DataGrid:                                                     |
  158. ## +---------------------------------------------------------------------------+
  159. ##  *** bind the DataGrid and draw it on the screen
  160. $dgrid->Bind();        
  161.  
  162. ?>