Sample 2-1. Simple DataGrid code.

    1. All modes (Add/Edit/Details/Delete/View).
    2. All features.
    3. Many types of fields.
    4. Image magnifying feature.
    5. WYSIWYG editor.
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_products.id,
  21.    demo_products.name,
  22.    demo_products.short_description,
  23.    demo_products.long_description,
  24.    demo_products.image_thumb,
  25.    demo_products.image_big,
  26.    demo_products.price,
  27.    demo_products.available_from,
  28.    demo_products.statistics,
  29.    demo_suppliers.name as supplier_name
  30. FROM demo_products
  31.    LEFT OUTER JOIN demo_suppliers ON demo_products.supplier_id = demo_suppliers.id ";
  32.  
  33. ##  *** set needed options and create a new class instance
  34. $debug_mode = false;        /* display SQL statements while processing */    
  35. $messaging = true;          /* display system messages on a screen */
  36. $unique_prefix = "prd_";    /* prevent overlays - must be started with a letter */
  37. $dgrid = new DataGrid($debug_mode, $messaging, $unique_prefix);
  38.  
  39. ##  *** set data source with needed options
  40. $default_order = array("demo_products.name"=>"ASC");
  41. $dgrid->DataSource("PDO", "mysql", $DB_HOST, $DB_NAME, $DB_USER, $DB_PASS, $sql, $default_order);            
  42.  
  43. ## +---------------------------------------------------------------------------+
  44. ## | 2. General Settings:                                                      |
  45. ## +---------------------------------------------------------------------------+
  46. ##  *** set interface language (default - English)
  47. $dg_language = "en";  
  48. $dgrid->SetInterfaceLang($dg_language);
  49. ##  *** set modes for operations ("type" => "link|button|image")
  50. $modes = array(
  51.  "add"    =>array("view"=>true, "edit"=>false, "type"=>"link", "show_add_button"=>"outside"),
  52.  "edit"   =>array("view"=>true, "edit"=>true,  "type"=>"link", "byFieldValue"=>""),
  53.  "details" =>array("view"=>true, "edit"=>false, "type"=>"link"),
  54.  "delete"  =>array("view"=>true, "edit"=>true,  "type"=>"image")
  55. );
  56. $dgrid->SetModes($modes);
  57. ##  *** allow multirow operations
  58. $multirow_option = true;
  59. $dgrid->AllowMultirowOperations($multirow_option);
  60. $multirow_operations = array("edit"  => array("view"=>false), "delete"  => array("view"=>true), "details" => array("view"=>true));
  61. $dgrid->SetMultirowOperations($multirow_operations);  
  62. ##  *** set CSS class for datagrid
  63. $css_class = "default";
  64. $dgrid->SetCssClass($css_class);
  65. ##  *** set other datagrid/s unique prefixes (if you use few datagrids on one page)
  66. ##  *** set DataGrid caption
  67. $dg_caption = "List of Mobile Phones";
  68. $dgrid->SetCaption($dg_caption);
  69.  
  70. ## +---------------------------------------------------------------------------+
  71. ## | 4. Sorting & Paging Settings:                                             |
  72. ## +---------------------------------------------------------------------------+
  73. ##  *** set paging option: true(default) or false
  74. $paging_option = true;
  75. $rows_numeration = false;
  76. $numeration_sign = "N #";
  77. $dropdown_paging = true;
  78. $dgrid->AllowPaging($paging_option, $rows_numeration, $numeration_sign, $dropdown_paging);
  79. ##  *** set paging settings
  80. $bottom_paging = array("results"=>true, "results_align"=>"left", "pages"=>true, "pages_align"=>"center", "page_size"=>true, "page_size_align"=>"right");
  81. $top_paging = array();
  82. $pages_array = array("10"=>"10", "25"=>"25", "50"=>"50", "100"=>"100", "250"=>"250", "500"=>"500", "1000"=>"1000");
  83. $default_page_size = 10;
  84. $paging_arrows = array("first"=>"|&lt;&lt;", "previous"=>"&lt;&lt;", "next"=>"&gt;&gt;", "last"=>"&gt;&gt;|");
  85. $dgrid->setPagingSettings($bottom_paging, $top_paging, $pages_array, $default_page_size, $paging_arrows);
  86.  
  87. ## +---------------------------------------------------------------------------+
  88. ## | 5. Filter Settings:                                                       |
  89. ## +---------------------------------------------------------------------------+
  90. ##  *** set filtering option: true or false(default)
  91. $filtering_option = true;
  92. $show_search_type = true;
  93. $dgrid->AllowFiltering($filtering_option, $show_search_type);
  94. ##  *** set additional filtering settings
  95. $filtering_fields = array(
  96.  "Supplier" => array("type"=>"enum", "view_type"=>"dropdownlist",  "order"=>"ASC", "table"=>"demo_suppliers", "field"=>"name", "source"=>"self", "show"=>"", "condition"=>"", "show_operator"=>"false", "default_operator"=>"=", "case_sensitive"=>"false", "comparison_type"=>"string", "width"=>"", "multiple"=>"false", "multiple_size"=>"4", "on_js_event"=>""),
  97.  "From"     => array("type"=>"calendar", "calendar_type"=>"floating", "table"=>"demo_products", "field"=>"available_from", "field_type"=>"from", "show_operator"=>"false", "default_operator"=>">=", "case_sensitive"=>"false", "comparison_type"=>"string", "width"=>"140px", "on_js_event"=>""),
  98.  "To"       => array("type"=>"calendar", "calendar_type"=>"floating", "table"=>"demo_products", "field"=>"available_from", "field_type"=>"to", "show_operator"=>"false", "default_operator"=>"<=", "case_sensitive"=>"false", "comparison_type"=>"string", "width"=>"140px", "on_js_event"=>""),
  99. );
  100. $dgrid->SetFieldsFiltering($filtering_fields);
  101.  
  102. ## +---------------------------------------------------------------------------+
  103. ## | 6. View Mode Settings:                                                    |
  104. ## +---------------------------------------------------------------------------+
  105. ##  *** set view mode table properties
  106. $vm_table_properties = array("width"=>"95%");
  107. $dgrid->SetViewModeTableProperties($vm_table_properties);  
  108. ##  *** set columns in view mode
  109. $vm_columns = array(
  110.  "name"              => array("header"=>"Name", "header_tooltip"=>"Model and Model Type", "header_tooltip_type"=>"simple", "type"=>"linktoedit", "align"=>"left", "width"=>"", "wrap"=>"nowrap", "text_length"=>"-1", "tooltip"=>"false", "case"=>"normal", "summarize"=>"false", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),
  111.  "image_thumb"       => array("header"=>"Image", "type"=>"image", "align"=>"center", "width"=>"58x", "wrap"=>"nowrap", "text_length"=>"-1", "tooltip"=>"false", "summarize"=>"false", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>"", "target_path"=>"uploads/", "default"=>"default_image.ext", "image_width"=>"50px", "image_height"=>"30px", "magnify"=>"true", "magnify_type"=>"magnifier", "magnify_power"=>"3"),
  112.  "supplier_name"     => array("header"=>"Supplier", "header_tooltip"=>"Supplier Name", "type"=>"linktoview", "align"=>"center", "width"=>"", "wrap"=>"nowrap", "text_length"=>"-1", "tooltip"=>"false", "case"=>"normal", "summarize"=>"false", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),
  113.  "short_description" => array("header"=>"Short Description", "type"=>"label", "align"=>"left", "width"=>"120px", "wrap"=>"nowrap", "text_length"=>"12", "tooltip"=>"true", "tooltip_type"=>"simple", "case"=>"normal", "summarize"=>"false", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),
  114.  "price"             => array("header"=>"Price", "type"=>"money", "align"=>"right", "width"=>"", "wrap"=>"nowrap", "text_length"=>"-1", "tooltip"=>"false", "summarize"=>true, "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>"", "sign"=>"$", "decimal_places"=>"2", "dec_separator"=>".", "thousands_separator"=>","),
  115.  "available_from"    => array("header"=>"Available From", "type"=>"label", "align"=>"center", "width"=>"", "wrap"=>"nowrap", "text_length"=>"-1", "tooltip"=>"false", "summarize"=>"false", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>"", "on_item_created"=>"my_format_date"),
  116.  "statistics"        => array("header"=>"Sales %", "type"=>"barchart", "align"=>"center", "width"=>"", "wrap"=>"nowrap", "text_length"=>"-1", "tooltip"=>"false", "summarize"=>"false", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>"", "field"=>"statistics", "maximum_value"=>"100"),
  117. );
  118. $dgrid->SetColumnsInViewMode($vm_columns);
  119.  
  120. ## +---------------------------------------------------------------------------+
  121. ## | 7. Add/Edit/Details Mode Settings:                                        |
  122. ## +---------------------------------------------------------------------------+
  123. ##  *** set add/edit mode table properties
  124. $em_table_properties = array("width"=>"70%");
  125. $dgrid->SetEditModeTableProperties($em_table_properties);
  126. ##  *** set details mode table properties
  127. $dm_table_properties = array("width"=>"70%");
  128. $dgrid->SetDetailsModeTableProperties($dm_table_properties);
  129. ##  ***  set settings for add/edit/details modes
  130. $table_name  = "demo_products";
  131. $primary_key = "id";
  132. $condition   = "";
  133. $dgrid->SetTableEdit($table_name, $primary_key, $condition);
  134. ##  *** set columns in edit mode  
  135. $fill_from_array_sales = array();
  136. for($i=1; $i<100; $i++){ $fill_from_array_sales[$i] = $i; }
  137. $fill_from_array_chargers = array("linear_regulator"=>"Linear Regulator", "switch_mode"=>"Switch Mode", "shunt_regulators"=>"Shunt Regulators", "pulsed_charging"=>"Pulsed Charging");
  138.  
  139. $em_columns = array(
  140.  "delimiter_0"   =>array("inner_html"=>"Textbox and dropdown list fields: They are basic fields for each DataGrid. Supplier is an example of Foreign Key with possibility to select value from dropdown list."),
  141.  "name"          =>array("header"=>"Product Name", "type"=>"textbox", "req_type"=>"rt", "width"=>"210px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>""),
  142.  "supplier_id"   =>array("header"=>"Supplier", "type"=>"foreign_key","req_type"=>"ri", "width"=>"210px", "title"=>"", "readonly"=>"false", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true"),
  143.  "charger_types" =>array("header"=>"Charger Types", "type"=>"enum", "req_type"=>"st", "width"=>"210px", "title"=>"", "readonly"=>false, "maxlength"=>"-1", "default"=>"", "unique"=>false, "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "source"=>$fill_from_array_chargers, "view_type"=>"checkbox", "elements_alignment"=>"horizontal|vertical", "multiple"=>true, "multiple_size"=>"5"),
  144.  
  145.  "delimiter_1"   =>array("inner_html"=>"Image fields: These are image fields. You can click on each picture to enlarge. Uploading or deleting of images is blocked in Demo version."),
  146.  "image_thumb"   =>array("header"=>"Thumbnail", "type"=>"image", "req_type"=>"st", "width"=>"220px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "target_path"=>"uploads/", "max_file_size"=>"200K", "image_width"=>"120px", "image_height"=>"90px", "magnify"=>"true", "magnify_type"=>"lightbox", "file_name"=>"", "host"=>"local"),
  147.  "image_big"     =>array("header"=>"Big Image", "type"=>"image", "req_type"=>"st", "width"=>"220px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "target_path"=>"uploads/", "max_file_size"=>"500K", "image_width"=>"240px", "image_height"=>"180px", "magnify"=>"true", "magnify_type"=>"lightbox", "file_name"=>"", "host"=>"local"),
  148.  
  149.  "delimiter_2"   =>array("inner_html"=>"Teaxtarea fields: There are 2 types of textarea fields. First is a 'simple', with draggable bar at the bottom to resizing. Second - is a WYSIWYG Editor. In WYSIWYG Editor you can easy edit and format the text. WYSIWYG implies a user interface that allows the user to view something very similar to the end result while the document is being created."),
  150.  "short_description"  =>array("header"=>"Short Description", "type"=>"textarea", "req_type"=>"rt", "width"=>"500px", "title"=>"", "readonly"=>"false", "maxlength"=>"255", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "edit_type"=>"simple", "resizable"=>"true", "rows"=>"5", "cols"=>"50"),
  151.  "long_description"   =>array("header"=>"Long Description", "type"=>"textarea", "req_type"=>"rt", "width"=>"500px", "title"=>"", "readonly"=>"false", "maxlength"=>"1024", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "edit_type"=>"wysiwyg", "resizable"=>"false", "rows"=>"7", "cols"=>"50"),
  152.  
  153.  "delimiter_3"   =>array("inner_html"=>"Date/Time fields: There are 2 types of date/time fields: floating calendar and dropdown list calendar."),
  154.  "available_from"  =>array("header"=>"Available From", "type"=>"datetime", "req_type"=>"rt", "width"=>"187px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "calendar_type"=>"dropdownlist"),
  155.  "last_updated"  =>array("header"=>"Last Updated", "type"=>"datetime", "req_type"=>"rt", "width"=>"187px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "calendar_type"=>"floating"),
  156.  
  157.  "delimiter_4"   =>array("inner_html"=>"Other fields: Money, checkbox, predefined dropdown list (enum) and radio buttons."),
  158.  "price"         =>array("header"=>"Price", "type"=>"textbox", "req_type"=>"rf", "width"=>"90px", "title"=>"", "readonly"=>"false", "maxlength"=>"12", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "pre_addition"=>"$"),
  159.  "statistics"    =>array("header"=>"Sales in %", "type"=>"enum", "req_type"=>"rf", "width"=>"", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "source"=>$fill_from_array_sales, "view_type"=>"dropdownlist", "elements_alignment"=>"horizontal|vertical", "multiple"=>"false", "multiple_size"=>"4"),
  160.  "subscribe"     =>array("header"=>"Subscribe for news?", "type"=>"checkbox",   "req_type"=>"st", "width"=>"210px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "true_value"=>1, "false_value"=>0),
  161.  "is_featured"   =>array("header"=>"Is Featured", "type"=>"enum", "req_type"=>"rr", "width"=>"210px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "source"=>"self", "view_type"=>"radiobutton", "elements_alignment"=>"horizontal", "multiple"=>"false", "multiple_size"=>"4"),
  162.  
  163.  "date_added"    =>array("header"=>"Date Added", "type"=>"hidden",     "req_type"=>"st", "default"=>"NOW", "value"=>"", "unique"=>"false", "visible"=>"true"),
  164. );
  165. $dgrid->SetColumnsInEditMode($em_columns);
  166. ##  *** set foreign keys for add/edit/details modes (if there are linked tables)
  167. $foreign_keys = array(
  168.  "supplier_id"=>array("table"=>"demo_suppliers", "field_key"=>"id", "field_name"=>"name", "view_type"=>"dropdownlist", "elements_alignment"=>"horizontal|vertical", "condition"=>"", "order_by_field"=>"name", "order_type"=>"ASC", "on_js_event"=>""),
  169. );
  170. $dgrid->SetForeignKeysEdit($foreign_keys);
  171.  
  172. ## +---------------------------------------------------------------------------+
  173. ## | 8. Bind the DataGrid:                                                     |
  174. ## +---------------------------------------------------------------------------+
  175. ##  *** bind the DataGrid and draw it on the screen
  176. $dgrid->Bind();        
  177.  
  178. ?>