Advertisement
gridphp

dropdown-callback

May 20th, 2014
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.78 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. // set up DB
  14. mysql_connect(PHPGRID_DBHOST, PHPGRID_DBUSER, PHPGRID_DBPASS);
  15. mysql_select_db(PHPGRID_DBNAME);
  16.  
  17. // include and create object
  18. include(PHPGRID_LIBPATH."inc/jqgrid_dist.php");
  19.  
  20. $g = new jqgrid();
  21.  
  22. // this db table will be used for add,edit,delete
  23. $g->table = "clients";
  24.  
  25. $col = array();
  26. $col["title"] = "Id";
  27. $col["name"] = "client_id";
  28. $col["width"] = "20";
  29. $col["editable"] = true;
  30. $col["hidden"] = true;
  31. $cols[] = $col;
  32.  
  33. $col = array();
  34. $col["title"] = "Name";
  35. $col["name"] = "name";
  36. $col["editable"] = true;
  37. $col["width"] = "80";
  38. $col["edittype"] = "select"; // render as select
  39. # fetch data from database, with alias k for key, v for value
  40. $str = $g->get_dropdown_values("select distinct client_id as k, name as v from clients");
  41. $col["editoptions"] = array(
  42.                             "value"=>$str,
  43.                             "onchange" => array(   
  44.                                     "sql"=>"select * from clients",
  45.                                     "search_on"=>"client_id",
  46.                                     "callback" => "fill_form" )
  47.                             );
  48. $col["stype"] = "select"; // enable dropdown search
  49. $col["searchoptions"] = array("value" => ":;".$str);
  50. $cols[] = $col;
  51.  
  52. $col = array();
  53. $col["title"] = "Gender";
  54. $col["name"] = "gender";
  55. $col["width"] = "40";
  56. $col["editable"] = true;
  57. $cols[] = $col;
  58.  
  59. $col = array();
  60. $col["title"] = "Company";
  61. $col["name"] = "company";
  62. $col["editable"] = true;
  63. $col["edittype"] = "textarea";
  64. $col["editoptions"] = array("rows"=>2, "cols"=>20);
  65. $cols[] = $col;
  66.  
  67. // 2 dropdown columns to simulate multiple dependent dropdowns based on single dropdown (name)
  68.  
  69. $col = array();
  70. $col["title"] = "Invoice";
  71. $col["name"] = "note";
  72. $col["width"] = "50";
  73. $col["editable"] = true;
  74. $col["edittype"] = "select"; // render as select
  75. $str = $g->get_dropdown_values("select note as k, note as v from invheader");
  76. $col["editoptions"] = array("value"=>$str);
  77. // initially load 'note' of that client_id
  78. $col["editoptions"]["onload"]["sql"] = "select note as k, note as v from invheader WHERE client_id = '{name}'";
  79. $col["formatter"] = "select"; // display label, not value
  80. $cols[] = $col;
  81.  
  82. $col = array();
  83. $col["title"] = "Amount";
  84. $col["name"] = "amount";
  85. $col["width"] = "50";
  86. $col["search"] = true;
  87. $col["editable"] = true;
  88. $col["edittype"] = "select"; // render as select
  89. $str = $g->get_dropdown_values("select amount as k, amount as v from invheader");
  90. $col["editoptions"] = array("value"=>$str);
  91. // initially load 'note' of that client_id
  92. $col["editoptions"]["onload"]["sql"] = "select amount as k, amount as v from invheader WHERE client_id = '{name}'";
  93. $cols[] = $col;
  94.  
  95.  
  96. $g->set_columns($cols);
  97.  
  98.  
  99. $grid["caption"] = "Clients Data"; // caption of grid
  100. $grid["autowidth"] = true; // expand grid to screen width
  101. $grid["multiselect"] = false; // allow you to multi-select through checkboxes
  102.  
  103. $g->set_options($grid);
  104.  
  105. $g->set_actions(array( 
  106.                         "add"=>true, // allow/disallow add
  107.                         "edit"=>true, // allow/disallow edit
  108.                         "delete"=>false, // allow/disallow delete
  109.                         "rowactions"=>true, // show/hide row wise edit/del/save option
  110.                         "autofilter" => true, // show/hide autofilter for search
  111.                     )
  112.                 );
  113.  
  114. // generate grid output, with unique grid name as 'list1'
  115. $out = $g->render("list1");
  116. ?>
  117. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
  118. <html>
  119. <head>
  120.     <link rel="stylesheet" type="text/css" media="screen" href="../../lib/js/themes/redmond/jquery-ui.custom.css"></link>  
  121.     <link rel="stylesheet" type="text/css" media="screen" href="../../lib/js/jqgrid/css/ui.jqgrid.css"></link> 
  122.    
  123.     <script src="../../lib/js/jquery.min.js" type="text/javascript"></script>
  124.     <script src="../../lib/js/jqgrid/js/i18n/grid.locale-en.js" type="text/javascript"></script>
  125.     <script src="../../lib/js/jqgrid/js/jquery.jqGrid.min.js" type="text/javascript"></script> 
  126.     <script src="../../lib/js/themes/jquery-ui.custom.min.js" type="text/javascript"></script>
  127. </head>
  128. <body>
  129.     <div style="margin:10px">
  130.     <?php echo $out?>
  131.     </div>
  132.    
  133.     <script>
  134.     function fill_form(data)
  135.     {
  136.         jQuery("input[name=gender].FormElement").val(data[0].gender);
  137.         jQuery("textarea[name=company].FormElement").val(data[0].company);
  138.  
  139.         jQuery("input[name=gender].editable").val(data[0].gender);
  140.         jQuery("textarea[name=company].editable").val(data[0].company);
  141.        
  142.         // fill dropdown 1 based on row data
  143.         o = jQuery('select[name=note]').get();
  144.         o.event = 'onload'; fx_get_dropdown(o,'note');     
  145.  
  146.         // fill dropdown 2 based on row data
  147.         o = jQuery('select[name=amount]').get();
  148.         o.event = 'onload'; fx_get_dropdown(o,'amount');
  149.        
  150.     }
  151.     </script>
  152. </body>
  153. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement