Guest User

Untitled

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