Advertisement
Guest User

Master/Detal insert

a guest
Mar 30th, 2015
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.64 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";
  23. $opt["height"] = "150";
  24. $opt["sortorder"] = "desc";
  25. // following params will enable subgrid -- by default first column (PK) of parent is passed as param 'id'
  26. $opt["detail_grid_id"] = "list2";
  27.  
  28. // extra params passed to detail grid, column name comma separated
  29. $opt["subgridparams"] = "client_id,client";
  30. $opt["multiselect"] = false;
  31. $opt["export"] = array("filename"=>"my-file", "sheetname"=>"Notes Report", "format"=>"pdf");
  32. $opt["export"]["range"] = "filtered";
  33. $grid->set_options($opt);
  34. $grid->table = "tbl_clients";
  35.  
  36. $grid->set_actions(array(  
  37.                         "add"=>true, // allow/disallow add
  38.                         "edit"=>true, // allow/disallow edit
  39.                         "delete"=>true, // allow/disallow delete
  40.                         "rowactions"=>true, // show/hide row wise edit/del/save option
  41.                         "export"=>true, // show/hide export to excel option
  42.                         "autofilter" => true, // show/hide autofilter for search
  43.                         "search" => "advance" // show single/multi field search condition (e.g. simple or advance)
  44.                     )
  45.                 );
  46.                
  47. $out_master = $grid->render("list1");
  48.  
  49. // detail grid
  50. $grid = new jqgrid();
  51.  
  52. $opt = array();
  53. $opt["sortname"] = 'task_id'; // by default sort grid by this field
  54. $opt["sortorder"] = "desc"; // ASC or DESC
  55. $opt["height"] = ""; // autofit height of subgrid
  56. $opt["caption"] = "Notes"; // caption of grid
  57. $opt["multiselect"] = true; // allow you to multi-select through checkboxes
  58. $opt["reloadedit"] = true; // allow you to multi-select through checkboxes
  59. $opt["export"] = array("filename"=>"my-file", "sheetname"=>"test", "format"=>"pdf"); // export to excel parameters
  60. $opt["export"]["range"] = "filtered";
  61.  
  62. // Check if master record is selected before detail addition
  63. // $opt["add_options"]["beforeInitData"] = "function(formid){ var selr = jQuery('#list1').jqGrid('getGridParam','selrow'); if (!selr) { alert('Please select master record first'); return false; } }";
  64.  
  65. // reload master after detail update
  66. $opt["onAfterSave"] = "function(){ jQuery('#list1').trigger('reloadGrid',[{jqgrid_page:1}]); }";
  67.  
  68. $grid->set_options($opt);
  69.  
  70. $grid->set_actions(array(  
  71.                         "add"=>true, // allow/disallow add
  72.                         "edit"=>true, // allow/disallow edit
  73.                         "delete"=>true, // allow/disallow delete
  74.                         "rowactions"=>true, // show/hide row wise edit/del/save option
  75.                         "export"=>true, // show/hide export to excel option
  76.                         "autofilter" => true, // show/hide autofilter for search
  77.                         "search" => "advance" // show single/multi field search condition (e.g. simple or advance)
  78.                     )
  79.                 );
  80.  
  81.  
  82. // receive id, selected row of parent grid
  83. $id = intval($_GET["rowid"]);
  84. $client = $_GET["Client"];
  85. $cid = intval($_GET["client_id"]);
  86.  
  87. // for non-int fields as PK
  88. // $id = (empty($_GET["rowid"])?0:$_GET["rowid"]);
  89.  
  90. // and use in sql for filteration
  91. $grid->select_command = "SELECT task_id,client_id,client,subject,note,lead_id,task_date FROM tbl_notes WHERE client_id = $id";
  92. // this db table will be used for add,edit,delete
  93. $grid->table = "tbl_notes";
  94.  
  95. $col = array();
  96. $col["title"] = "ID"; // caption of column
  97. $col["name"] = "task_id"; // field name, must be exactly same as with SQL prefix or db field
  98. $col["width"] = "10";
  99. $col["hidden"] = true; // hide on grid
  100. $col["editable"] = true;
  101. $cols[] = $col;
  102.  
  103. $col = array();
  104. $col["title"] = "CID"; // caption of column
  105. $col["name"] = "client_id"; // field name, must be exactly same as with SQL prefix or db field
  106. $col["width"] = "10";
  107. $col["hidden"] = true; // hide on grid
  108. $col["editable"] = true;
  109. $cols[] = $col;
  110.  
  111.  
  112. $col = array();
  113. $col["title"] = "Client"; // caption of column
  114. $col["name"] = "client"; // field name, must be exactly same as with SQL prefix or db field
  115. $col["width"] = "100";
  116. $col["editable"] = true;
  117. $col["show"] = array("list"=>true,"edit"=>true,"add"=>false,"view"=>false);
  118. $cols[] = $col;
  119.  
  120. $col = array();
  121. $col["title"] = "Lead_ID";
  122. $col["name"] = "lead_id";
  123. $col["width"] = "100";
  124. $col["align"] = "left";
  125. $col["hidden"] = true; // hide on grid
  126. $col["search"] = true;
  127. $col["editable"] = true;
  128. $cols[] = $col;
  129.  
  130. $col = array();
  131. $col["title"] = "Subject";
  132. $col["name"] = "subject";
  133. $col["width"] = "100";
  134. $col["search"] = true;
  135. $col["editable"] = true;
  136. $cols[] = $col;
  137.  
  138. $col = array();
  139. $col["title"] = "Note";
  140. $col["name"] = "note";
  141. $col["sortable"] = false; // this column is not sortable
  142. $col["search"] = false; // this column is not searchable
  143. $col["editable"] = true;
  144. // $col["hidden"] = true;
  145. $col["edittype"] = "textarea"; // render as textarea on edit
  146. $col["formatter"] = "wysiwyg";
  147. $col["editoptions"] = array("rows"=>2, "cols"=>50); // with these attributes
  148. // $col["editrules"] = array("edithidden"=>true); // with these attributes
  149. $cols[] = $col;
  150.  
  151. $grid->set_columns($cols);
  152. $e["on_insert"] = array("add_client", null, true);
  153. $e["on_update"] = array("update_client", null, true);
  154. $grid->set_events($e);
  155.  
  156.  
  157. $grid->set_actions(array(  
  158.                         "add"=>true, // allow/disallow add
  159.                         "edit"=>true, // allow/disallow edit
  160.                         "delete"=>true, // allow/disallow delete
  161.                         "inlineadd"=>true, // allow/disallow delete
  162.                         "rowactions"=>true, // show/hide row wise edit/del/save option
  163.                         "autofilter" => true, // show/hide autofilter for search
  164.                         "search" => "advance" // show single/multi field search condition (e.g. simple or advance)
  165.                     )
  166.                 );
  167.  
  168.            
  169. function add_client(&$data)
  170. {
  171.     $id = intval($_GET["rowid"]);
  172.     $data["params"]["client_id"] = $id;
  173.     $data["params"]["subject"] = $subject;
  174.     $data["params"]["note"] = $note;
  175.  
  176. }
  177.  
  178. function update_client(&$data)
  179. {
  180.     $id = intval($_GET["rowid"]);
  181.     $data["params"]["client_id"] = $id;
  182.     $data["params"]["subject"] = $subject;
  183.     $data["params"]["note"] = $note;
  184. }
  185.  
  186. // generate grid output, with unique grid name as 'list1'
  187. $out_detail = $grid->render("list2");
  188. ?>
  189. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
  190. <html>
  191. <head>
  192.     <link rel="stylesheet" type="text/css" media="screen" href="../lib/js/themes/redmond/jquery-ui.custom.css"></link>    
  193.     <link rel="stylesheet" type="text/css" media="screen" href="../lib/js/jqgrid/css/ui.jqgrid.css">
  194.     <link href="../bootstrap/css/bootstrap.min.css" rel="stylesheet">
  195.     <link rel="stylesheet" type="text/css" media="screen" href="../lib/js/jqgrid/css/ui.bootstrap.jqgrid.css">      
  196.  
  197.     <script src="../lib/js/jquery.min.js" type="text/javascript"></script>
  198.     <script src="../lib/js/jqgrid/js/i18n/grid.locale-en.js" type="text/javascript"></script>
  199.     <script src="../lib/js/jqgrid/js/jquery.jqGrid.min.js" type="text/javascript"></script>    
  200.     <script src="../lib/js/themes/jquery-ui.custom.min.js" type="text/javascript"></script>
  201.     <script src="//cdn.jsdelivr.net/jquery.hotkeys/0.8b/jquery.hotkeys.js"></script>
  202.     <script src="../lib/js/ckeditor/ckeditor.js" type="text/javascript"></script>
  203.  
  204. </head>
  205. <body>
  206.     <div style="margin:10px">
  207.     Master Detail Grid, on same page
  208.     <br>
  209.     <br>
  210.     <?php echo $out_master ?>
  211.     <br>
  212.     <br>
  213.     <?php echo $out_detail; ?>
  214.     </div>
  215.    
  216.     <script>
  217.     // insert key to add new row, tab to focus on save icon & press enter to save
  218.     $(document).bind('keyup', 'insert', function(){
  219.           jQuery('#list2_iladd').click();
  220.         });
  221.        
  222.     </script>  
  223. </body>
  224. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement