Advertisement
Guest User

Phpgrid - master detail mysqli

a guest
May 21st, 2015
436
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.18 KB | None | 0 0
  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. $db_conf = array();
  14. $db_conf["type"] = "mysqli";
  15. $db_conf["server"] = PHPGRID_DBHOST; // or you mysql ip
  16. $db_conf["user"] = PHPGRID_DBUSER; // username
  17. $db_conf["password"] = PHPGRID_DBPASS; // password
  18. $db_conf["database"] = PHPGRID_DBNAME; // database
  19.  
  20. // include and create object
  21. include(PHPGRID_LIBPATH."inc/jqgrid_dist.php");
  22.  
  23. // master grid
  24. $grid = new jqgrid($db_conf);
  25. $opt["caption"] = "Clients Data";
  26. $opt["height"] = "150";
  27. // following params will enable subgrid -- by default first column (PK) of parent is passed as param 'id'
  28. $opt["detail_grid_id"] = "list2";
  29.  
  30. // refresh detail grid on master edit
  31. $opt["edit_options"]["afterSubmit"] = "function(){ jQuery('#list2').trigger('reloadGrid', [{current:true}]); return [true,'']; jQuery('#list1').setSelection(jQuery('#list1').jqGrid('getGridParam','selrow')); }";
  32.  
  33. // select after add
  34. $opt["add_options"]["afterComplete"] = "function (response, postdata) { r = JSON.parse(response.responseText); $('#list1').setSelection(r.id); }";
  35.  
  36. // extra params passed to detail grid, column name comma separated
  37. $opt["subgridparams"] = "client_id,gender,company";
  38. $opt["multiselect"] = false;
  39. $opt["export"] = array("filename"=>"my-file", "sheetname"=>"test", "format"=>"pdf");
  40. $opt["export"]["range"] = "filtered";
  41. $grid->set_options($opt);
  42. $grid->table = "clients";
  43.  
  44. $grid->set_actions(array(
  45. "add"=>true, // allow/disallow add
  46. "edit"=>true, // allow/disallow edit
  47. "delete"=>false, // allow/disallow delete
  48. "rowactions"=>true, // show/hide row wise edit/del/save option
  49. "export"=>true, // show/hide export to excel option
  50. "autofilter" => true, // show/hide autofilter for search
  51. "search" => "advance" // show single/multi field search condition (e.g. simple or advance)
  52. )
  53. );
  54.  
  55. $out_master = $grid->render("list1");
  56.  
  57. // detail grid
  58. $grid = new jqgrid($db_conf);
  59.  
  60. $opt = array();
  61. $opt["sortname"] = 'id'; // by default sort grid by this field
  62. $opt["sortorder"] = "desc"; // ASC or DESC
  63. $opt["height"] = ""; // autofit height of subgrid
  64. $opt["caption"] = "Invoice Data"; // caption of grid
  65. $opt["multiselect"] = true; // allow you to multi-select through checkboxes
  66. $opt["reloadedit"] = true; // allow you to multi-select through checkboxes
  67. $opt["export"] = array("filename"=>"my-file", "sheetname"=>"test", "format"=>"pdf"); // export to excel parameters
  68. $opt["export"]["range"] = "filtered";
  69.  
  70. // Check if master record is selected before detail addition
  71. // $opt["add_options"]["beforeInitData"] = "function(formid){ var selr = jQuery('#list1').jqGrid('getGridParam','selrow'); if (!selr) { alert('Please select master record first'); return false; } }";
  72.  
  73. // reload master after detail update
  74. $opt["onAfterSave"] = "function(){ var selr = jQuery('#list1').jqGrid('getGridParam','selrow'); jQuery('#list1').trigger('reloadGrid',[{jqgrid_page:1}]); setTimeout( function(){jQuery('#list1').setSelection(selr,true);},500 ); }";
  75.  
  76. $grid->set_options($opt);
  77.  
  78. // receive id, selected row of parent grid
  79. $id = intval($_GET["rowid"]);
  80. $gender = $_GET["gender"];
  81. $company = $_GET["company"];
  82. $cid = intval($_GET["client_id"]);
  83.  
  84. // for non-int fields as PK
  85. // $id = (empty($_GET["rowid"])?0:$_GET["rowid"]);
  86.  
  87. // and use in sql for filteration
  88. $grid->select_command = "SELECT id,client_id,invdate,amount,tax,note,total,'$company' as 'company' FROM invheader WHERE client_id = $id";
  89. // this db table will be used for add,edit,delete
  90. $grid->table = "invheader";
  91.  
  92. $col = array();
  93. $col["title"] = "Id"; // caption of column
  94. $col["name"] = "id"; // field name, must be exactly same as with SQL prefix or db field
  95. $col["width"] = "10";
  96. $cols[] = $col;
  97.  
  98. $col = array();
  99. $col["title"] = "Company"; // caption of column
  100. $col["name"] = "company"; // field name, must be exactly same as with SQL prefix or db field
  101. $col["width"] = "100";
  102. $col["editable"] = false;
  103. $col["show"] = array("list"=>true,"edit"=>true,"add"=>false,"view"=>false);
  104. $cols[] = $col;
  105.  
  106. $col = array();
  107. $col["title"] = "Client";
  108. $col["name"] = "client_id";
  109. $col["width"] = "100";
  110. $col["align"] = "left";
  111. $col["search"] = true;
  112. $col["editable"] = false;
  113. $cols[] = $col;
  114.  
  115. $col = array();
  116. $col["title"] = "Date";
  117. $col["name"] = "invdate";
  118. $col["formatter"] = "date";
  119. $col["width"] = "100";
  120. $col["search"] = true;
  121. $col["editable"] = true;
  122. $cols[] = $col;
  123.  
  124. $col = array();
  125. $col["title"] = "Amount";
  126. $col["name"] = "amount";
  127. $col["width"] = "100";
  128. $col["search"] = true;
  129. $col["editable"] = true;
  130. $cols[] = $col;
  131.  
  132. $col = array();
  133. $col["title"] = "Tax";
  134. $col["name"] = "tax";
  135. $col["width"] = "100";
  136. $col["search"] = true;
  137. $col["editable"] = true;
  138. $cols[] = $col;
  139.  
  140. $col = array();
  141. $col["title"] = "Total";
  142. $col["name"] = "total";
  143. $col["width"] = "100";
  144. $col["search"] = true;
  145. $col["editable"] = false;
  146. $cols[] = $col;
  147.  
  148. $col = array();
  149. $col["title"] = "Invoices";
  150. $col["name"] = "note";
  151. $col["width"] = "100";
  152. $col["search"] = true;
  153. $col["editable"] = true;
  154. $cols[] = $col;
  155.  
  156. $grid->set_columns($cols);
  157. $e["on_insert"] = array("add_client", null, true);
  158. $e["on_update"] = array("update_client", null, true);
  159. $grid->set_events($e);
  160.  
  161. $grid->set_actions(array(
  162. "add"=>true, // allow/disallow add
  163. "edit"=>true, // allow/disallow edit
  164. "delete"=>true, // allow/disallow delete
  165. "rowactions"=>true, // show/hide row wise edit/del/save option
  166. "autofilter" => true, // show/hide autofilter for search
  167. "search" => "advance" // show single/multi field search condition (e.g. simple or advance)
  168. )
  169. );
  170.  
  171.  
  172. function add_client(&$data)
  173. {
  174. $id = intval($_GET["rowid"]);
  175. $data["params"]["client_id"] = $id;
  176. $data["params"]["total"] = $data["params"]["amount"] + $data["params"]["tax"];
  177. }
  178.  
  179. function update_client(&$data)
  180. {
  181. $id = intval($_GET["rowid"]);
  182. $data["params"]["client_id"] = $id;
  183. $data["params"]["total"] = $data["params"]["amount"] + $data["params"]["tax"];
  184. }
  185.  
  186. // generate grid output, with unique grid name as 'list1'
  187. $out_detail = $grid->render("list2");
  188. ?>
  189. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
  190. <html>
  191. <head>
  192. <link rel="stylesheet" type="text/css" media="screen" href="../../lib/js/themes/redmond/jquery-ui.custom.css"></link>
  193. <link rel="stylesheet" type="text/css" media="screen" href="../../lib/js/jqgrid/css/ui.jqgrid.css"></link>
  194.  
  195. <script src="../../lib/js/jquery.min.js" type="text/javascript"></script>
  196. <script src="../../lib/js/jqgrid/js/i18n/grid.locale-en.js" type="text/javascript"></script>
  197. <script src="../../lib/js/jqgrid/js/jquery.jqGrid.min.js" type="text/javascript"></script>
  198. <script src="../../lib/js/themes/jquery-ui.custom.min.js" type="text/javascript"></script>
  199. <script src="//cdn.jsdelivr.net/jquery.hotkeys/0.8b/jquery.hotkeys.js"></script>
  200. </head>
  201. <body>
  202. <div style="margin:10px">
  203. Master Detail Grid, on same page
  204. <br>
  205. <br>
  206. <?php echo $out_master ?>
  207. <br>
  208. <br>
  209. <?php echo $out_detail; ?>
  210. </div>
  211. </body>
  212. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement