Guest User

Untitled

a guest
Jan 8th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.51 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. // include and create object
  14. include(PHPGRID_LIBPATH."inc/jqgrid_dist.php");
  15.  
  16. $g = new jqgrid();
  17.  
  18. $opt["rowNum"] = 10; // by default 20
  19. $opt["sortname"] = 'id'; // by default sort grid by this field
  20. $opt["sortorder"] = "asc"; // ASC or DESC
  21. $opt["caption"] = "Footer Row"; // caption of grid
  22. $opt["autowidth"] = true; // expand grid to screen width
  23. $opt["multiselect"] = true; // allow you to multi-select through checkboxes
  24. $opt["footerrow"] = true;
  25. $opt["reloadedit"] = true;
  26.  
  27. // use html renderer
  28. $opt["export"]["render_type"] = "html";
  29.  
  30. $opt["onSelectAll"] = "function(id,status){ grid_onselect(); }";
  31. $g->set_options($opt);
  32.  
  33. $g->set_actions(array(
  34. "add"=>true, // allow/disallow add
  35. "edit"=>true, // allow/disallow edit
  36. "delete"=>true, // allow/disallow delete
  37. "rowactions"=>true, // show/hide row wise edit/del/save option
  38. "search" => "advance", // show single/multi field search condition (e.g. simple or advance)
  39. "autofilter" => true,
  40. "export_pdf" => true
  41. )
  42. );
  43.  
  44. // you can provide custom SQL query to display data
  45. $g->select_command = "SELECT id,invdate,total,(SELECT sum(total) from invheader) AS table_total FROM invheader";
  46.  
  47. // this db table will be used for add,edit,delete
  48. $g->table = "invheader";
  49.  
  50. $cols = array();
  51.  
  52. $col = array();
  53. $col["title"] = "id";
  54. $col["name"] = "id";
  55. $col["width"] = "100";
  56. $col["editable"] = true;
  57. $cols[] = $col;
  58.  
  59. $col = array();
  60. $col["title"] = "invdate";
  61. $col["name"] = "invdate";
  62. $col["width"] = "100";
  63. $col["editable"] = true;
  64. $cols[] = $col;
  65.  
  66. $col = array();
  67. $col["title"] = "total";
  68. $col["name"] = "total";
  69. $col["width"] = "100";
  70. $col["editable"] = true;
  71. $col["formatter"] = "function(cellvalue, options, rowObject){ return formatPrice(cellvalue, options, rowObject);}";
  72. $col["unformat"] = "function(cellvalue, options, rowObject){ return parseFloat(cellvalue.replace('$','').trim());}";
  73. $cols[] = $col;
  74.  
  75. // virtual column for running total
  76. $col = array();
  77. $col["title"] = "running_total";
  78. $col["name"] = "running_total";
  79. $col["width"] = "100";
  80. $col["hidden"] = true;
  81. $cols[] = $col;
  82.  
  83. // virtual column for grand total
  84. $col = array();
  85. $col["title"] = "table_total";
  86. $col["name"] = "table_total";
  87. $col["width"] = "100";
  88. $col["hidden"] = true;
  89. $cols[] = $col;
  90.  
  91. // pass the cooked columns to grid
  92. $g->set_columns($cols);
  93.  
  94. // running total calculation
  95. $e = array();
  96. $e["on_data_display"] = array("pre_render","",true);
  97. $e["on_render_pdf"] = array("render_pdf", null, true);
  98.  
  99. $e["js_on_select_row"] = "grid_onselect";
  100. $e["js_on_load_complete"] = "grid_onload";
  101.  
  102. $g->set_events($e);
  103.  
  104. function pre_render($data)
  105. {
  106. $rows = $_GET["jqgrid_page"] * $_GET["rows"];
  107. $sidx = $_GET['sidx']; // get index row - i.e. user click to sort
  108. $sord = $_GET['sord']; // get the direction
  109.  
  110. // to where filtered data
  111. $swhere = "WHERE 1=1 ".$_SESSION["jqgrid_list1_filter"];
  112.  
  113. global $g;
  114. $result = $g->execute_query("SELECT SUM(total) as s FROM (SELECT total FROM invheader $swhere ORDER BY $sidx $sord LIMIT $rows) AS tmp");
  115. $rs = $result->GetRows();
  116. $rs = $rs[0];
  117.  
  118. foreach($data["params"] as &$d)
  119. {
  120. $d["running_total"] = $rs["s"];
  121. }
  122. }
  123.  
  124. // custom on_export callback function
  125. function render_pdf($param)
  126. {
  127. $grid = $param["grid"];
  128. $arr = $param["data"];
  129.  
  130. $html .= "<h1>".$grid->options["export"]["heading"]."</h1>";
  131. $html .= '<table border="0" cellpadding="4" cellspacing="2">';
  132.  
  133. $i = 0;
  134. $total = 0;
  135. foreach($arr as $v)
  136. {
  137. $shade = ($i++ % 2) ? 'bgcolor="#efefef"' : '';
  138. $html .= "<tr>";
  139. foreach($v as $k=>$d)
  140. {
  141. if ($k == 'total')
  142. $total += floatval($d);
  143.  
  144. // bold header
  145. if ($i == 1)
  146. $html .= "<td bgcolor=\"lightgrey\"><strong>".ucwords($d)."</strong></td>";
  147. else
  148. $html .= "<td $shade>$d</td>";
  149. }
  150. $html .= "</tr>";
  151. }
  152.  
  153.  
  154. $html .= "<tr bgcolor=\"lightgrey\"><td></td><td></td><td align='right'><strong>Total: $total</strong></td><td></td><td></td></tr>";
  155.  
  156. $html .= "</table>";
  157.  
  158. return $html;
  159. }
  160. $GLOBALS['language']['thousandsSeparator'] = ",";
  161. $GLOBALS['language']['decimalSeparator'] = ".";
  162. // generate grid output, with unique grid name as 'list1'
  163. $out = $g->render("list1");
  164.  
  165. ?>
  166. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
  167. <html>
  168. <head>
  169. <link rel="stylesheet" type="text/css" media="screen" href="../../lib/js/themes/redmond/jquery-ui.custom.css"></link>
  170. <link rel="stylesheet" type="text/css" media="screen" href="../../lib/js/jqgrid/css/ui.jqgrid.css"></link>
  171.  
  172. <script src="../../lib/js/jquery.min.js" type="text/javascript"></script>
  173. <script src="../../lib/js/jqgrid/js/i18n/grid.locale-en.js" type="text/javascript"></script>
  174. <script src="../../lib/js/jqgrid/js/jquery.jqGrid.min.js" type="text/javascript"></script>
  175. <script src="../../lib/js/themes/jquery-ui.custom.min.js" type="text/javascript"></script>
  176. </head>
  177. <body>
  178. <div style="margin:10px;">
  179. <script>
  180. // e.g. to show footer summary
  181. function grid_onload()
  182. {
  183. var grid = $("#list1");
  184.  
  185. // initially select few records
  186. // grid.jqGrid('setSelection', 2, true);
  187. // grid.jqGrid('setSelection', 5, true);
  188. // grid.jqGrid('setSelection', 6, true);
  189.  
  190. // sum of displayed result
  191. sum = grid.jqGrid('getCol', 'total', false, 'sum'); // 'sum, 'avg', 'count' (use count-1 as it count footer row).
  192.  
  193. // sum of running total records
  194. sum_running = grid.jqGrid('getCol', 'running_total')[0];
  195.  
  196. // sum of total records
  197. sum_table = grid.jqGrid('getCol', 'table_total')[0];
  198.  
  199. // record count
  200. c = grid.jqGrid('getCol', 'id', false, 'sum');
  201.  
  202. // 4th arg value of false will disable the using of formatter
  203. grid.jqGrid('footerData','set', {id: 'Total: $ ' + sum.toFixed(2), invdate: 'Sub Total: '+sum_running, total: 'Grand Total: '+sum_table}, false);
  204. };
  205.  
  206. // e.g. to update footer summary on selection
  207. function grid_onselect()
  208. {
  209.  
  210. var grid = $("#list1");
  211.  
  212. var t = 0;
  213. var selr = grid.jqGrid('getGridParam','selarrrow'); // array of id's of the selected rows when multiselect options is true. Empty array if not selection
  214. for (var x=0;x<selr.length;x++)
  215. {
  216. t += parseFloat(grid.jqGrid('getCell', selr[x], 'total'));
  217. }
  218. grid.jqGrid('footerData','set', {invdate: 'Selected Total: '+t.toFixed(2)}, false);
  219. };
  220.  
  221. function select_all()
  222. {
  223. jQuery('#list1').jqGrid('resetSelection');
  224. var ids = jQuery('#list1').jqGrid('getDataIDs');
  225. for (i = 0; i < ids.length; i++) {
  226. jQuery('#list1').jqGrid('setSelection', ids[i], true);
  227. }
  228. }
  229.  
  230. function formatPrice(a,b,c) {
  231. if (a!='') {
  232. var n = nv = '';
  233. if (a.substr(0, 1) == '-')
  234. n = '-';
  235. var v = a.replace(/[^\d]/g,'').replace(/^0+/g,'');
  236. var d = v.substr(v.length - 2);
  237. v = v.substr(0, v.length - 2);
  238. while(v.length > 3) {
  239. nv = '<?php echo $GLOBALS['language']['thousandsSeparator']; ?>' + v.substr(v.length - 3) + nv;
  240. v = v.substr(0, v.length - 3);
  241. }
  242. var r = v + nv + '<?php echo $GLOBALS['language']['decimalSeparator']; ?>' + d;
  243. return n + ' ' + '$' + ' ' + r;
  244. }
  245. else
  246. return a;
  247. }
  248. </script>
  249.  
  250. <?php echo $out?>
  251. </div>
  252. <br>
  253. <button onclick="select_all()">Select All</button>
  254. </body>
  255. </html>
Add Comment
Please, Sign In to add comment