daily pastebin goal
44%
SHARE
TWEET

Untitled

gridphp Jun 4th, 2014 25 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /**
  3.  * PHP Grid Component
  4.  *
  5.  * @author Abu Ghufran <gridphp@gmail.com> - http://www.phpgrid.org
  6.  * @version 1.5.2
  7.  * @license: see license.txt included in package
  8.  */
  9.  
  10. // include db config
  11. include_once("../../config.php");
  12.  
  13. // set up DB
  14. mysql_connect(PHPGRID_DBHOST, PHPGRID_DBUSER, PHPGRID_DBPASS);
  15. mysql_select_db(PHPGRID_DBNAME);
  16.  
  17. // include and create object
  18. include(PHPGRID_LIBPATH."inc/jqgrid_dist.php");
  19.  
  20. // master grid
  21. $grid = new jqgrid();
  22. $opt["caption"] = "Clients Data";
  23. $opt["height"] = "150";
  24. $opt["sortorder"] = "desc";
  25. // following params will enable subgrid -- by default first column (PK) of parent is passed as param 'id'
  26. $opt["detail_grid_id"] = "list2";
  27.  
  28. // extra params passed to detail grid, column name comma separated
  29. $opt["subgridparams"] = "client_id,gender,company";
  30. $opt["multiselect"] = false;
  31. $opt["export"] = array("filename"=>"my-file", "sheetname"=>"test", "format"=>"pdf");
  32. $opt["export"]["range"] = "filtered";
  33. $grid->set_options($opt);
  34. $grid->table = "clients";
  35.  
  36. $grid->set_actions(array(      
  37.                                                 "add"=>true, // allow/disallow add
  38.                                                 "edit"=>true, // allow/disallow edit
  39.                                                 "delete"=>true, // allow/disallow delete
  40.                                                 "rowactions"=>true, // show/hide row wise edit/del/save option
  41.                                                 "export"=>true, // show/hide export to excel option
  42.                                                 "autofilter" => true, // show/hide autofilter for search
  43.                                                 "search" => "advance" // show single/multi field search condition (e.g. simple or advance)
  44.                                         )
  45.                                 );
  46.                                
  47. $out_master = $grid->render("list1");
  48.  
  49. // detail grid
  50. $grid = new jqgrid();
  51.  
  52. $opt = array();
  53. $opt["sortname"] = 'id'; // by default sort grid by this field
  54. $opt["sortorder"] = "desc"; // ASC or DESC
  55. $opt["height"] = ""; // autofit height of subgrid
  56. $opt["caption"] = "Invoice Data"; // caption of grid
  57. $opt["multiselect"] = true; // allow you to multi-select through checkboxes
  58. $opt["reloadedit"] = true; // allow you to multi-select through checkboxes
  59. $opt["export"] = array("filename"=>"my-file", "sheetname"=>"test", "format"=>"pdf"); // export to excel parameters
  60. $opt["export"]["range"] = "filtered";
  61. // Check if master record is selected before detail addition
  62. $opt["add_options"]["beforeInitData"] = "function(formid){ var selr = jQuery('#list1').jqGrid('getGridParam','selrow'); if (!selr) { alert('Please select master record first'); return false; } }";
  63.  
  64. // reload master after detail update
  65. $opt["onAfterSave"] = "function(){ jQuery('#list1').trigger('reloadGrid',[{jqgrid_page:1}]); }";
  66.  
  67. $grid->set_options($opt);
  68.  
  69. $grid->set_actions(array(      
  70.                                                 "add"=>true, // allow/disallow add
  71.                                                 "edit"=>true, // allow/disallow edit
  72.                                                 "delete"=>true, // allow/disallow delete
  73.                                                 "rowactions"=>true, // show/hide row wise edit/del/save option
  74.                                                 "export"=>true, // show/hide export to excel option
  75.                                                 "autofilter" => true, // show/hide autofilter for search
  76.                                                 "search" => "advance" // show single/multi field search condition (e.g. simple or advance)
  77.                                         )
  78.                                 );
  79.  
  80.  
  81. // receive id, selected row of parent grid
  82. $id = intval($_GET["rowid"]);
  83. $gender = $_GET["gender"];
  84. $company = $_GET["company"];
  85. $cid = intval($_GET["client_id"]);
  86.  
  87. // for non-int fields as PK
  88. // $id = (empty($_GET["rowid"])?0:$_GET["rowid"]);
  89.  
  90. // and use in sql for filteration
  91. $grid->select_command = "SELECT id,client_id,invdate,amount,tax,note,total,'$company' as 'company' FROM invheader WHERE client_id = $id";
  92. // this db table will be used for add,edit,delete
  93. $grid->table = "invheader";
  94.  
  95. $col = array();
  96. $col["title"] = "Id"; // caption of column
  97. $col["name"] = "id"; // field name, must be exactly same as with SQL prefix or db field
  98. $col["width"] = "10";
  99. $cols[] = $col;
  100.  
  101. $col = array();
  102. $col["title"] = "Company"; // caption of column
  103. $col["name"] = "company"; // field name, must be exactly same as with SQL prefix or db field
  104. $col["width"] = "100";
  105. $col["editable"] = false;
  106. $col["show"] = array("list"=>true,"edit"=>true,"add"=>false,"view"=>false);
  107. $cols[] = $col;
  108.                
  109. $col = array();
  110. $col["title"] = "Client";
  111. $col["name"] = "client_id";
  112. $col["width"] = "100";
  113. $col["align"] = "left";
  114. $col["search"] = true;
  115. $col["editable"] = false;
  116. $cols[] = $col;
  117.  
  118. $col = array();
  119. $col["title"] = "Date";
  120. $col["name"] = "invdate";
  121. $col["formatter"] = "date";
  122. $col["width"] = "100";
  123. $col["search"] = true;
  124. $col["editable"] = true;
  125. $cols[] = $col;
  126.  
  127. $col = array();
  128. $col["title"] = "Amount";
  129. $col["name"] = "amount";
  130. $col["width"] = "100";
  131. $col["search"] = true;
  132. $col["editable"] = true;
  133. $cols[] = $col;
  134.  
  135. $col = array();
  136. $col["title"] = "Tax";
  137. $col["name"] = "tax";
  138. $col["width"] = "100";
  139. $col["search"] = true;
  140. $col["editable"] = true;
  141. $cols[] = $col;
  142.  
  143. $col = array();
  144. $col["title"] = "Total";
  145. $col["name"] = "total";
  146. $col["width"] = "100";
  147. $col["search"] = true;
  148. $col["editable"] = false;
  149. $cols[] = $col;
  150.  
  151. $col = array();
  152. $col["title"] = "Invoices";
  153. $col["name"] = "note";
  154. $col["width"] = "100";
  155. $col["search"] = true;
  156. $col["editable"] = true;
  157. $cols[] = $col;
  158.  
  159. $grid->set_columns($cols);
  160. $e["on_insert"] = array("add_client", null, true);
  161. $e["on_update"] = array("update_client", null, true);
  162. $grid->set_events($e);
  163.  
  164.  
  165. $grid->set_actions(array(      
  166.                                                 "add"=>true, // allow/disallow add
  167.                                                 "edit"=>true, // allow/disallow edit
  168.                                                 "delete"=>true, // allow/disallow delete
  169.                                                 "inlineadd"=>true, // allow/disallow delete
  170.                                                 "rowactions"=>true, // show/hide row wise edit/del/save option
  171.                                                 "autofilter" => true, // show/hide autofilter for search
  172.                                                 "search" => "advance" // show single/multi field search condition (e.g. simple or advance)
  173.                                         )
  174.                                 );
  175.  
  176.                        
  177. function add_client(&$data)
  178. {
  179.         $id = intval($_GET["rowid"]);
  180.         $data["params"]["client_id"] = $id;
  181.         $data["params"]["total"] = $data["params"]["amount"] + $data["params"]["tax"];
  182. }
  183.  
  184. function update_client(&$data)
  185. {
  186.         $id = intval($_GET["rowid"]);
  187.         $data["params"]["client_id"] = $id;
  188.         $data["params"]["total"] = $data["params"]["amount"] + $data["params"]["tax"];
  189. }
  190.  
  191. // generate grid output, with unique grid name as 'list1'
  192. $out_detail = $grid->render("list2");
  193. ?>
  194. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
  195. <html>
  196. <head>
  197.         <link rel="stylesheet" type="text/css" media="screen" href="../../lib/js/themes/start/jquery-ui.custom.css"></link>    
  198.         <link rel="stylesheet" type="text/css" media="screen" href="../../lib/js/jqgrid/css/ui.jqgrid.css"></link>     
  199.        
  200.         <script src="../../lib/js/jquery.min.js" type="text/javascript"></script>
  201.         <script src="../../lib/js/jqgrid/js/i18n/grid.locale-en.js" type="text/javascript"></script>
  202.         <script src="../../lib/js/jqgrid/js/jquery.jqGrid.min.js" type="text/javascript"></script>     
  203.         <script src="../../lib/js/themes/jquery-ui.custom.min.js" type="text/javascript"></script>
  204.         <script src="//cdn.jsdelivr.net/jquery.hotkeys/0.8b/jquery.hotkeys.js"></script>
  205. </head>
  206. <body>
  207.         <div style="margin:10px">
  208.         Master Detail Grid, on same page
  209.         <br>
  210.         <br>
  211.         <?php echo $out_master ?>
  212.         <br>
  213.         <br>
  214.         <?php echo $out_detail; ?>
  215.         </div>
  216.        
  217.         <script>
  218.         // insert key to add new row, tab to focus on save icon & press enter to save
  219.         $(document).bind('keyup', 'insert', function(){
  220.                   jQuery('#list2_iladd').click();
  221.                 });
  222.                
  223.         </script>      
  224. </body>
  225. </html>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top