Guest User

Untitled

a guest
Apr 23rd, 2015
239
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. // set few params
  23. $grid["caption"] = "Sample Grid";
  24. $grid["height"] = "250";
  25. $grid["width"] = "1200";
  26. $grid["autowidth"] = true;
  27. // $grid["scroll"] = true;
  28. $grid["multiselect"] = true;
  29. $grid["rowList"] = array();
  30. $grid["rowNum"] = 15;
  31. $grid["form"]["position"] = "center";
  32. $g->set_options($grid);
  33.  
  34. // set database table for CRUD operations
  35. $g->table = "clients";
  36. // $g->select_command = "clients";
  37.  
  38. $col = array();
  39. $col["title"] = "Id"; // caption of column
  40. $col["name"] = "client_id"; // grid column name, must be exactly same as returned column-name from sql (tablefield or field-alias)
  41. $col["editable"] = true;
  42. $cols[] = $col;
  43.  
  44. $col = array();
  45. $col["title"] = "Name"; // caption of column
  46. $col["name"] = "name"; // grid column name, must be exactly same as returned column-name from sql (tablefield or field-alias)
  47. $col["editable"] = true;
  48. $cols[] = $col;
  49.  
  50. $col = array();
  51. $col["title"] = "Gender"; // caption of column
  52. $col["name"] = "gender"; // grid column name, must be exactly same as returned column-name from sql (tablefield or field-alias)
  53. $col["editable"] = true;
  54. $col["edittype"] = "select";
  55. $col["editoptions"] = array("value" => "male:Male;female:Female");
  56.  
  57. // use callback function condition for readonly
  58. $col["editrules"] = array("required"=>true, "readonly"=>true, "readonly-when"=>"check_client");
  59.  
  60. // use certain field value condition for readonly
  61. // $col["editrules"] = array("required"=>true, "readonly"=>true, "readonly-when"=>array("client_id","==","8"));
  62.  
  63. $col["show"] = array("list"=>true, "add"=>true, "edit"=>true, "view"=>true);
  64. $cols[] = $col;
  65.  
  66. $col = array();
  67. $col["title"] = "Company"; // caption of column
  68. $col["name"] = "company"; // grid column name, must be exactly same as returned column-name from sql (tablefield or field-alias)
  69. $col["editable"] = true;
  70. // $col["formatter"] = "wysiwyg";
  71. $col["editoptions"] = array("defaultValue" => "Default Company");
  72. $cols[] = $col;
  73.  
  74. $g->set_columns($cols);
  75.  
  76. $e["js_on_load_complete"] = "grid_load";
  77. $e["js_on_select_row"] = "grid_select";
  78. $g->set_events($e);
  79.  
  80. $g->set_actions(array( 
  81.                         "add"=>true, // allow/disallow add
  82.                         "edit"=>true, // allow/disallow edit
  83.                         "delete"=>true, // allow/disallow delete
  84.                         "view"=>true, // allow/disallow view
  85.                         "rowactions"=>true, // show/hide row wise edit/del/save option
  86.                         "export"=>true, // show/hide export to excel option
  87.                         "autofilter" => true, // show/hide autofilter for search
  88.                         "search" => "advance" // show single/multi field search condition (e.g. simple or advance)
  89.                     )
  90.                 );
  91.  
  92. // render grid
  93. $out = $g->render("list1");
  94.  
  95. ?>
  96. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
  97. <html>
  98. <head>
  99.     <link rel="stylesheet" type="text/css" media="screen" href="../../lib/js/themes/redmond/jquery-ui.custom.css"></link>  
  100.     <link rel="stylesheet" type="text/css" media="screen" href="../../lib/js/jqgrid/css/ui.jqgrid.css"></link> 
  101.     <script src="../../lib/js/ckeditor/ckeditor.js" type="text/javascript"></script>
  102.     <script src="../../lib/js/jquery.min.js" type="text/javascript"></script>
  103.     <script src="../../lib/js/jqgrid/js/i18n/grid.locale-en.js" type="text/javascript"></script>
  104.     <script src="../../lib/js/jqgrid/js/jquery.jqGrid.min.js" type="text/javascript"></script> 
  105.     <script src="../../lib/js/themes/jquery-ui.custom.min.js" type="text/javascript"></script>
  106. </head>
  107. <body>
  108.  
  109.     <div style="margin:10px">
  110.     <?php echo $out?>
  111.     </div> 
  112.  
  113.     <script>
  114.     function grid_load()
  115.     {
  116.         var grid = $('#list1');
  117.         var rowids = grid.getDataIDs();
  118.         var columnModels = grid.getGridParam().colModel;
  119.  
  120.         // check each visible row
  121.         for (var i = 0; i < rowids.length; i++)
  122.         {
  123.             var rowid = rowids[i];
  124.             var data = grid.getRowData(rowid);
  125.  
  126.             if (data.name.indexOf("Ana") != -1) // show only edit, no delete
  127.             {    
  128.                 jQuery("tr#"+rowid+" td[aria-describedby$='_act'] span:first").html(jQuery("tr#"+rowid+" td[aria-describedby$='_act']").find("a:first"));
  129.             }
  130.             else if (data.gender == 'male') // view only
  131.             {    
  132.                 jQuery("tr#"+rowid).addClass("not-editable-row");
  133.                 jQuery("tr#"+rowid+" td[aria-describedby$='_act']").html("-");
  134.             }
  135.            
  136.         }
  137.        
  138.         // for multiselect check all, list1 is grid id
  139.         $("#cb_list1").click(function(){
  140.            
  141.             var selr_one = grid.getGridParam('selrow');
  142.             var selr = [];
  143.             selr = grid.jqGrid('getGridParam','selarrrow'); // array of id's of the selected rows when multiselect options is true. Empty array if not selection
  144.             if (selr.length < 2 && selr_one)
  145.                 selr[0] = selr_one;
  146.                
  147.             for (var x=0;x < selr.length;x++)
  148.             {
  149.                 rowid = selr[x];
  150.                 var data = grid.getRowData(rowid);
  151.  
  152.                 if (data.name.indexOf("Ana") != -1) // show only edit, no delete
  153.                 {
  154.                     jQuery("#del_list1").addClass("ui-state-disabled");
  155.                 }
  156.                 else if (data.gender == 'male') // view only
  157.                 {    
  158.                     jQuery("#edit_list1").addClass("ui-state-disabled");
  159.                     jQuery("#del_list1").addClass("ui-state-disabled");
  160.                 }
  161.             }
  162.         });
  163.     }
  164.  
  165.     function grid_select(id)
  166.     {
  167.         var grid = $('#list1');
  168.        
  169.         var rowid = grid.getGridParam('selrow');
  170.         if (rowid)
  171.         {
  172.             var data = grid.getRowData(rowid);
  173.             if (data.name.indexOf("Ana") != -1) // show only edit, no delete
  174.             {
  175.                 jQuery("#del_list1").addClass("ui-state-disabled");
  176.                 jQuery("#edit_list1").removeClass("ui-state-disabled");
  177.             }
  178.             else if (data.gender == 'male') // view only
  179.             {    
  180.                 jQuery("#edit_list1").addClass("ui-state-disabled");
  181.                 jQuery("#del_list1").addClass("ui-state-disabled");
  182.             }
  183.             else
  184.             {
  185.                 jQuery("#edit_list1").removeClass("ui-state-disabled");
  186.                 jQuery("#del_list1").removeClass("ui-state-disabled");
  187.             }
  188.         }
  189.         // for multiselect
  190.         var rowids = grid.getGridParam('selarrrow');
  191.         if (rowids.length > 1)
  192.         {
  193.             for (var x=0;x < rowids.length;x++)
  194.             {
  195.                 rowid = rowids[x];
  196.                 var data = grid.getRowData(rowid);
  197.  
  198.                 if (data.name.indexOf("Ana") != -1) // show only edit, no delete
  199.                 {
  200.                     jQuery("#del_list1").addClass("ui-state-disabled");
  201.                 }
  202.                 else if (data.gender == 'male') // view only
  203.                 {    
  204.                     jQuery("#del_list1").addClass("ui-state-disabled");
  205.                 }
  206.             }          
  207.         }      
  208.     }
  209.    
  210.     // readonly gender conditional function - when return true, field will be readonly
  211.     function check_client(formid)
  212.     {
  213.         client_id = jQuery("input[name=client_id]:last, select[name=client_id]:last",formid).val();
  214.         client_id = parseInt(client_id);
  215.        
  216.         if (jQuery.inArray(client_id,[3,6,7,8,9]) != -1)
  217.             return true;
  218.     }
  219.     </script>
  220. </body>
  221. </html>
RAW Paste Data