Here is an example to demonstrate how we can enable inline editing in jqGrid. This example is a mixture of ‘Costom Edit’, ‘Using Events’ and ‘Input Types’ examples from jqGrid Demo examples from Mark Williams in MVC (model/view/controller) flavor using PHP Framework CodeIgniter.
You can using any language you want, you just have to do the language specific changes of course.
I have demonstrated how to integrate jqGrid with your code in my last post integrate jqGrid with codeigniter.., please check it first if you are looking for integrating jqGrid on your webpage, the template view and controller/model structures would be the same.
Here I’m using a different table to show the different input types working..
Following is the SQL dump of the table being used.
1 2 3 4 5 6 7 8 9 10 11 12 | CREATE TABLE `stetbl` ( CREATE TABLE `ebc_group` ( `id` int(11) NOT NULL auto_increment, `name` varchar(255) default NULL, `category` int(11) default NULL, `description` text, `owner` int(11) default NULL, `visibility` enum('All','Selected') default 'All', `created` datetime default NULL, `approved` enum('Y','N') default 'Y', PRIMARY KEY (`id`) ) |
The front end controller function
1 2 3 4 5 6 7 | function inline_edit_test() { $data['title'] = "Inline Edit Demo jqGrid | {$this->app_name}"; $data['main'] = 'edit_test'; $this->load->vars($data); $this->load->view('admin_template'); } |
the view to hold the javascript grid code, grid table and pagination div here is edit_test.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | <? $ci =& get_instance(); $base_url = base_url(); ?> <table id="list1"></table> <!--Grid table--> <div id="pager1"></div> <!--Pagination div--> <script type="text/javascript"> var lastsel2; //save row ID of the last row selected jQuery().ready(function (){ jQuery("#list1").jqGrid({ url:'<?=$base_url.'inline_edit_test'?>', //Backend controller function generating grid XML editurl: '<?=$base_url.'edit_test_save'?>', //Backend controller function save inline updates mtype : "post", //Ajax request type. It also could be GET datatype: "xml", //supported formats XML, JSON or Arrray colNames:['id','Group Title','Category','Group Description','Group Owner','Created','Visibility','publish','Action'], //Grid column headings colModel:[ {name:'id',index:'id', width:35, align:"left", editable:true, sorttype:'int', editable: true, editrules: { edithidden: true }, hidden: true}, {name:'name',index:'name', width:35, align:"left", editable:true, editoptions:{size:'20',maxlength:'255'}}, {name:'category',index:'category', width:35, align:"left", search:false}, {name:'description',index:'description', width:75, align:"left", editable:true,edittype:'textarea', editoptions:{rows:'2',cols:'50'}}, {name:'owner',index:'owner', width:25, align:"left", search:false}, {name:'created',index:'created', width:20, align:"left", search:false}, {name:'visibility',index:'visibility', width:20, align:"left", editable:true,edittype:'select',editoptions:{value:'All:All;selected:Selected'}, search:false}, {name:'approved',index:'approved', width:15, align:"center", editable:true,edittype:'select',editoptions: {value:'Y:Publish;N:Ban'}, search:false}, {name:'act',index:'act', width:15, align:"center", editable:false, search:false}, ], rowNum:100, autowidth: true, height: 300, rowList:[100,200,300,500,800,1000], pager: '#pager1', toolbar: [true,"top"], sortname: 'created', viewrecords: true, gridComplete: function(){ var ids = jQuery("#list1").getDataIDs(); for(var i=0;i < ids.length;i++){ var cl = ids[i]; se = "<input type='button' value='Save' onclick=\"jQuery('#list1').saveRow('"+cl+"',checksave);\" />"; //checksave is a callback function to show the response of inline save controller function ce = "<input type='button' value='C' title='Cancel' onclick=\"jQuery('#list1').restoreRow('"+cl+"');\" />"; jQuery("#list1").setRowData(ids[i],{act:se+ce}); //Save and Cancel buttons inserted via jqGrid setRowData function } }, onSelectRow: function(id){ if(id && id!==lastsel2){ jQuery('#list1').restoreRow(lastsel2); //restore last grid row jQuery('#list1').editRow(id,true); //show form elements for the row selected to enable updates lastsel2=id; //save current row ID so that when focus is gone it can be restored } }, caption:" Inline Edit Example" }).navGrid('#pager1',{edit:false,add:false,del:false}); function checksave(result) { if(result.responseText!='') alert(result.responseText); else $("#list1").trigger("reloadGrid"); } |
The backend controller function to generate XML feed would be the same as in my last post, except the required changes as we are using a different table here..
And now we need to implement the backend controller function edit_test_save to save the changes as we described in the jqGrid script before. It’s again is pretty simple..
1 2 3 4 5 6 7 8 9 | function edit_test_save() { if(isset($_POST)) { //model function updateRow writes and executes an UPDATE query with the data supplied $r = $this->DB_Func->updateRow('ebc_group',$_POST,array('id'=>$_POST['id'])); //$_POST contains all the editable data when we hit the save button in the grid echo $r; } } |
Here the POST array generated by the grid we are passing in the model function updateRow
1 2 3 4 5 | id => 1 name => test group description => test group test group test group test group test group test group test grou... visibility => All approved => Y |
Again DB_Func here is a model containing a number of SQL query builder functions and updateRow is onw of them. It’s nothing special..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | function updateRow($table,$data,$where=null) { $sql = "UPDATE `$table` SET"; foreach($data as $key => $value) { $sql .= " `$key`='".mysql_escape_string($value)."',"; } $sql = substr($sql,0,-1); if($where!=null) { $sql .= " WHERE"; foreach($where as $key => $value) { $sql .= " `$key`='".mysql_escape_string($value)."' AND"; } $sql = substr($sql,0,-4); } mysql_query($sql); if(mysql_error()) { return mysql_error()."<br>$sql"; } return ''; } |
Image may be NSFW.
Clik here to view.
Please feel free to comment and also if you have any suggestions, I’m always ready to learn Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.