Sample 2-7. Advanced DataGrid code. AJAX sorting and paging.

    1. All modes (Add/Edit/Details/Delete/View).
    2. All features.
    3. Dependent dropdown boxes (regions and countries).
    4. AJAX PostBack submission type.
Show Plain Text »
  1. <?php
  2.  
  3. $postback = (isset($_REQUEST["postback"]) && $_REQUEST["postback"] != "") ? strip_tags($_REQUEST["postback"]) : "ajax";
  4. $mode = isset($_REQUEST[$unique_prefix."mode"]) ? $_REQUEST[$unique_prefix."mode"] : "";    
  5. $region_id = isset($_REQUEST["riyregion_id"]) ? (int)$_REQUEST["riyregion_id"] : "";
  6.  
  7. ## +---------------------------------------------------------------------------+
  8. ## | 1. Creating & Calling:                                                    |
  9. ## +---------------------------------------------------------------------------+
  10. ##  *** define a relative (virtual) path to datagrid.class.php file
  11. ##  *** directory (relatively to the current file)
  12. ##  *** RELATIVE PATH ONLY ***
  13. define ("DATAGRID_DIR", "datagrid/");                    
  14. require_once(DATAGRID_DIR."datagrid.class.php");
  15.  
  16. ##  *** creating variables that we need for database connection    
  17. $DB_USER = "db_user";
  18. $DB_PASS = "db_password";      
  19. $DB_HOST = "db_host";      
  20. $DB_NAME = "db_name";      
  21.  
  22. ##  *** put a primary key on the first place
  23. $sql=" SELECT
  24.        demo_presidents.*,
  25.        demo_countries.name as country_name            
  26.    FROM demo_presidents
  27.        INNER JOIN demo_countries ON demo_presidents.country_id=demo_countries.id ";
  28.  
  29. ##  *** set needed options and create a new class instance
  30. $debug_mode = false;        /* display SQL statements while processing */    
  31. $messaging = true;          /* display system messages on a screen */
  32. $dgrid = new DataGrid($debug_mode, $messaging, $unique_prefix);
  33. ##  *** set data source with needed options
  34. $default_order = array("rating"=>"ASC");
  35. $dgrid->DataSource("PDO", "mysql", $DB_HOST, $DB_NAME, $DB_USER, $DB_PASS, $sql, $default_order);              
  36.  
  37. ## +---------------------------------------------------------------------------+
  38. ## | 2. General Settings:                                                      |
  39. ## +---------------------------------------------------------------------------+
  40. ## +-- PostBack Submission Method ---------------------------------------------+
  41. ##  *** defines postback submission method for DataGrid: AJAX, POST or GET(default)
  42. $dgrid->SetPostBackMethod("ajax");
  43. ##  *** set layouts: "0" - tabular(horizontal) - default, "1" - columnar(vertical), "2" - customized
  44. $layouts = array("view"=>"0", "edit"=>"1", "details"=>"1", "filter"=>"2");
  45. $dgrid->SetLayouts($layouts);
  46. ##  *** allow multirow operations
  47. $dgrid->AllowMultirowOperations(true);
  48. $multirow_operations = array(
  49.     "edit"  => array("view"=>true),
  50.     "delete"  => array("view"=>true),
  51.     "details" => array("view"=>true),
  52. );
  53. $dgrid->SetMultirowOperations($multirow_operations);  
  54. ##  *** set DataGrid caption
  55. $dg_caption = "Presidents";
  56. $dgrid->SetCaption($dg_caption);
  57.  
  58. ## +---------------------------------------------------------------------------+
  59. ## | 5. Filter Settings:                                                       |
  60. ## +---------------------------------------------------------------------------+
  61. ##  *** set filtering option: true or false(default)
  62. $filtering_option = true;
  63. $show_search_type = true;
  64. $dgrid->AllowFiltering($filtering_option, $show_search_type);
  65. ##  *** set additional filtering settings
  66. $filtering_fields = array(    
  67.  "Name"       =>array("type"=>"textbox", "table"=>"demo_presidents", "field"=>"name", "show_operator"=>"false", "default_operator"=>"like%", "case_sensitive"=>"false", "comparison_type"=>"string", "width"=>"80px", "on_js_event"=>""),
  68.  "Birth From" =>array("type"=>"calendar", "field_type"=>"from", "calendar_type"=>"floating", "date_format"=>"date", "table"=>"demo_presidents", "field"=>"birth_date", "show_operator"=>"false", "default_operator"=>">=", "case_sensitive"=>"false", "comparison_type"=>"string", "width"=>"70px", "on_js_event"=>""),
  69.  "To"         =>array("type"=>"calendar", "field_type"=>"to", "calendar_type"=>"floating", "date_format"=>"date", "table"=>"demo_presidents", "field"=>"birth_date", "show_operator"=>"false", "default_operator"=>"<=", "case_sensitive"=>"false", "comparison_type"=>"string", "width"=>"70px", "on_js_event"=>""),
  70.  "Status"     =>array("type"=>"enum", "table"=>"demo_presidents", "field"=>"status", "field_view"=>"", "filter_condition"=>"", "order"=>"ASC", "source"=>"self", "view_type"=>"radiobutton", "condition"=>"", "show_operator"=>"false", "default_operator"=>"=", "case_sensitive"=>"false", "comparison_type"=>"string", "width"=>"", "multiple"=>"false", "multiple_size"=>"4", "on_js_event"=>""),
  71. );
  72. $dgrid->SetFieldsFiltering($filtering_fields);
  73.  
  74. ## +---------------------------------------------------------------------------+
  75. ## | 6. View Mode Settings:                                                    |
  76. ## +---------------------------------------------------------------------------+
  77. ##  *** set view mode table properties
  78. $vm_table_properties = array("width"=>"85%");
  79. $dgrid->SetViewModeTableProperties($vm_table_properties);  
  80. ##  *** set columns in view mode
  81. $vm_columns = array(
  82.  "name" =>array("header"=>"Name", "type"=>"label", "align"=>"left", "wrap"=>"wrap", "text_length"=>"26", "case"=>"normal", "tooltip"=>"true"),
  83.  "birth_date" =>array("header"=>"Birth Date", "type"=>"label", "align"=>"center", "header_tooltip"=>"Shows the birth date", "wrap"=>"nowrap", "text_length"=>"-1", "case"=>"normal"),
  84.  "status" =>array("header"=>"Status", "type"=>"label", "align"=>"center", "header_tooltip"=>"Shows the current status", "wrap"=>"nowrap", "text_length"=>"30", "case"=>"normal"),
  85.  "favorite_color" =>array("header"=>"Favorite Color", "type"=>"color", "align"=>"center", "width"=>"", "wrap"=>"wrap|nowrap", "text_length"=>"-1", "tooltip"=>"false", "tooltip_type"=>"floating|simple", "case"=>"normal|upper|lower|camel", "sort_type"=>"string|numeric", "sort_by"=>"", "visible"=>"true", "on_js_event"=>"", "view_type"=>"image"),
  86.  "is_active" =>array("header"=>"Active", "type"=>"checkbox", "align"=>"center", "width"=>"", "wrap"=>"nowrap", "sort_type"=>"numeric", "sort_by"=>"", "visible"=>"true", "on_js_event"=>"", "true_value"=>1, "false_value"=>0),
  87.  "rating" =>array("header"=>"Rating", "type"=>"label", "align"=>"center", "wrap"=>"nowrap", "text_length"=>"-1", "case"=>"normal", "movable"=>"true"),
  88.  "country_name" =>array("header"=>"Country", "type"=>"label", "align"=>"center", "wrap"=>"nowrap", "text_length"=>"30", "case"=>"normal"),
  89. );
  90. $dgrid->SetColumnsInViewMode($vm_columns);
  91.  
  92. ## +---------------------------------------------------------------------------+
  93. ## | 7. Add/Edit/Details Mode Settings:                                        |
  94. ## +---------------------------------------------------------------------------+
  95. ##  *** set add/edit mode table properties
  96. $em_table_properties = array("width"=>"75%");
  97. $dgrid->SetEditModeTableProperties($em_table_properties);
  98. ##  *** set details mode table properties
  99. $dm_table_properties = array("width"=>"60%");
  100. $dgrid->SetDetailsModeTableProperties($dm_table_properties);
  101. ##  ***  set settings for add/edit/details modes
  102. $table_name  = "demo_presidents";
  103. $primary_key = "id";
  104. $condition   = "";
  105. $dgrid->SetTableEdit($table_name, $primary_key, $condition);
  106. ##  *** set columns in edit mode  
  107. $em_columns = array(
  108.  "name" =>array("header"=>"Name", "type"=>"textbox", "width"=>"140px", "req_type"=>"rt", "title"=>"Name"),
  109.  "birth_date" =>array("header"=>"Birth Date", "type"=>"date", "req_type"=>"rt", "width"=>"80px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "calendar_type"=>"floating"),
  110.  "work_phone" =>array("header"=>"Work Phone", "type"=>"textbox", "width"=>"140px", "req_type"=>"rx", "template"=>"(ddd)-ddd-dd-dd", "title"=>"Work Phone"),
  111.  "status" =>array("header"=>"Status", "type"=>"enum", "req_type"=>"st", "width"=>"210px", "title"=>"Status", "readonly"=>false, "maxlength"=>"-1", "default"=>"", "unique"=>false, "unique_condition"=>"", "on_js_event"=>"", "source"=>"self", "view_type"=>"dropdownlist"),
  112.  "region_id" =>array("header"=>"Region", "type"=>"foreign_key","req_type"=>"ri", "width"=>"210px", "title"=>"", "readonly"=>"false", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true"),
  113.  "country_id" =>array("header"=>"Country", "type"=>"foreign_key","req_type"=>"ri", "width"=>"210px", "title"=>"", "readonly"=>"false", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true"),
  114.  "favorite_color" =>array("header"=>"Favorite Color", "type"=>"color", "req_type"=>"rt", "width"=>"210px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "view_type"=>"dropdownlist", "save_format"=>"hexcodes"),
  115.  "is_active" =>array("header"=>"Active", "type"=>"checkbox", "req_type"=>"si", "width"=>"210px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "true_value"=>1, "false_value"=>0),
  116.  "rating" =>array("header"=>"Rating", "type"=>"textbox", "width"=>"100px", "req_type"=>"ri", "maxlength"=>"3", "title"=>"Rating"),
  117.  
  118.  "email" =>array("header"=>"Email", "type"=>"textbox", "req_type"=>"re", "width"=>"210px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>false, "unique_condition"=>"", "visible"=>"true", "on_js_event"=>""),
  119.  "validator_email" =>array("header"=>"Re-enter Email", "type"=>"validator", "req_type"=>"sv", "width"=>"210px", "title"=>"Confirm Email", "readonly"=>"false", "maxlength"=>"50", "default"=>"", "visible"=>"true", "on_js_event"=>"", "for_field"=>"email", "validation_type"=>"email"),
  120.  "password" =>array("header"=>"Password", "type"=>"textbox", "req_type"=>"rp", "width"=>"210px", "title"=>"", "readonly"=>"false", "maxlength"=>"25", "default"=>"", "unique"=>false, "unique_condition"=>"", "visible"=>"true", "on_js_event"=>""),
  121.  "validator_password" =>array("header"=>"Please re-enter your password", "type"=>"validator", "req_type"=>"sv", "width"=>"210px", "title"=>"Confirm Password", "readonly"=>"false", "maxlength"=>"50", "default"=>"", "visible"=>"true", "on_js_event"=>"", "for_field"=>"password", "validation_type"=>"password"),
  122. );
  123. $dgrid->SetColumnsInEditMode($em_columns);
  124. ##  *** set foreign keys for add/edit/details modes (if there are linked tables)
  125. $foreign_keys = array(
  126.  "region_id" =>array("table"=>"demo_regions", "field_key"=>"id", "field_name"=>"name", "view_type"=>"dropdownlist", "elements_alignment"=>"horizontal", "condition"=>"", "order_by_field"=>"", "order_type"=>"ASC", "on_js_event"=>"onchange='_dgFormAction(\"\", \"\", \"".$unique_prefix."\", \"".$dgrid->HTTP_URL."\", \"".$_SERVER['QUERY_STRING']."\", \"".$postback."\", \"".$mode."\")'", "show_count"=>"true"),
  127.  "country_id"=>array("table"=>"demo_countries", "field_key"=>"id", "field_name"=>"name", "view_type"=>"dropdownlist", "elements_alignment"=>"horizontal", "condition"=>(($region_id != "") ? "region_id=".$region_id : ""), "order_by_field"=>"name", "order_type"=>"ASC", "on_js_event"=>"", "show_count"=>"true"),
  128. );
  129. $dgrid->SetForeignKeysEdit($foreign_keys);
  130.  
  131. ## +---------------------------------------------------------------------------+
  132. ## | 8. Bind the DataGrid:                                                     |
  133. ## +---------------------------------------------------------------------------+
  134. ##  *** bind the DataGrid and draw it on the screen
  135. $dgrid->Bind();        
  136.  
  137. ?>