Advertisement
gridphp

Master detail with dropdown

Aug 9th, 2014
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.97 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. // master grid
  21. $grid = new jqgrid();
  22. $opt["caption"] = "Clients Data";
  23. $opt["height"] = "150";
  24. // following params will enable subgrid -- by default first column (PK) of parent is passed as param 'id'
  25. $opt["detail_grid_id"] = "list2";
  26.  
  27. // extra params passed to detail grid, column name comma separated
  28. $opt["subgridparams"] = "client_id,gender,company";
  29. $opt["multiselect"] = false;
  30. $opt["export"] = array("filename"=>"my-file", "sheetname"=>"test", "format"=>"pdf");
  31. $opt["export"]["range"] = "filtered";
  32. $grid->set_options($opt);
  33. $grid->table = "clients";
  34.  
  35. $grid->set_actions(array(  
  36.                         "add"=>true, // allow/disallow add
  37.                         "edit"=>true, // allow/disallow edit
  38.                         "delete"=>true, // allow/disallow delete
  39.                         "rowactions"=>false, // show/hide row wise edit/del/save option
  40.                         "export"=>true, // show/hide export to excel option
  41.                         "autofilter" => true, // show/hide autofilter for search
  42.                         "search" => "advance" // show single/multi field search condition (e.g. simple or advance)
  43.                     )
  44.                 );
  45.                
  46. $out_master = $grid->render("list1");
  47.  
  48. // detail grid
  49. $grid = new jqgrid();
  50.  
  51. $opt = array();
  52. $opt["sortname"] = 'id'; // by default sort grid by this field
  53. $opt["sortorder"] = "desc"; // ASC or DESC
  54. $opt["height"] = ""; // autofit height of subgrid
  55. $opt["caption"] = "Invoice Data"; // caption of grid
  56. $opt["multiselect"] = true; // allow you to multi-select through checkboxes
  57. $opt["export"] = array("filename"=>"my-file", "sheetname"=>"test", "format"=>"pdf"); // export to excel parameters
  58. $opt["export"]["range"] = "filtered";
  59. // Check if master record is selected before detail addition
  60. $opt["add_options"]["beforeInitData"] = "function(formid){ var selr = jQuery('#list1').jqGrid('getGridParam','selrow'); if (!selr) { alert('Please select master record first'); return false; } }";
  61. $grid->set_options($opt);
  62.  
  63. $grid->set_actions(array(  
  64.                         "add"=>true, // allow/disallow add
  65.                         "edit"=>true, // allow/disallow edit
  66.                         "delete"=>true, // allow/disallow delete
  67.                         "rowactions"=>true, // show/hide row wise edit/del/save option
  68.                         "export"=>true, // show/hide export to excel option
  69.                         "autofilter" => true, // show/hide autofilter for search
  70.                         "search" => "advance" // show single/multi field search condition (e.g. simple or advance)
  71.                     )
  72.                 );
  73.  
  74.  
  75. // receive id, selected row of parent grid
  76. $id = intval($_GET["rowid"]);
  77. $gender = $_GET["gender"];
  78. $company = $_GET["company"];
  79. $cid = intval($_GET["client_id"]);
  80.  
  81. // for non-int fields as PK
  82. // $id = (empty($_GET["rowid"])?0:$_GET["rowid"]);
  83.  
  84. // and use in sql for filteration
  85. $grid->select_command = "SELECT id,client_id,invdate,amount,tax,note,total,'$company' as 'company' FROM invheader WHERE client_id = $id";
  86. // this db table will be used for add,edit,delete
  87. $grid->table = "invheader";
  88.  
  89. $col = array();
  90. $col["title"] = "Id"; // caption of column
  91. $col["name"] = "id"; // field name, must be exactly same as with SQL prefix or db field
  92. $col["width"] = "10";
  93. $col["formoptions"] = array("rowpos"=>"1", "colpos"=>"1");
  94. $cols[] = $col;
  95.  
  96. $col = array();
  97. $col["title"] = "Company"; // caption of column
  98. $col["name"] = "company"; // field name, must be exactly same as with SQL prefix or db field
  99. $col["width"] = "100";
  100. $col["editable"] = false;
  101. $col["formoptions"] = array("rowpos"=>"1", "colpos"=>"2");
  102. $col["show"] = array("list"=>true,"edit"=>true,"add"=>false,"view"=>false);
  103. $cols[] = $col;
  104.  
  105. $col = array();
  106. $col["title"] = "Client";
  107. $col["name"] = "client_id";
  108. $col["width"] = "100";
  109. $col["align"] = "left";
  110. $col["hidden"] = true;
  111. $col["editable"] = true;
  112. $col["edittype"] = "select"; // render as select
  113. $col["editrules"] = array("edithidden"=>true);
  114. $col["editoptions"]["onload"]["sql"] = "select distinct client_id as k, name as v from clients WHERE client_id = '$cid'";
  115. $col["editoptions"]["onchange"]["sql"] = "select note as k, note as v from invheader WHERE client_id = '{client_id}'";
  116. $col["editoptions"]["onchange"]["update_field"] = "note";
  117. $cols[] = $col;
  118.  
  119. $col = array();
  120. $col["title"] = "Invoices";
  121. $col["name"] = "note";
  122. $col["width"] = "100";
  123. $col["search"] = true;
  124. $col["editable"] = true;
  125. $col["edittype"] = "select"; // render as select
  126. $col["editrules"] = array("edithidden"=>true);
  127. $col["editoptions"]["onload"]["sql"] = "select note as k, note as v from invheader WHERE client_id = '$cid'";
  128. $cols[] = $col;
  129.  
  130. $grid->set_columns($cols);
  131. $e["on_insert"] = array("add_client", null, true);
  132. $grid->set_events($e);
  133.  
  134. function add_client(&$data)
  135. {
  136.     $id = intval($_GET["rowid"]);
  137.     $data["params"]["client_id"] = $id;
  138. }
  139.  
  140. // generate grid output, with unique grid name as 'list1'
  141. $out_detail = $grid->render("list2");
  142. ?>
  143. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
  144. <html>
  145. <head>
  146.     <link rel="stylesheet" type="text/css" media="screen" href="../../lib/js/themes/start/jquery-ui.custom.css"></link>
  147.     <link rel="stylesheet" type="text/css" media="screen" href="../../lib/js/jqgrid/css/ui.jqgrid.css"></link> 
  148.    
  149.     <script src="../../lib/js/jquery.min.js" type="text/javascript"></script>
  150.     <script src="../../lib/js/jqgrid/js/i18n/grid.locale-en.js" type="text/javascript"></script>
  151.     <script src="../../lib/js/jqgrid/js/jquery.jqGrid.min.js" type="text/javascript"></script> 
  152.     <script src="../../lib/js/themes/jquery-ui.custom.min.js" type="text/javascript"></script>
  153. </head>
  154. <body>
  155.     <div style="margin:10px">
  156.     Master Detail Grid, on same page
  157.     <br>
  158.     <br>
  159.     <?php echo $out_master ?>
  160.     <br>
  161.     <br>
  162.     <?php echo $out_detail; ?>
  163.     </div>
  164. </body>
  165. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement