Sample 2-2. Advanced DataGrid code.

    1. All modes (Add/Edit/Details/Delete/View).
    2. All features.
    3. Inline editing.
    4. AJAX autocomplete feature for filtering.
Show Plain Text »
  1. <?php
  2.  
  3. ## +---------------------------------------------------------------------------+
  4. ## | 1. Creating & Calling:                                                    |
  5. ## +---------------------------------------------------------------------------+
  6. ##  *** define a relative (virtual) path to datagrid.class.php file
  7. ##  *** directory (relatively to the current file)
  8. ##  *** RELATIVE PATH ONLY ***
  9. define ("DATAGRID_DIR", "datagrid/");                    
  10. require_once(DATAGRID_DIR."datagrid.class.php");
  11.  
  12. ##  *** creating variables that we need for database connection    
  13. $DB_USER = "db_user";
  14. $DB_PASS = "db_password";      
  15. $DB_HOST = "db_host";      
  16. $DB_NAME = "db_name";      
  17.  
  18. ##  *** put a primary key on the first place
  19. $sql=" SELECT
  20.    demo_presidents.id,
  21.    demo_presidents.region_id,
  22.    demo_presidents.country_id,
  23.    demo_presidents.name,
  24.    demo_presidents.birth_date,
  25.    demo_presidents.status,
  26.    demo_presidents.rating,
  27.    demo_countries.name as country_name
  28. FROM demo_presidents
  29.    INNER JOIN demo_countries ON demo_presidents.country_id=demo_countries.id ";
  30.  
  31. ##  *** set needed options and create a new class instance
  32. $debug_mode = false;        /* display SQL statements while processing */    
  33. $messaging = true;          /* display system messages on a screen */
  34. $unique_prefix = "prs_";    /* prevent overlays - must be started with a letter */
  35. $dgrid = new DataGrid($debug_mode, $messaging, $unique_prefix);
  36.  
  37. ##  *** set data source with needed options
  38. $default_order = array("rating"=>"ASC");
  39. $dgrid->DataSource("PDO", "mysql", $DB_HOST, $DB_NAME, $DB_USER, $DB_PASS, $sql, $default_order);            
  40. $dgrid->isDemo = true;
  41. $dgrid->navigationBar = "";
  42.  
  43. ## +---------------------------------------------------------------------------+
  44. ## | 2. General Settings:                                                      |
  45. ## +---------------------------------------------------------------------------+
  46. ##  *** allow multirow operations
  47. $multirow_option = true;
  48. $dgrid->AllowMultirowOperations($multirow_option);
  49. $multirow_operations = array(
  50.     "edit"  => array("view"=>false),
  51.     "delete"  => array("view"=>true),
  52.     "details" => array("view"=>true),
  53. );
  54. $dgrid->SetMultirowOperations($multirow_operations);  
  55. ##  *** set DataGrid caption
  56. $dg_caption = "Presidents";
  57. $dgrid->SetCaption($dg_caption);
  58.  
  59. ## +---------------------------------------------------------------------------+
  60. ## | 3. Printing & Exporting Settings:                                         |
  61. ## +---------------------------------------------------------------------------+
  62. ##  *** set printing option: true(default) or false
  63. $printing_option = false;
  64. $dgrid->AllowPrinting($printing_option);
  65.  
  66. ## +---------------------------------------------------------------------------+
  67. ## | 4. Sorting & Paging Settings:                                             |
  68. ## +---------------------------------------------------------------------------+
  69. ##  *** set paging option: true(default) or false
  70. $paging_option = true;
  71. $rows_numeration = false;
  72. $numeration_sign = "N #";
  73. $dropdown_paging = true;
  74. $dgrid->AllowPaging($paging_option, $rows_numeration, $numeration_sign, $dropdown_paging);
  75. ##  *** set paging settings
  76. $bottom_paging = array("results"=>true, "results_align"=>"left", "pages"=>true, "pages_align"=>"center", "page_size"=>true, "page_size_align"=>"right");
  77. $top_paging = array();
  78. $pages_array = array("5"=>"5", "10"=>"10", "25"=>"25", "50"=>"50", "100"=>"100", "250"=>"250", "500"=>"500", "1000"=>"1000");
  79. $default_page_size = 10;
  80. $paging_arrows = array("first"=>"|&lt;&lt;", "previous"=>"&lt;&lt;", "next"=>"&gt;&gt;", "last"=>"&gt;&gt;|");
  81. $dgrid->SetPagingSettings($bottom_paging, $top_paging, $pages_array, $default_page_size, $paging_arrows);
  82.  
  83. ## +---------------------------------------------------------------------------+
  84. ## | 5. Filter Settings:                                                       |
  85. ## +---------------------------------------------------------------------------+
  86. ##  *** set filtering option: true or false(default)
  87. $filtering_option = true;
  88. $show_search_type = true;
  89. $dgrid->AllowFiltering($filtering_option, $show_search_type);
  90. ##  *** set additional filtering settings
  91. ##  *** tips: use "," (comma) if you want to make search by some words, for ex.: hello, bye, hi
  92. $filtering_fields = array(
  93.     "<b>Country</b> <br />(start typing to see autocomplete in work)"=>array("type"=>"textbox", "autocomplete"=>"true", "handler"=>"autosuggest_countries.php", "maxresults"=>"12", "shownoresults"=>"true", "table"=>"demo_countries", "field"=>"name", "show_operator"=>"false", "default_operator"=>"like%", "case_sensitive"=>"false", "comparison_type"=>"string", "width"=>"", "on_js_event"=>""),
  94. );
  95. $dgrid->SetFieldsFiltering($filtering_fields);
  96.  
  97. ## +---------------------------------------------------------------------------+
  98. ## | 6. View Mode Settings:                                                    |
  99. ## +---------------------------------------------------------------------------+
  100. ##  *** set view mode table properties
  101. $vm_table_properties = array("width"=>"75%");
  102. $dgrid->SetViewModeTableProperties($vm_table_properties);  
  103. ##  *** set columns in view mode
  104. $vm_columns = array(
  105.     "name" => array("header"=>"Name", "type"=>"label", "header_tooltip"=>"Name of President for specific country", "header_tooltip_type"=>"floating", "align"=>"left", "wrap"=>"wrap", "text_length"=>"20"),
  106.     "birth_date" => array("header"=>"Birth Date", "type"=>"label", "align"=>"center", "wrap"=>"nowrap", "text_length"=>"-1"),
  107.     "status" => array("header"=>"Status", "type"=>"label", "align"=>"center", "wrap"=>"nowrap", "text_length"=>"30"),
  108.     "rating" => array("header"=>"Rating", "type"=>"label", "align"=>"center", "wrap"=>"nowrap", "text_length"=>"-1", "movable"=>"true"),
  109.     "country_name" => array("header"=>"Country", "type"=>"label", "align"=>"center", "wrap"=>"nowrap", "text_length"=>"30", "sortable"=>"false")
  110. );
  111. $dgrid->SetColumnsInViewMode($vm_columns);
  112.  
  113. ## +---------------------------------------------------------------------------+
  114. ## | 7. Add/Edit/Details Mode Settings:                                        |
  115. ## +---------------------------------------------------------------------------+
  116. ##  *** set add/edit mode table properties
  117. $em_table_properties = array("width"=>"75%");
  118. $dgrid->SetEditModeTableProperties($em_table_properties);
  119. ##  *** set details mode table properties
  120. $dm_table_properties = array("width"=>"60%");
  121. $dgrid->SetDetailsModeTableProperties($dm_table_properties);
  122. ##  ***  set settings for add/edit/details modes
  123. $table_name  = "demo_presidents";
  124. $primary_key = "id";
  125. $condition   = "";
  126. $dgrid->SetTableEdit($table_name, $primary_key, $condition);
  127. ##  *** set columns in edit mode    
  128. $em_columns = array(
  129.     "name" =>array("header"=>"Name", "type"=>"textbox", "width"=>"140px", "req_type"=>"rt"),
  130.     "birth_date" =>array("header"=>"Birth Date", "type"=>"date", "req_type"=>"rt", "width"=>"80px", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "calendar_type"=>"floating"),
  131.     "status" =>array("header"=>"Status", "type"=>"enum", "req_type"=>"st", "width"=>"210px", "readonly"=>false, "maxlength"=>"-1", "default"=>"", "unique"=>false, "unique_condition"=>"", "on_js_event"=>"", "source"=>"self", "view_type"=>"dropdownlist"),
  132.     "rating" =>array("header"=>"Rating", "type"=>"textbox", "width"=>"100px", "req_type"=>"ri", "maxlength"=>"3"),
  133.     "country_id" =>array("header"=>"Country", "type"=>"textbox", "width"=>"160px", "req_type"=>"ri", "readonly"=>true),      
  134. );
  135. $dgrid->SetColumnsInEditMode($em_columns);
  136. ##  *** set foreign keys for add/edit/details modes (if there are linked tables)
  137. $foreign_keys = array(
  138.     "country_id"=>array("table"=>"demo_countries", "field_key"=>"id", "field_name"=>"name", "view_type"=>"dropdownlist", "condition"=>"", "order_by_field"=>"", "order_type"=>"ASC", "on_js_event"=>""),
  139. );
  140. $dgrid->SetForeignKeysEdit($foreign_keys);
  141.  
  142. ## +---------------------------------------------------------------------------+
  143. ## | 8. Bind the DataGrid:                                                     |
  144. ## +---------------------------------------------------------------------------+
  145. ##  *** bind the DataGrid and draw it on the screen
  146. $dgrid->Bind();        
  147.  
  148. ?>