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

    1. All modes (Add/Edit/Details/Delete/View).
    2. Customized layout in View Mode.
Show Plain Text »
  1. <?php
  2. ## +---------------------------------------------------------------------------+
  3. ## | 1. Creating & Calling:                                                    |
  4. ## +---------------------------------------------------------------------------+
  5. ##  *** define a relative (virtual) path to datagrid.class.php file
  6. ##  *** directory (relatively to the current file)
  7. ##  *** RELATIVE PATH ONLY ***
  8. define ("DATAGRID_DIR", "datagrid/");                    
  9. require_once(DATAGRID_DIR."datagrid.class.php");
  10.  
  11. ##  *** creating variables that we need for database connection    
  12. $DB_USER = "db_user";
  13. $DB_PASS = "db_password";      
  14. $DB_HOST = "db_host";      
  15. $DB_NAME = "db_name";      
  16.  
  17. $sql = "SELECT demo_cars.*, model_name as details_link FROM demo_cars";
  18. ##  *** set needed options and create a new class instance
  19. $debug_mode = false;        
  20. $messaging = true;          
  21. $unique_prefix = "cr_";    
  22. $dgrid = new DataGrid($debug_mode, $messaging, $unique_prefix);
  23.  
  24. ##  *** set data source with needed options
  25. $default_order = array("model_name"=>"ASC");
  26. $dgrid->DataSource("PDO", "mysql", $DB_HOST, $DB_NAME, $DB_USER, $DB_PASS, $sql, $default_order);              
  27.  
  28. ## +---------------------------------------------------------------------------+
  29. ## | 2. General Settings:                                                      |
  30. ## +---------------------------------------------------------------------------+
  31. ## +-- Layouts, Templates & CSS -----------------------------------------------+
  32. ##  *** set layouts: "0" - tabular(horizontal) - default, "1" - columnar(vertical), "2" - customized
  33. $layouts = array("view"=>"2", "edit"=>"1", "details"=>"1", "filter"=>"1");
  34. $dgrid->SetLayouts($layouts);
  35. /// *** $mode_template = array("header"=>"", "body"=>"", "footer"=>"");
  36. $view_template = array("header"=>"", "body"=>"", "footer"=>"");
  37.  
  38. $view_template["header"] = '<table style="margin-bottom:3px; border-collapse:collapse; border: #d0d0d0 1px solid; font:normal 12px tahoma;" align="center" width="80%" border="0" cellspacing="2" cellpadding="3" bgcolor="#f3f3f3">
  39. <tr>
  40. <td height="10" width="105px" nowrap="nowrap" align="left">[ADD]</td>
  41. <td align="left">Sort By: @model_name@ | @price_from@</td>
  42. </tr>
  43. </table>';
  44.  
  45. $view_template["body"] = '<table style="margin-bottom:3px; border-collapse:collapse; border:#d0d0d0 1px solid; font:normal 12px tahoma;" align="center" width="80%" border="0" cellspacing="2" cellpadding="3" bgcolor="#f3f3f3">
  46. <tr><td colspan="4" height="10" nowrap="nowrap" align="left"></td></tr>
  47. <tr>
  48.    <td width="105px" valign="top" align="center">
  49.        <a href="uploads/{image}" rel="lyteshow[cars]" title="">{image_thumb}</a><br />
  50.        [MULTIROW_CHECKBOX] [EDIT] | [DETAILS]
  51.    </td>
  52.    <td width="5px" nowrap="nowrap"></td>
  53.    <td valign="top">
  54.        <table width="100%" border="0" cellspacing="0" cellpadding="0">
  55.        <tr>
  56.            <td>
  57.                <table width="100%" border="0" cellspacing="0" cellpadding="0">
  58.                <tr>
  59.                    <td width="235" height="20" valign="top">[ROWS_NUMERATION]. <b>{details_link}</b></td>
  60.                    <td width="140" valign="top"><span style="color:#960000;">{price_from} - {price_to}</span></td>
  61.                    <td width="185" valign="top"><a class="x-gray_dg_a" href="#">EuroAutomobile</a></td>
  62.                </tr>
  63.                </table>
  64.            </td>
  65.        </tr>
  66.        <tr><td width="550">{description}</td></tr>
  67.        <tr><td height="3" nowrap="nowrap"></td></tr>
  68.        <tr>
  69.            <td height="20">
  70.                <table width="100%" border="0" cellspacing="0" cellpadding="0">
  71.                <tr>
  72.                    <td width="100px">Submodel(s):&nbsp;
  73.                        <a class="x-gray_dg_a" href="#" onclick=\"$("#submodels_{id}").toggle();\">[+]</a>
  74.                    </td>
  75.                    <td><div id="submodels_{id}" style="display:none;">{submodels}</div></td>
  76.                </tr>
  77.                </table>
  78.            </td>
  79.        </tr>
  80.        <tr><td height="20" style="font-style:italic">Updated:&nbsp; 20-Aug-2009</td></tr>
  81.        </table>
  82.    </td>
  83.    <td valign="top" width="150px" align="center">
  84.        N/A <br/>                
  85.        <a class="x-gray_dg_a" href="#">Rate it!</a><br/>                
  86.        <a class="x-gray_dg_a" href="#">EXPERT REVIEW</a>
  87.    </td>
  88. </tr>
  89. </table>';
  90.      
  91. $dgrid->SetTemplates($view_template,"","");
  92. ##  *** set modes for operations ("type" => "link|button|image")
  93. $modes = array(
  94.    "add"     => array("view"=>true, "edit"=>false, "type"=>"link", "show_add_button"=>"inside"),
  95.    "edit"    => array("view"=>true, "edit"=>true,  "type"=>"link", "byFieldValue"=>""),
  96.    "details" => array("view"=>true, "edit"=>false, "type"=>"link"),
  97.    "delete"  => array("view"=>true, "edit"=>false,  "type"=>"image")
  98. );
  99. $dgrid->SetModes($modes);
  100. ##  *** set CSS class for datagrid
  101. $css_class = "x-gray";
  102. $dgrid->SetCssClass($css_class);
  103. ##  *** set DataGrid caption
  104. $dg_caption = "New Cars";
  105. $dgrid->SetCaption($dg_caption);
  106. ## +-- Multirow Operations ----------------------------------------------------+
  107. ##  *** allow multirow operations
  108. $multirow_option = true;
  109. $dgrid->AllowMultirowOperations($multirow_option);
  110. $multirow_operations = array(
  111.    "edit"    => array("view"=>true),
  112.    "details" => array("view"=>true),
  113.    "delete"  => array("view"=>true),
  114. );
  115. $dgrid->SetMultirowOperations($multirow_operations);  
  116.  
  117. ## +---------------------------------------------------------------------------+
  118. ## | 8. Bind the DataGrid:                                                     |
  119. ## +---------------------------------------------------------------------------+
  120. ##  *** bind the DataGrid and draw it on the screen
  121. $dgrid->Bind();        
  122.  
  123. ?>