Advertisement
gridphp

Calculated Columns in PHP Grid

Jul 10th, 2014
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.53 KB | None | 0 0
  1.  
  2. <?php
  3. /**
  4.  * PHP Grid Component
  5.  *
  6.  * @author Abu Ghufran <gridphp@gmail.com> - http://www.phpgrid.org
  7.  * @version 1.5.2
  8.  * @license: see license.txt included in package
  9.  */
  10.  
  11. // include db config
  12. include_once("../../config.php");
  13.  
  14. // set up DB
  15. mysql_connect(PHPGRID_DBHOST, PHPGRID_DBUSER, PHPGRID_DBPASS);
  16. mysql_select_db(PHPGRID_DBNAME);
  17.  
  18. // include and create object
  19. include(PHPGRID_LIBPATH."inc/jqgrid_dist.php");
  20.  
  21. $g = new jqgrid();
  22.  
  23. $col = array();
  24. $col["title"] = "Id"; // caption of column
  25. $col["name"] = "client_id";
  26. $col["width"] = "10";
  27. $col["editable"] = true;
  28. $cols[] = $col;    
  29.  
  30.  
  31. $col = array();
  32. $col["title"] = "Formula"; // caption of column
  33. $col["name"] = "cal_field";
  34. $col["width"] = "20";
  35. $col["editable"] = false;
  36. $cols[] = $col;
  37.  
  38. $col = array();
  39. $col["title"] = "Client";
  40. $col["name"] = "name";
  41. $col["width"] = "100";
  42. $col["search"] = true;
  43. $col["editable"] = true;
  44. $col["export"] = false; // this column will not be exported
  45. $cols[] = $col;
  46.  
  47. $col = array();
  48. $col["title"] = "Diff";
  49. $col["name"] = "diff";
  50. $col["width"] = "20";
  51. $col["search"] = true;
  52. $col["editable"] = false;
  53. $cols[] = $col;
  54.  
  55. $col = array();
  56. $col["title"] = "Calc Text";
  57. $col["name"] = "newcol";
  58. $col["width"] = "100";
  59. $col["search"] = true;
  60. $col["editable"] = false;
  61. $cols[] = $col;
  62.  
  63. $grid["sortname"] = 'client_id'; // by default sort grid by this field
  64. $grid["sortorder"] = "desc"; // ASC or DESC
  65. $grid["caption"] = "Clients Data"; // caption of grid
  66. $grid["autowidth"] = true; // expand grid to screen width
  67. $grid["multiselect"] = false; // allow you to multi-select through checkboxes
  68. $grid["reloadedit"] = true;
  69.  
  70. $g->set_options($grid);
  71.  
  72. $g->set_actions(array( 
  73.                         "add"=>false, // allow/disallow add
  74.                         "edit"=>true, // allow/disallow edit
  75.                         "delete"=>true, // allow/disallow delete
  76.                         "rowactions"=>true, // show/hide row wise edit/del/save option
  77.                         "autofilter" => true, // show/hide autofilter for search
  78.                         "search" => "advance" // show single/multi field search condition (e.g. simple or advance)
  79.                     )
  80.                 );
  81.  
  82. // you can provide custom SQL query to display data
  83. $g->select_command = "SELECT * FROM clients";
  84.  
  85. // this db table will be used for add,edit,delete
  86. $g->table = "clients";
  87.  
  88. // pass the cooked columns to grid
  89. $g->set_columns($cols);
  90.  
  91. $e["on_data_display"] = array("filter_display", null, true);
  92. $g->set_events($e);
  93.  
  94. function filter_display($data)
  95. {
  96.     foreach($data["params"] as &$d)
  97.     {
  98.         $d["cal_field"] = "<a href='/index.php?i=".md5($d["client_id"])."'>Click Me</a>";
  99.         $d["newcol"] = "Hello, ".$d["name"] . " ({$d["client_id"]})";
  100.         $d["diff"] = 200-$d["client_id"];
  101.     }
  102. }
  103.  
  104.  
  105. // generate grid output, with unique grid name as 'list1'
  106. $out = $g->render("list1");
  107. ?>
  108. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
  109. <html>
  110. <head>
  111.     <link rel="stylesheet" type="text/css" media="screen" href="../../lib/js/themes/smoothness/jquery-ui.custom.css"></link>   
  112.     <link rel="stylesheet" type="text/css" media="screen" href="../../lib/js/jqgrid/css/ui.jqgrid.css"></link> 
  113.    
  114.     <script src="../../lib/js/jquery.min.js" type="text/javascript"></script>
  115.     <script src="../../lib/js/jqgrid/js/i18n/grid.locale-en.js" type="text/javascript"></script>
  116.     <script src="../../lib/js/jqgrid/js/jquery.jqGrid.min.js" type="text/javascript"></script> 
  117.     <script src="../../lib/js/themes/jquery-ui.custom.min.js" type="text/javascript"></script>
  118. </head>
  119. <body>
  120.     <div style="margin:10px">
  121.     <?php echo $out?>
  122.     </div>
  123. </body>
  124. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement