3 * jqGrid extension for manipulating Grid Data
\r
4 * Tony Tomov tony@trirand.com
\r
5 * http://trirand.com/blog/
\r
6 * Dual licensed under the MIT and GPL licenses:
\r
7 * http://www.opensource.org/licenses/mit-license.php
\r
8 * http://www.gnu.org/licenses/gpl.html
\r
12 editRow : function(rowid,keys,oneditfunc,succesfunc, url, extraparam, aftersavefunc,errorfunc) {
\r
13 return this.each(function(){
\r
14 var $t = this, nm, tmp, editable, cnt=0, focus=null, svr=[];
\r
15 if (!$t.grid ) { return; }
\r
17 if( !$t.p.multiselect ) {
\r
18 editable = $("#"+rowid,$t.grid.bDiv).attr("editable") || "0";
\r
19 if (editable == "0") {
\r
20 $('#'+rowid+' td',$t.grid.bDiv).each( function(i) {
\r
21 nm = $t.p.colModel[i].name;
\r
22 hc = $t.p.colModel[i].hidden===true ? true : false;
\r
23 tmp = $(this).html().replace(/\ \;/ig,'');
\r
25 if ( nm !== 'cb' && nm !== 'subgrid' && $t.p.colModel[i].editable===true && !hc) {
\r
26 if(focus===null) { focus = i; }
\r
28 var opt = $.extend($t.p.colModel[i].editoptions || {} ,{id:rowid+"_"+nm,name:nm});
\r
29 if(!$t.p.colModel[i].edittype) { $t.p.colModel[i].edittype = "text"; }
\r
30 var elc = createEl($t.p.colModel[i].edittype,opt,tmp,$(this));
\r
31 $(elc).addClass("editable");
\r
32 $(this).append(elc);
\r
34 if($t.p.colModel[i].edittype == "select" && $t.p.colModel[i].editoptions.multiple===true && $.browser.msie) {
\r
35 $(elc).width($(elc).width());
\r
41 svr['id'] = rowid; $t.p.savedRow.push(svr);
\r
42 $('#'+rowid,$t.grid.bDiv).attr("editable","1");
\r
43 $('#'+rowid+" td:eq("+focus+") input",$t.grid.bDiv).focus();
\r
45 $('#'+rowid,$t.grid.bDiv).bind("keydown",function(e) {
\r
46 if (e.keyCode === 27) { $($t).restoreRow(rowid); }
\r
47 if (e.keyCode === 13) {
\r
48 $($t).saveRow(rowid,succesfunc, url, extraparam, aftersavefunc,errorfunc);
\r
50 e.stopPropagation();
\r
53 if( typeof oneditfunc === "function") { oneditfunc(rowid); }
\r
59 saveRow : function(rowid, succesfunc, url, extraparam, aftersavefunc,errorfunc) {
\r
60 return this.each(function(){
\r
61 var $t = this, nm, tmp={}, tmp2, editable, fr;
\r
62 if (!$t.grid ) { return; }
\r
63 editable = $('#'+rowid,$t.grid.bDiv).attr("editable");
\r
64 url = url ? url : $t.p.editurl;
\r
65 if (editable==="1" && url) {
\r
66 $('#'+rowid+" td",$t.grid.bDiv).each(function(i) {
\r
67 nm = $t.p.colModel[i].name;
\r
68 if ( nm !== 'cb' && nm !== 'subgrid' && $t.p.colModel[i].editable===true) {
\r
69 if( $t.p.colModel[i].hidden===true) { tmp[nm] = $(this).html(); }
\r
71 switch ($t.p.colModel[i].edittype) {
\r
73 tmp[nm]= $("input",this).attr("checked") ? 1 : 0;
\r
77 tmp[nm]= $("input",this).val();
\r
80 tmp[nm]= $("textarea",this).val();
\r
83 if(!$t.p.colModel[i].editoptions.multiple) {
\r
84 tmp[nm] = $("select>option:selected",this).val();
\r
86 var sel = $("select",this);
\r
87 tmp[nm] = $(sel).val();
\r
94 if(tmp) { tmp["id"] = rowid; if(extraparam) { $.extend(tmp,extraparam);} }
\r
95 if(!$t.grid.hDiv.loading) {
\r
96 $t.grid.hDiv.loading = true;
\r
97 $("div.loading",$t.grid.hDiv).fadeIn("fast");
\r
101 complete: function(res,stat){
\r
102 if (stat === "success"){
\r
104 if( typeof succesfunc === "function") { ret = succesfunc(res); }
\r
107 $('#'+rowid+" td",$t.grid.bDiv).each(function(i) {
\r
108 nm = $t.p.colModel[i].name;
\r
109 if ( nm !== 'cb' && nm !== 'subgrid' && $t.p.colModel[i].editable===true) {
\r
110 switch ($t.p.colModel[i].edittype) {
\r
112 if(!$t.p.colModel[i].editoptions.multiple) {
\r
113 tmp2 = $("select>option:selected", this).text();
\r
114 } else if( $t.p.colModel[i].editoptions.multiple ===true) {
\r
115 var selectedText = [];
\r
116 $("select > option:selected",this).each(
\r
117 function(i,selected){
\r
118 selectedText[i] = $(selected).text();
\r
121 tmp2= selectedText.join(",");
\r
125 var cbv = $t.p.colModel[i].editoptions.value.split(":") || ["Yes","No"];
\r
126 tmp2 = $("input",this).attr("checked") ? cbv[0] : cbv[1];
\r
131 tmp2 = $("input, textarea", this).val();
\r
135 $(this).html(tmp2 || " ");
\r
138 $('#'+rowid,$t.grid.bDiv).attr("editable","0");
\r
139 for( var k=0;k<$t.p.savedRow.length;k++) {
\r
140 if( $t.p.savedRow[k].id===rowid) {fr = k; break;}
\r
142 if(fr >= 0) { $t.p.savedRow.splice(fr,1); }
\r
143 if( typeof aftersavefunc === "function") { aftersavefunc(rowid,res.responseText); }
\r
144 } else { $($t).restoreRow(rowid); }
\r
147 error:function(res,stat){
\r
148 if(typeof errorfunc == "function") {
\r
149 errorfunc(res,stat)
\r
151 alert("Error Row: "+rowid+" Result: " +res.status+":"+res.statusText+" Status: "+stat);
\r
155 $t.grid.hDiv.loading = false;
\r
156 $("div.loading",$t.grid.hDiv).fadeOut("fast");
\r
157 $("#"+rowid,$t.grid.bDiv).unbind("keydown");
\r
162 restoreRow : function(rowid) {
\r
163 return this.each(function(){
\r
164 var $t= this, nm, fr;
\r
165 if (!$t.grid ) { return; }
\r
166 for( var k=0;k<$t.p.savedRow.length;k++) {
\r
167 if( $t.p.savedRow[k].id===rowid) {fr = k; break;}
\r
170 $('#'+rowid+" td",$t.grid.bDiv).each(function(i) {
\r
171 nm = $t.p.colModel[i].name;
\r
172 if ( nm !== 'cb' && nm !== 'subgrid') {
\r
174 $(this).html($t.p.savedRow[fr][nm] || " ");
\r
177 $('#'+rowid,$t.grid.bDiv).attr("editable","0");
\r
178 $t.p.savedRow.splice(fr,1);
\r