grid.inlinedit.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. /*jshint eqeqeq:false, eqnull:true, devel:true */
  2. /*global jQuery */
  3. (function($){
  4. /**
  5. * jqGrid extension for manipulating Grid Data
  6. * Tony Tomov tony@trirand.com
  7. * http://trirand.com/blog/
  8. * Dual licensed under the MIT and GPL licenses:
  9. * http://www.opensource.org/licenses/mit-license.php
  10. * http://www.gnu.org/licenses/gpl-2.0.html
  11. **/
  12. "use strict";
  13. $.jgrid.inlineEdit = $.jgrid.inlineEdit || {};
  14. $.jgrid.extend({
  15. //Editing
  16. editRow : function(rowid,keys,oneditfunc,successfunc, url, extraparam, aftersavefunc,errorfunc, afterrestorefunc) {
  17. // Compatible mode old versions
  18. var o={}, args = $.makeArray(arguments).slice(1);
  19. if( $.type(args[0]) === "object" ) {
  20. o = args[0];
  21. } else {
  22. if (keys !== undefined) { o.keys = keys; }
  23. if ($.isFunction(oneditfunc)) { o.oneditfunc = oneditfunc; }
  24. if ($.isFunction(successfunc)) { o.successfunc = successfunc; }
  25. if (url !== undefined) { o.url = url; }
  26. if (extraparam !== undefined) { o.extraparam = extraparam; }
  27. if ($.isFunction(aftersavefunc)) { o.aftersavefunc = aftersavefunc; }
  28. if ($.isFunction(errorfunc)) { o.errorfunc = errorfunc; }
  29. if ($.isFunction(afterrestorefunc)) { o.afterrestorefunc = afterrestorefunc; }
  30. // last two not as param, but as object (sorry)
  31. //if (restoreAfterError !== undefined) { o.restoreAfterError = restoreAfterError; }
  32. //if (mtype !== undefined) { o.mtype = mtype || "POST"; }
  33. }
  34. o = $.extend(true, {
  35. keys : false,
  36. oneditfunc: null,
  37. successfunc: null,
  38. url: null,
  39. extraparam: {},
  40. aftersavefunc: null,
  41. errorfunc: null,
  42. afterrestorefunc: null,
  43. restoreAfterError: true,
  44. mtype: "POST",
  45. focusField : true
  46. }, $.jgrid.inlineEdit, o );
  47. // End compatible
  48. return this.each(function(){
  49. var $t = this, nm, tmp, editable, cnt=0, focus=null, svr={}, ind,cm, bfer;
  50. if (!$t.grid ) { return; }
  51. ind = $($t).jqGrid("getInd",rowid,true);
  52. if( ind === false ) {return;}
  53. bfer = $.isFunction( o.beforeEditRow ) ? o.beforeEditRow.call($t,o, rowid) : undefined;
  54. if( bfer === undefined ) {
  55. bfer = true;
  56. }
  57. if(!bfer) { return; }
  58. editable = $(ind).attr("editable") || "0";
  59. if (editable === "0" && !$(ind).hasClass("not-editable-row")) {
  60. cm = $t.p.colModel;
  61. $('td[role="gridcell"]',ind).each( function(i) {
  62. nm = cm[i].name;
  63. var treeg = $t.p.treeGrid===true && nm === $t.p.ExpandColumn;
  64. if(treeg) { tmp = $("span:first",this).html();}
  65. else {
  66. try {
  67. tmp = $.unformat.call($t,this,{rowId:rowid, colModel:cm[i]},i);
  68. } catch (_) {
  69. tmp = ( cm[i].edittype && cm[i].edittype === 'textarea' ) ? $(this).text() : $(this).html();
  70. }
  71. }
  72. if ( nm !== 'cb' && nm !== 'subgrid' && nm !== 'rn') {
  73. if($t.p.autoencode) { tmp = $.jgrid.htmlDecode(tmp); }
  74. svr[nm]=tmp;
  75. if(cm[i].editable===true) {
  76. if(focus===null) { focus = i; }
  77. if (treeg) { $("span:first",this).html(""); }
  78. else { $(this).html(""); }
  79. var opt = $.extend({},cm[i].editoptions || {},{id:rowid+"_"+nm,name:nm,rowId:rowid});
  80. if(!cm[i].edittype) { cm[i].edittype = "text"; }
  81. if(tmp === " " || tmp === " " || (tmp.length===1 && tmp.charCodeAt(0)===160) ) {tmp='';}
  82. var elc = $.jgrid.createEl.call($t,cm[i].edittype,opt,tmp,true,$.extend({},$.jgrid.ajaxOptions,$t.p.ajaxSelectOptions || {}));
  83. $(elc).addClass("editable");
  84. if(treeg) { $("span:first",this).append(elc); }
  85. else { $(this).append(elc); }
  86. $.jgrid.bindEv.call($t, elc, opt);
  87. //Again IE
  88. if(cm[i].edittype === "select" && cm[i].editoptions!==undefined && cm[i].editoptions.multiple===true && cm[i].editoptions.dataUrl===undefined && $.jgrid.msie) {
  89. $(elc).width($(elc).width());
  90. }
  91. cnt++;
  92. }
  93. }
  94. });
  95. if(cnt > 0) {
  96. svr.id = rowid; $t.p.savedRow.push(svr);
  97. $(ind).attr("editable","1");
  98. if(o.focusField ) {
  99. if(typeof o.focusField === 'number' && parseInt(o.focusField,10) <= cm.length) {
  100. focus = o.focusField;
  101. }
  102. setTimeout(function(){
  103. var fe = $("td:eq("+focus+") :input:visible",ind).not(":disabled");
  104. if(fe.length > 0) {
  105. fe.focus();
  106. }
  107. },0);
  108. }
  109. if(o.keys===true) {
  110. $(ind).bind("keydown",function(e) {
  111. if (e.keyCode === 27) {
  112. $($t).jqGrid("restoreRow",rowid, o.afterrestorefunc);
  113. if($t.p._inlinenav) {
  114. try {
  115. $($t).jqGrid('showAddEditButtons');
  116. } catch (eer1) {}
  117. }
  118. return false;
  119. }
  120. if (e.keyCode === 13) {
  121. var ta = e.target;
  122. if(ta.tagName === 'TEXTAREA') { return true; }
  123. if( $($t).jqGrid("saveRow", rowid, o ) ) {
  124. if($t.p._inlinenav) {
  125. try {
  126. $($t).jqGrid('showAddEditButtons');
  127. } catch (eer2) {}
  128. }
  129. }
  130. return false;
  131. }
  132. });
  133. }
  134. $($t).triggerHandler("jqGridInlineEditRow", [rowid, o]);
  135. if( $.isFunction(o.oneditfunc)) { o.oneditfunc.call($t, rowid); }
  136. }
  137. }
  138. });
  139. },
  140. saveRow : function(rowid, successfunc, url, extraparam, aftersavefunc,errorfunc, afterrestorefunc) {
  141. // Compatible mode old versions
  142. var args = $.makeArray(arguments).slice(1), o = {};
  143. if( $.type(args[0]) === "object" ) {
  144. o = args[0];
  145. } else {
  146. if ($.isFunction(successfunc)) { o.successfunc = successfunc; }
  147. if (url !== undefined) { o.url = url; }
  148. if (extraparam !== undefined) { o.extraparam = extraparam; }
  149. if ($.isFunction(aftersavefunc)) { o.aftersavefunc = aftersavefunc; }
  150. if ($.isFunction(errorfunc)) { o.errorfunc = errorfunc; }
  151. if ($.isFunction(afterrestorefunc)) { o.afterrestorefunc = afterrestorefunc; }
  152. }
  153. o = $.extend(true, {
  154. successfunc: null,
  155. url: null,
  156. extraparam: {},
  157. aftersavefunc: null,
  158. errorfunc: null,
  159. afterrestorefunc: null,
  160. restoreAfterError: true,
  161. mtype: "POST",
  162. saveui : "enable",
  163. savetext : $.jgrid.defaults.savetext || "Saving..."
  164. }, $.jgrid.inlineEdit, o );
  165. // End compatible
  166. var success = false;
  167. var $t = this[0], nm, tmp={}, tmp2={}, tmp3= {}, editable, fr, cv, ind;
  168. if (!$t.grid ) { return success; }
  169. ind = $($t).jqGrid("getInd",rowid,true);
  170. if(ind === false) {return success;}
  171. var bfsr = $.isFunction( o.beforeSaveRow ) ? o.beforeSaveRow.call($t,o, rowid) : undefined;
  172. if( bfsr === undefined ) {
  173. bfsr = true;
  174. }
  175. if(!bfsr) { return; }
  176. editable = $(ind).attr("editable");
  177. o.url = o.url || $t.p.editurl;
  178. if (editable==="1") {
  179. var cm;
  180. $('td[role="gridcell"]',ind).each(function(i) {
  181. cm = $t.p.colModel[i];
  182. nm = cm.name;
  183. if ( nm !== 'cb' && nm !== 'subgrid' && cm.editable===true && nm !== 'rn' && !$(this).hasClass('not-editable-cell')) {
  184. switch (cm.edittype) {
  185. case "checkbox":
  186. var cbv = ["Yes","No"];
  187. if(cm.editoptions ) {
  188. cbv = cm.editoptions.value.split(":");
  189. }
  190. tmp[nm]= $("input",this).is(":checked") ? cbv[0] : cbv[1];
  191. break;
  192. case 'text':
  193. case 'password':
  194. case 'textarea':
  195. case "button" :
  196. tmp[nm]=$("input, textarea",this).val();
  197. break;
  198. case 'select':
  199. if(!cm.editoptions.multiple) {
  200. tmp[nm] = $("select option:selected",this).val();
  201. tmp2[nm] = $("select option:selected", this).text();
  202. } else {
  203. var sel = $("select",this), selectedText = [];
  204. tmp[nm] = $(sel).val();
  205. if(tmp[nm]) { tmp[nm]= tmp[nm].join(","); } else { tmp[nm] =""; }
  206. $("select option:selected",this).each(
  207. function(i,selected){
  208. selectedText[i] = $(selected).text();
  209. }
  210. );
  211. tmp2[nm] = selectedText.join(",");
  212. }
  213. if(cm.formatter && cm.formatter === 'select') { tmp2={}; }
  214. break;
  215. case 'custom' :
  216. try {
  217. if(cm.editoptions && $.isFunction(cm.editoptions.custom_value)) {
  218. tmp[nm] = cm.editoptions.custom_value.call($t, $(".customelement",this),'get');
  219. if (tmp[nm] === undefined) { throw "e2"; }
  220. } else { throw "e1"; }
  221. } catch (e) {
  222. if (e==="e1") { $.jgrid.info_dialog($.jgrid.errors.errcap,"function 'custom_value' "+$.jgrid.edit.msg.nodefined,$.jgrid.edit.bClose); }
  223. if (e==="e2") { $.jgrid.info_dialog($.jgrid.errors.errcap,"function 'custom_value' "+$.jgrid.edit.msg.novalue,$.jgrid.edit.bClose); }
  224. else { $.jgrid.info_dialog($.jgrid.errors.errcap,e.message,$.jgrid.edit.bClose); }
  225. }
  226. break;
  227. }
  228. cv = $.jgrid.checkValues.call($t,tmp[nm],i);
  229. if(cv[0] === false) {
  230. return false;
  231. }
  232. if($t.p.autoencode) { tmp[nm] = $.jgrid.htmlEncode(tmp[nm]); }
  233. if(o.url !== 'clientArray' && cm.editoptions && cm.editoptions.NullIfEmpty === true) {
  234. if(tmp[nm] === "") {
  235. tmp3[nm] = 'null';
  236. }
  237. }
  238. }
  239. });
  240. if (cv[0] === false){
  241. try {
  242. var tr = $($t).jqGrid('getGridRowById', rowid), positions = $.jgrid.findPos(tr);
  243. $.jgrid.info_dialog($.jgrid.errors.errcap,cv[1],$.jgrid.edit.bClose,{left:positions[0],top:positions[1]+$(tr).outerHeight()});
  244. } catch (e) {
  245. alert(cv[1]);
  246. }
  247. return success;
  248. }
  249. var idname, opers = $t.p.prmNames, oldRowId = rowid;
  250. if ($t.p.keyName === false) {
  251. idname = opers.id;
  252. } else {
  253. idname = $t.p.keyName;
  254. }
  255. if(tmp) {
  256. tmp[opers.oper] = opers.editoper;
  257. if (tmp[idname] === undefined || tmp[idname]==="") {
  258. tmp[idname] = rowid;
  259. } else if (ind.id !== $t.p.idPrefix + tmp[idname]) {
  260. // rename rowid
  261. var oldid = $.jgrid.stripPref($t.p.idPrefix, rowid);
  262. if ($t.p._index[oldid] !== undefined) {
  263. $t.p._index[tmp[idname]] = $t.p._index[oldid];
  264. delete $t.p._index[oldid];
  265. }
  266. rowid = $t.p.idPrefix + tmp[idname];
  267. $(ind).attr("id", rowid);
  268. if ($t.p.selrow === oldRowId) {
  269. $t.p.selrow = rowid;
  270. }
  271. if ($.isArray($t.p.selarrrow)) {
  272. var i = $.inArray(oldRowId, $t.p.selarrrow);
  273. if (i>=0) {
  274. $t.p.selarrrow[i] = rowid;
  275. }
  276. }
  277. if ($t.p.multiselect) {
  278. var newCboxId = "jqg_" + $t.p.id + "_" + rowid;
  279. $("input.cbox",ind)
  280. .attr("id", newCboxId)
  281. .attr("name", newCboxId);
  282. }
  283. // TODO: to test the case of frozen columns
  284. }
  285. if($t.p.inlineData === undefined) { $t.p.inlineData ={}; }
  286. tmp = $.extend({},tmp,$t.p.inlineData,o.extraparam);
  287. }
  288. if (o.url === 'clientArray') {
  289. tmp = $.extend({},tmp, tmp2);
  290. if($t.p.autoencode) {
  291. $.each(tmp,function(n,v){
  292. tmp[n] = $.jgrid.htmlDecode(v);
  293. });
  294. }
  295. var k, resp = $($t).jqGrid("setRowData",rowid,tmp);
  296. $(ind).attr("editable","0");
  297. for(k=0;k<$t.p.savedRow.length;k++) {
  298. if( String($t.p.savedRow[k].id) === String(oldRowId)) {fr = k; break;}
  299. }
  300. if(fr >= 0) { $t.p.savedRow.splice(fr,1); }
  301. $($t).triggerHandler("jqGridInlineAfterSaveRow", [rowid, resp, tmp, o]);
  302. if( $.isFunction(o.aftersavefunc) ) { o.aftersavefunc.call($t, rowid, resp, tmp, o); }
  303. success = true;
  304. $(ind).removeClass("jqgrid-new-row").unbind("keydown");
  305. } else {
  306. $($t).jqGrid("progressBar", {method:"show", loadtype : o.saveui, htmlcontent: o.savetext });
  307. tmp3 = $.extend({},tmp,tmp3);
  308. tmp3[idname] = $.jgrid.stripPref($t.p.idPrefix, tmp3[idname]);
  309. $.ajax($.extend({
  310. url:o.url,
  311. data: $.isFunction($t.p.serializeRowData) ? $t.p.serializeRowData.call($t, tmp3) : tmp3,
  312. type: o.mtype,
  313. async : false, //?!?
  314. complete: function(res,stat){
  315. $($t).jqGrid("progressBar", {method:"hide", loadtype : o.saveui, htmlcontent: o.savetext});
  316. if (stat === "success"){
  317. var ret = true, sucret, k;
  318. sucret = $($t).triggerHandler("jqGridInlineSuccessSaveRow", [res, rowid, o]);
  319. if (!$.isArray(sucret)) {sucret = [true, tmp];}
  320. if (sucret[0] && $.isFunction(o.successfunc)) {sucret = o.successfunc.call($t, res);}
  321. if($.isArray(sucret)) {
  322. // expect array - status, data, rowid
  323. ret = sucret[0];
  324. tmp = sucret[1] || tmp;
  325. } else {
  326. ret = sucret;
  327. }
  328. if (ret===true) {
  329. if($t.p.autoencode) {
  330. $.each(tmp,function(n,v){
  331. tmp[n] = $.jgrid.htmlDecode(v);
  332. });
  333. }
  334. tmp = $.extend({},tmp, tmp2);
  335. $($t).jqGrid("setRowData",rowid,tmp);
  336. $(ind).attr("editable","0");
  337. for(k=0;k<$t.p.savedRow.length;k++) {
  338. if( String($t.p.savedRow[k].id) === String(rowid)) {fr = k; break;}
  339. }
  340. if(fr >= 0) { $t.p.savedRow.splice(fr,1); }
  341. $($t).triggerHandler("jqGridInlineAfterSaveRow", [rowid, res, tmp, o]);
  342. if( $.isFunction(o.aftersavefunc) ) { o.aftersavefunc.call($t, rowid, res, tmp, o); }
  343. success = true;
  344. $(ind).removeClass("jqgrid-new-row").unbind("keydown");
  345. } else {
  346. $($t).triggerHandler("jqGridInlineErrorSaveRow", [rowid, res, stat, null, o]);
  347. if($.isFunction(o.errorfunc) ) {
  348. o.errorfunc.call($t, rowid, res, stat, null);
  349. }
  350. if(o.restoreAfterError === true) {
  351. $($t).jqGrid("restoreRow",rowid, o.afterrestorefunc);
  352. }
  353. }
  354. }
  355. },
  356. error:function(res,stat,err){
  357. $("#lui_"+$.jgrid.jqID($t.p.id)).hide();
  358. $($t).triggerHandler("jqGridInlineErrorSaveRow", [rowid, res, stat, err, o]);
  359. if($.isFunction(o.errorfunc) ) {
  360. o.errorfunc.call($t, rowid, res, stat, err);
  361. } else {
  362. var rT = res.responseText || res.statusText;
  363. try {
  364. $.jgrid.info_dialog($.jgrid.errors.errcap,'<div class="ui-state-error">'+ rT +'</div>', $.jgrid.edit.bClose,{buttonalign:'right'});
  365. } catch(e) {
  366. alert(rT);
  367. }
  368. }
  369. if(o.restoreAfterError === true) {
  370. $($t).jqGrid("restoreRow",rowid, o.afterrestorefunc);
  371. }
  372. }
  373. }, $.jgrid.ajaxOptions, $t.p.ajaxRowOptions || {}));
  374. }
  375. }
  376. return success;
  377. },
  378. restoreRow : function(rowid, afterrestorefunc) {
  379. // Compatible mode old versions
  380. var args = $.makeArray(arguments).slice(1), o={};
  381. if( $.type(args[0]) === "object" ) {
  382. o = args[0];
  383. } else {
  384. if ($.isFunction(afterrestorefunc)) { o.afterrestorefunc = afterrestorefunc; }
  385. }
  386. o = $.extend(true, {}, $.jgrid.inlineEdit, o );
  387. // End compatible
  388. return this.each(function(){
  389. var $t= this, fr=-1, ind, ares={}, k;
  390. if (!$t.grid ) { return; }
  391. ind = $($t).jqGrid("getInd",rowid,true);
  392. if(ind === false) {return;}
  393. var bfcr = $.isFunction( o.beforeCancelRow ) ? o.beforeCancelRow.call($t, o, rowid) : undefined;
  394. if( bfcr === undefined ) {
  395. bfcr = true;
  396. }
  397. if(!bfcr) { return; }
  398. for(k=0;k<$t.p.savedRow.length;k++) {
  399. if( String($t.p.savedRow[k].id) === String(rowid)) {fr = k; break;}
  400. }
  401. if(fr >= 0) {
  402. if($.isFunction($.fn.datepicker)) {
  403. try {
  404. $("input.hasDatepicker","#"+$.jgrid.jqID(ind.id)).datepicker('hide');
  405. } catch (e) {}
  406. }
  407. $.each($t.p.colModel, function(){
  408. if(this.editable === true && $t.p.savedRow[fr].hasOwnProperty(this.name)) {
  409. ares[this.name] = $t.p.savedRow[fr][this.name];
  410. }
  411. });
  412. $($t).jqGrid("setRowData",rowid,ares);
  413. $(ind).attr("editable","0").unbind("keydown");
  414. $t.p.savedRow.splice(fr,1);
  415. if($("#"+$.jgrid.jqID(rowid), "#"+$.jgrid.jqID($t.p.id)).hasClass("jqgrid-new-row")){
  416. setTimeout(function(){
  417. $($t).jqGrid("delRowData",rowid);
  418. $($t).jqGrid('showAddEditButtons');
  419. },0);
  420. }
  421. }
  422. $($t).triggerHandler("jqGridInlineAfterRestoreRow", [rowid]);
  423. if ($.isFunction(o.afterrestorefunc))
  424. {
  425. o.afterrestorefunc.call($t, rowid);
  426. }
  427. });
  428. },
  429. addRow : function ( p ) {
  430. p = $.extend(true, {
  431. rowID : null,
  432. initdata : {},
  433. position :"first",
  434. useDefValues : true,
  435. useFormatter : false,
  436. addRowParams : {extraparam:{}}
  437. },p || {});
  438. return this.each(function(){
  439. if (!this.grid ) { return; }
  440. var $t = this;
  441. var bfar = $.isFunction( p.beforeAddRow ) ? p.beforeAddRow.call($t,p.addRowParams) : undefined;
  442. if( bfar === undefined ) {
  443. bfar = true;
  444. }
  445. if(!bfar) { return; }
  446. p.rowID = $.isFunction(p.rowID) ? p.rowID.call($t, p) : ( (p.rowID != null) ? p.rowID : $.jgrid.randId());
  447. if(p.useDefValues === true) {
  448. $($t.p.colModel).each(function(){
  449. if( this.editoptions && this.editoptions.defaultValue ) {
  450. var opt = this.editoptions.defaultValue,
  451. tmp = $.isFunction(opt) ? opt.call($t) : opt;
  452. p.initdata[this.name] = tmp;
  453. }
  454. });
  455. }
  456. $($t).jqGrid('addRowData', p.rowID, p.initdata, p.position);
  457. p.rowID = $t.p.idPrefix + p.rowID;
  458. $("#"+$.jgrid.jqID(p.rowID), "#"+$.jgrid.jqID($t.p.id)).addClass("jqgrid-new-row");
  459. if(p.useFormatter) {
  460. $("#"+$.jgrid.jqID(p.rowID)+" .ui-inline-edit", "#"+$.jgrid.jqID($t.p.id)).click();
  461. } else {
  462. var opers = $t.p.prmNames,
  463. oper = opers.oper;
  464. p.addRowParams.extraparam[oper] = opers.addoper;
  465. $($t).jqGrid('editRow', p.rowID, p.addRowParams);
  466. $($t).jqGrid('setSelection', p.rowID);
  467. }
  468. });
  469. },
  470. inlineNav : function (elem, o) {
  471. o = $.extend(true,{
  472. edit: true,
  473. editicon: "ui-icon-pencil",
  474. add: true,
  475. addicon:"ui-icon-plus",
  476. save: true,
  477. saveicon:"ui-icon-disk",
  478. cancel: true,
  479. cancelicon:"ui-icon-cancel",
  480. addParams : {addRowParams: {extraparam: {}}},
  481. editParams : {},
  482. restoreAfterSelect : true
  483. }, $.jgrid.nav, o ||{});
  484. return this.each(function(){
  485. if (!this.grid ) { return; }
  486. var $t = this, onSelect, gID = $.jgrid.jqID($t.p.id);
  487. $t.p._inlinenav = true;
  488. // detect the formatactions column
  489. if(o.addParams.useFormatter === true) {
  490. var cm = $t.p.colModel,i;
  491. for (i = 0; i<cm.length; i++) {
  492. if(cm[i].formatter && cm[i].formatter === "actions" ) {
  493. if(cm[i].formatoptions) {
  494. var defaults = {
  495. keys:false,
  496. onEdit : null,
  497. onSuccess: null,
  498. afterSave:null,
  499. onError: null,
  500. afterRestore: null,
  501. extraparam: {},
  502. url: null
  503. },
  504. ap = $.extend( defaults, cm[i].formatoptions );
  505. o.addParams.addRowParams = {
  506. "keys" : ap.keys,
  507. "oneditfunc" : ap.onEdit,
  508. "successfunc" : ap.onSuccess,
  509. "url" : ap.url,
  510. "extraparam" : ap.extraparam,
  511. "aftersavefunc" : ap.afterSave,
  512. "errorfunc": ap.onError,
  513. "afterrestorefunc" : ap.afterRestore
  514. };
  515. }
  516. break;
  517. }
  518. }
  519. }
  520. if(o.add) {
  521. $($t).jqGrid('navButtonAdd', elem,{
  522. caption : o.addtext,
  523. title : o.addtitle,
  524. buttonicon : o.addicon,
  525. id : $t.p.id+"_iladd",
  526. onClickButton : function () {
  527. $($t).jqGrid('addRow', o.addParams);
  528. if(!o.addParams.useFormatter) {
  529. $("#"+gID+"_ilsave").removeClass('ui-state-disabled');
  530. $("#"+gID+"_ilcancel").removeClass('ui-state-disabled');
  531. $("#"+gID+"_iladd").addClass('ui-state-disabled');
  532. $("#"+gID+"_iledit").addClass('ui-state-disabled');
  533. }
  534. }
  535. });
  536. }
  537. if(o.edit) {
  538. $($t).jqGrid('navButtonAdd', elem,{
  539. caption : o.edittext,
  540. title : o.edittitle,
  541. buttonicon : o.editicon,
  542. id : $t.p.id+"_iledit",
  543. onClickButton : function () {
  544. var sr = $($t).jqGrid('getGridParam','selrow');
  545. if(sr) {
  546. $($t).jqGrid('editRow', sr, o.editParams);
  547. $("#"+gID+"_ilsave").removeClass('ui-state-disabled');
  548. $("#"+gID+"_ilcancel").removeClass('ui-state-disabled');
  549. $("#"+gID+"_iladd").addClass('ui-state-disabled');
  550. $("#"+gID+"_iledit").addClass('ui-state-disabled');
  551. } else {
  552. $.jgrid.viewModal("#alertmod",{gbox:"#gbox_"+gID,jqm:true});$("#jqg_alrt").focus();
  553. }
  554. }
  555. });
  556. }
  557. if(o.save) {
  558. $($t).jqGrid('navButtonAdd', elem,{
  559. caption : o.savetext || '',
  560. title : o.savetitle || 'Save row',
  561. buttonicon : o.saveicon,
  562. id : $t.p.id+"_ilsave",
  563. onClickButton : function () {
  564. var sr = $t.p.savedRow[0].id;
  565. if(sr) {
  566. var opers = $t.p.prmNames,
  567. oper = opers.oper, tmpParams = o.editParams;
  568. if($("#"+$.jgrid.jqID(sr), "#"+gID ).hasClass("jqgrid-new-row")) {
  569. o.addParams.addRowParams.extraparam[oper] = opers.addoper;
  570. tmpParams = o.addParams.addRowParams;
  571. } else {
  572. if(!o.editParams.extraparam) {
  573. o.editParams.extraparam = {};
  574. }
  575. o.editParams.extraparam[oper] = opers.editoper;
  576. }
  577. if( $($t).jqGrid('saveRow', sr, tmpParams) ) {
  578. $($t).jqGrid('showAddEditButtons');
  579. }
  580. } else {
  581. $.jgrid.viewModal("#alertmod",{gbox:"#gbox_"+gID,jqm:true});$("#jqg_alrt").focus();
  582. }
  583. }
  584. });
  585. $("#"+gID+"_ilsave").addClass('ui-state-disabled');
  586. }
  587. if(o.cancel) {
  588. $($t).jqGrid('navButtonAdd', elem,{
  589. caption : o.canceltext || '',
  590. title : o.canceltitle || 'Cancel row editing',
  591. buttonicon : o.cancelicon,
  592. id : $t.p.id+"_ilcancel",
  593. onClickButton : function () {
  594. var sr = $t.p.savedRow[0].id, cancelPrm = o.editParams;
  595. if(sr) {
  596. if($("#"+$.jgrid.jqID(sr), "#"+gID ).hasClass("jqgrid-new-row")) {
  597. cancelPrm = o.addParams.addRowParams;
  598. }
  599. $($t).jqGrid('restoreRow', sr, cancelPrm);
  600. $($t).jqGrid('showAddEditButtons');
  601. } else {
  602. $.jgrid.viewModal("#alertmod",{gbox:"#gbox_"+gID,jqm:true});$("#jqg_alrt").focus();
  603. }
  604. }
  605. });
  606. $("#"+gID+"_ilcancel").addClass('ui-state-disabled');
  607. }
  608. if(o.restoreAfterSelect === true) {
  609. if($.isFunction($t.p.beforeSelectRow)) {
  610. onSelect = $t.p.beforeSelectRow;
  611. } else {
  612. onSelect = false;
  613. }
  614. $t.p.beforeSelectRow = function(id, stat) {
  615. var ret = true;
  616. if($t.p.savedRow.length > 0 && $t.p._inlinenav===true && ( id !== $t.p.selrow && $t.p.selrow !==null) ) {
  617. if($t.p.selrow === o.addParams.rowID ) {
  618. $($t).jqGrid('delRowData', $t.p.selrow);
  619. } else {
  620. $($t).jqGrid('restoreRow', $t.p.selrow, o.editParams);
  621. }
  622. $($t).jqGrid('showAddEditButtons');
  623. }
  624. if(onSelect) {
  625. ret = onSelect.call($t, id, stat);
  626. }
  627. return ret;
  628. };
  629. }
  630. });
  631. },
  632. showAddEditButtons : function() {
  633. return this.each(function(){
  634. if (!this.grid ) { return; }
  635. var gID = $.jgrid.jqID(this.p.id);
  636. $("#"+gID+"_ilsave").addClass('ui-state-disabled');
  637. $("#"+gID+"_ilcancel").addClass('ui-state-disabled');
  638. $("#"+gID+"_iladd").removeClass('ui-state-disabled');
  639. $("#"+gID+"_iledit").removeClass('ui-state-disabled');
  640. });
  641. }
  642. //end inline edit
  643. });
  644. })(jQuery);