Guest User

Untitled

a guest
Mar 11th, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.71 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. // you can customize your own columns ...
  17.  
  18. $col = array();
  19. $col["title"] = "Id"; // caption of column
  20. $col["name"] = "id"; // grid column name, must be exactly same as returned column-name from sql (tablefield or field-alias)
  21. $col["width"] = "10";
  22. $col["editable"] = true;
  23. $col["hidden"] = true;
  24. $cols[] = $col;
  25.  
  26. $col = array();
  27. $col["title"] = "Date";
  28. $col["name"] = "invdate";
  29. $col["width"] = "50";
  30. $col["editable"] = true; // this column is editable
  31. $col["editoptions"] = array("size"=>20, "defaultValue"=>"02/02/2013"); // with default display of textbox with size 20
  32. $col["editrules"] = array("required"=>true, "edithidden"=>true); // and is required
  33. $col["formatter"] = "date"; // format as date
  34. $col["formatoptions"] = array("srcformat"=>'Y-m-d',"newformat"=>'d/m/Y'); // http://docs.jquery.com/UI/Datepicker/formatDate
  35. $cols[] = $col;
  36.  
  37. $col = array();
  38. $col["title"] = "Client";
  39. $col["name"] = "name";
  40. $col["width"] = "100";
  41. $col["editable"] = false; // this column is not editable
  42. $col["align"] = "center"; // this column is not editable
  43. $col["search"] = false; // this column is not searchable
  44.  
  45. $cols[] = $col;
  46.  
  47. $col = array();
  48. $col["title"] = "Note";
  49. $col["name"] = "note";
  50. # $col["width"] = "300"; // not specifying width will expand to fill space
  51. $col["sortable"] = false; // this column is not sortable
  52. $col["search"] = false; // this column is not searchable
  53. $col["editable"] = true;
  54. $col["edittype"] = "textarea"; // render as textarea on edit
  55. $col["editoptions"] = array("rows"=>2, "cols"=>20); // with these attributes
  56. $cols[] = $col;
  57.  
  58. $col = array();
  59. $col["title"] = "Total";
  60. $col["name"] = "total";
  61. $col["width"] = "50";
  62. $col["editable"] = true;
  63. $col["formatter"] = "number";
  64.  
  65. // To mask password field, apply following attribs
  66. # $col["edittype"] = "password";
  67. # $col["formatter"] = "password";
  68.  
  69. // default render is textbox
  70. // $col["editoptions"] = array("value"=>'10');
  71.  
  72. // can be switched to select (dropdown)
  73. // $col["edittype"] = "select"; // render as select
  74. // $col["editoptions"] = array("value"=>'10:$10;20:$20;30:$30;40:$40;50:$50'); // with these values "key:value;key:value;key:value"
  75.  
  76. $cols[] = $col;
  77.  
  78. $col = array();
  79. $col["title"] = "Closed";
  80. $col["name"] = "closed";
  81. $col["width"] = "50";
  82.  
  83. $col["editable"] = true;
  84. $col["edittype"] = "checkbox"; // render as checkbox
  85. $col["editoptions"] = array("value"=>"1:0"); // with these values "checked_value:unchecked_value"
  86.  
  87. // custom formatter to show active checkbox
  88. $col["formatter"] = "function(cellvalue, options, rowObject){ return cboxFormatter(cellvalue, options, rowObject); }";
  89. $col["unformat"] = "function(cellvalue, options, cell){ return cboxUnFormat(cellvalue, options, cell);}";
  90.  
  91. $col["align"] = "center";
  92. $cols[] = $col;
  93.  
  94. $g = new jqgrid();
  95.  
  96. // $grid["url"] = ""; // your paramterized URL -- defaults to REQUEST_URI
  97. $grid["rowNum"] = 10; // by default 20
  98. $grid["sortname"] = 'id'; // by default sort grid by this field
  99. $grid["sortorder"] = "desc"; // ASC or DESC
  100. $grid["caption"] = "Checkbox + Ajax update without edit mode"; // caption of grid
  101. $grid["autowidth"] = true; // expand grid to screen width
  102. $grid["multiselect"] = true; // allow you to multi-select through checkboxes
  103. $grid["form"]["position"] = "center";
  104. $g->set_options($grid);
  105.  
  106. $g->set_actions(array(
  107. "add"=>true, // allow/disallow add
  108. "edit"=>true, // allow/disallow edit
  109. "delete"=>true, // allow/disallow delete
  110. "view"=>true, // allow/disallow delete
  111. "rowactions"=>true, // show/hide row wise edit/del/save option
  112. "search" => "advance", // show single/multi field search condition (e.g. simple or advance)
  113. "showhidecolumns" => false
  114. )
  115. );
  116.  
  117. // you can provide custom SQL query to display data
  118. $g->select_command = "SELECT i.id, invdate , c.name,
  119. i.note, i.total, i.closed FROM invheader i
  120. INNER JOIN clients c ON c.client_id = i.client_id";
  121.  
  122. // you can provide custom SQL count query to display data
  123. $g->select_count = "SELECT count(*) as c FROM invheader i
  124. INNER JOIN clients c ON c.client_id = i.client_id";
  125.  
  126. // this db table will be used for add,edit,delete
  127. $g->table = "invheader";
  128.  
  129. // pass the cooked columns to grid
  130. $g->set_columns($cols);
  131.  
  132. // generate grid output, with unique grid name as 'list1'
  133. $out = $g->render("list1");
  134. ?>
  135. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
  136. <html>
  137. <head>
  138. <link rel="stylesheet" type="text/css" media="screen" href="../../lib/js/themes/redmond/jquery-ui.custom.css"></link>
  139. <link rel="stylesheet" type="text/css" media="screen" href="../../lib/js/jqgrid/css/ui.jqgrid.css"></link>
  140. <script src="../../lib/js/jquery.min.js" type="text/javascript"></script>
  141. <script src="../../lib/js/jqgrid/js/i18n/grid.locale-en.js" type="text/javascript"></script>
  142. <script src="../../lib/js/jqgrid/js/jquery.jqGrid.min.js" type="text/javascript"></script>
  143. <script src="../../lib/js/themes/jquery-ui.custom.min.js" type="text/javascript"></script>
  144. </head>
  145. <body>
  146. <script>
  147. // checkbox + ajax update without edit mode
  148. function cboxFormatter(cellvalue, options, rowObject)
  149. {
  150. if ( cellvalue == 1 )
  151. return '<input id="cbox'+options.rowId+'" type="checkbox" name="completed" value="'+options.rowId+'" onclick="updateRow('+options.rowId+',this.checked);" checked/> ';
  152. else
  153. return '<input id="cbox'+options.rowId+'" type="checkbox" name="completed" value="'+options.rowId+'" onclick="updateRow('+options.rowId+',this.checked);" /> ';
  154. }
  155.  
  156. function cboxUnFormat(cellvalue, options, cell)
  157. {
  158. return jQuery('input', cell).attr('value');
  159. }
  160.  
  161. function updateRow(id, checked)
  162. {
  163. // call ajax to update date in db
  164. var request = {};
  165. request['oper'] = 'edit';
  166. request['id'] = id;
  167.  
  168. if (checked)
  169. request['closed'] = 1;
  170. else
  171. request['closed'] = 0;
  172.  
  173. var grid = jQuery('#list1');
  174. jQuery.ajax({
  175. url: grid.jqGrid('getGridParam','url'),
  176. dataType: 'html',
  177. data: request,
  178. type: 'POST',
  179. error: function(res, status) {
  180. jQuery.jgrid.info_dialog(jQuery.jgrid.errors.errcap,'<div class=\"ui-state-error\">'+ res.responseText +'</div>',
  181. jQuery.jgrid.edit.bClose,{buttonalign:'right'});
  182. },
  183. success: function( data ) {
  184. // reload grid for data changes
  185. grid.jqGrid().trigger('reloadGrid',[{jqgrid_page:1}]);
  186. }
  187. });
  188. }
  189.  
  190. </script>
  191. <div style="margin:10px">
  192. <?php echo $out?>
  193. </div>
  194. </body>
  195. </html>
Add Comment
Please, Sign In to add comment