grid.import.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /**
  2. * jqGrid extension for constructing Grid Data from external file
  3. * Tony Tomov tony@trirand.com, http://trirand.com/blog/
  4. * Dual licensed under the MIT and GPL licenses:
  5. * http://www.opensource.org/licenses/mit-license.php
  6. * http://www.gnu.org/licenses/gpl-2.0.html
  7. **/
  8. /*jshint eqeqeq:false, eqnull:true, devel:true */
  9. /*global jQuery, define, xmlJsonClass, exports, module, require */
  10. /*jslint browser: true, devel: true, white: true */
  11. (function (global, factory) {
  12. "use strict";
  13. if (typeof define === "function" && define.amd) {
  14. // AMD. Register as an anonymous module.
  15. define([
  16. "jquery",
  17. "./grid.base",
  18. "./jsonxml"
  19. ], function ($) {
  20. return factory($, global);
  21. });
  22. } else if (typeof module === "object" && module.exports) {
  23. // Node/CommonJS
  24. module.exports = function (root, $) {
  25. if (!root) {
  26. root = window;
  27. }
  28. if ($ === undefined) {
  29. // require("jquery") returns a factory that requires window to
  30. // build a jQuery instance, we normalize how we use modules
  31. // that require this pattern but the window provided is a noop
  32. // if it's defined (how jquery works)
  33. $ = typeof window !== "undefined" ?
  34. require("jquery") :
  35. require("jquery")(root);
  36. }
  37. require("./grid.base");
  38. require("./jsonxml");
  39. factory($, root);
  40. return $;
  41. };
  42. } else {
  43. // Browser globals
  44. factory(jQuery, global);
  45. }
  46. }(typeof window !== "undefined" ? window : this, function ($, window) {
  47. "use strict";
  48. var jgrid = $.jgrid;
  49. // begin module grid.import
  50. $.jgrid.extend({
  51. jqGridImport: function (o) {
  52. o = $.extend({
  53. imptype: "xml", // xml, json, xmlstring, jsonstring
  54. impstring: "",
  55. impurl: "",
  56. mtype: "GET",
  57. impData: {},
  58. xmlGrid: {
  59. config: "roots>grid",
  60. data: "roots>rows"
  61. },
  62. jsonGrid: {
  63. config: "grid",
  64. data: "data"
  65. },
  66. ajaxOptions: {}
  67. }, o || {});
  68. return this.each(function () {
  69. var $t = this,
  70. xmlConvert = function (xml, options) {
  71. var cnfg = $(options.xmlGrid.config, xml)[0], xmldata = $(options.xmlGrid.data, xml)[0], jstr, jstr1, key, svdatatype;
  72. if (xmlJsonClass.xml2json) {
  73. jstr = xmlJsonClass.xml2json(cnfg, " ");
  74. jstr = $.parseJSON(jstr);
  75. for (key in jstr) {
  76. if (jstr.hasOwnProperty(key)) {
  77. jstr1 = jstr[key];
  78. }
  79. }
  80. if (jstr1 !== undefined) {
  81. if (xmldata) {
  82. // save the datatype
  83. svdatatype = jstr.grid.datatype;
  84. jstr.grid.datatype = "xmlstring";
  85. jstr.grid.datastr = xml;
  86. $($t).jqGrid(jstr1).jqGrid("setGridParam", { datatype: svdatatype });
  87. } else {
  88. $($t).jqGrid(jstr1);
  89. }
  90. }
  91. } else {
  92. (jgrid.defaults != null && $.isFunction(jgrid.defaults.fatalError) ? jgrid.defaults.fatalError : alert)("xml2json or parse are not present");
  93. }
  94. },
  95. jsonConvert = function (jsonstr, options) {
  96. if (jsonstr && typeof jsonstr === "string") {
  97. var json = $.parseJSON(jsonstr),
  98. gprm = json[options.jsonGrid.config],
  99. jdata = json[options.jsonGrid.data], svdatatype;
  100. if (jdata) {
  101. svdatatype = gprm.datatype;
  102. gprm.datatype = "jsonstring";
  103. gprm.datastr = jdata;
  104. $($t).jqGrid(gprm).jqGrid("setGridParam", { datatype: svdatatype });
  105. } else {
  106. $($t).jqGrid(gprm);
  107. }
  108. }
  109. },
  110. xmld;
  111. switch (o.imptype) {
  112. case "xml":
  113. $.ajax($.extend({
  114. url: o.impurl,
  115. type: o.mtype,
  116. data: o.impData,
  117. dataType: "xml",
  118. context: o,
  119. complete: function (jqXHR) {
  120. if ((jqXHR.status < 300 || jqXHR.status === 304) && (jqXHR.status !== 0 || jqXHR.readyState !== 4)) {
  121. xmlConvert(jqXHR.responseXML, this);
  122. $($t).triggerHandler("jqGridImportComplete", [jqXHR, this]);
  123. if ($.isFunction(this.importComplete)) {
  124. this.importComplete(jqXHR);
  125. }
  126. }
  127. }
  128. }, o.ajaxOptions));
  129. break;
  130. case "xmlstring":
  131. // we need to make just the conversion and use the same code as xml
  132. if (o.impstring && typeof o.impstring === "string") {
  133. xmld = $.parseXML(o.impstring);
  134. if (xmld) {
  135. xmlConvert(xmld, o);
  136. $($t).triggerHandler("jqGridImportComplete", [xmld, o]);
  137. if ($.isFunction(o.importComplete)) {
  138. o.importComplete(xmld);
  139. }
  140. o.impstring = null;
  141. }
  142. }
  143. break;
  144. case "json":
  145. $.ajax($.extend({
  146. url: o.impurl,
  147. type: o.mtype,
  148. data: o.impData,
  149. dataType: "json",
  150. context: o,
  151. complete: function (jqXHR) {
  152. try {
  153. if ((jqXHR.status < 300 || jqXHR.status === 304) && (jqXHR.status !== 0 || jqXHR.readyState !== 4)) {
  154. jsonConvert(jqXHR.responseText, this);
  155. $($t).triggerHandler("jqGridImportComplete", [jqXHR, this]);
  156. if ($.isFunction(this.importComplete)) {
  157. this.importComplete(jqXHR);
  158. }
  159. }
  160. } catch (ignore) { }
  161. }
  162. }, o.ajaxOptions));
  163. break;
  164. case "jsonstring":
  165. if (o.impstring && typeof o.impstring === "string") {
  166. jsonConvert(o.impstring, o);
  167. $($t).triggerHandler("jqGridImportComplete", [o.impstring, o]);
  168. if ($.isFunction(o.importComplete)) {
  169. o.importComplete(o.impstring);
  170. }
  171. o.impstring = null;
  172. }
  173. break;
  174. }
  175. });
  176. },
  177. jqGridExport: function (o) {
  178. o = $.extend({
  179. exptype: "xmlstring",
  180. root: "grid",
  181. ident: "\t"
  182. }, o || {});
  183. var ret = null;
  184. this.each(function () {
  185. if (!this.grid) {
  186. return;
  187. }
  188. var key, gprm = $.extend(true, {}, $(this).jqGrid("getGridParam"));
  189. // we need to check for:
  190. // 1.multiselect, 2.subgrid 3. treegrid and remove the unneded columns from colNames
  191. if (gprm.rownumbers) {
  192. gprm.colNames.splice(0, 1);
  193. gprm.colModel.splice(0, 1);
  194. }
  195. if (gprm.multiselect) {
  196. gprm.colNames.splice(0, 1);
  197. gprm.colModel.splice(0, 1);
  198. }
  199. if (gprm.subGrid) {
  200. gprm.colNames.splice(0, 1);
  201. gprm.colModel.splice(0, 1);
  202. }
  203. gprm.knv = null;
  204. if (gprm.treeGrid) {
  205. for (key in gprm.treeReader) {
  206. if (gprm.treeReader.hasOwnProperty(key)) {
  207. gprm.colNames.splice(gprm.colNames.length - 1);
  208. gprm.colModel.splice(gprm.colModel.length - 1);
  209. }
  210. }
  211. }
  212. switch (o.exptype) {
  213. case "xmlstring":
  214. ret = "<" + o.root + ">" + xmlJsonClass.json2xml(gprm, o.ident) + "</" + o.root + ">";
  215. break;
  216. case "jsonstring":
  217. ret = "{" + xmlJsonClass.toJson(gprm, o.root, o.ident, false) + "}";
  218. if (gprm.postData.filters !== undefined) {
  219. ret = ret.replace(/filters":"/, "filters\":");
  220. ret = ret.replace(/\}\]\}"/, "}]}");
  221. }
  222. break;
  223. }
  224. });
  225. return ret;
  226. },
  227. excelExport: function (o) {
  228. o = $.extend({
  229. exptype: "remote",
  230. url: null,
  231. oper: "oper",
  232. tag: "excel",
  233. exportOptions: {}
  234. }, o || {});
  235. return this.each(function () {
  236. var pdata;
  237. if (!this.grid) {
  238. return;
  239. }
  240. if (o.exptype === "remote") {
  241. pdata = $.extend({}, this.p.postData, o.exportOptions);
  242. pdata[o.oper] = o.tag;
  243. window.location = o.url + (o.url != null && o.url.indexOf("?") >= 0 ? "&" : "?") + $.param(pdata);
  244. }
  245. });
  246. }
  247. });
  248. // end module grid.import
  249. }));