123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- /**
- * jqGrid extension for constructing Grid Data from external file
- * Tony Tomov tony@trirand.com, http://trirand.com/blog/
- * Dual licensed under the MIT and GPL licenses:
- * http://www.opensource.org/licenses/mit-license.php
- * http://www.gnu.org/licenses/gpl-2.0.html
- **/
- /*jshint eqeqeq:false, eqnull:true, devel:true */
- /*global jQuery, define, xmlJsonClass, exports, module, require */
- /*jslint browser: true, devel: true, white: true */
- (function (global, factory) {
- "use strict";
- if (typeof define === "function" && define.amd) {
- // AMD. Register as an anonymous module.
- define([
- "jquery",
- "./grid.base",
- "./jsonxml"
- ], function ($) {
- return factory($, global);
- });
- } else if (typeof module === "object" && module.exports) {
- // Node/CommonJS
- module.exports = function (root, $) {
- if (!root) {
- root = window;
- }
- if ($ === undefined) {
- // require("jquery") returns a factory that requires window to
- // build a jQuery instance, we normalize how we use modules
- // that require this pattern but the window provided is a noop
- // if it's defined (how jquery works)
- $ = typeof window !== "undefined" ?
- require("jquery") :
- require("jquery")(root);
- }
- require("./grid.base");
- require("./jsonxml");
- factory($, root);
- return $;
- };
- } else {
- // Browser globals
- factory(jQuery, global);
- }
- }(typeof window !== "undefined" ? window : this, function ($, window) {
- "use strict";
- var jgrid = $.jgrid;
- // begin module grid.import
- $.jgrid.extend({
- jqGridImport: function (o) {
- o = $.extend({
- imptype: "xml", // xml, json, xmlstring, jsonstring
- impstring: "",
- impurl: "",
- mtype: "GET",
- impData: {},
- xmlGrid: {
- config: "roots>grid",
- data: "roots>rows"
- },
- jsonGrid: {
- config: "grid",
- data: "data"
- },
- ajaxOptions: {}
- }, o || {});
- return this.each(function () {
- var $t = this,
- xmlConvert = function (xml, options) {
- var cnfg = $(options.xmlGrid.config, xml)[0], xmldata = $(options.xmlGrid.data, xml)[0], jstr, jstr1, key, svdatatype;
- if (xmlJsonClass.xml2json) {
- jstr = xmlJsonClass.xml2json(cnfg, " ");
- jstr = $.parseJSON(jstr);
- for (key in jstr) {
- if (jstr.hasOwnProperty(key)) {
- jstr1 = jstr[key];
- }
- }
- if (jstr1 !== undefined) {
- if (xmldata) {
- // save the datatype
- svdatatype = jstr.grid.datatype;
- jstr.grid.datatype = "xmlstring";
- jstr.grid.datastr = xml;
- $($t).jqGrid(jstr1).jqGrid("setGridParam", { datatype: svdatatype });
- } else {
- $($t).jqGrid(jstr1);
- }
- }
- } else {
- (jgrid.defaults != null && $.isFunction(jgrid.defaults.fatalError) ? jgrid.defaults.fatalError : alert)("xml2json or parse are not present");
- }
- },
- jsonConvert = function (jsonstr, options) {
- if (jsonstr && typeof jsonstr === "string") {
- var json = $.parseJSON(jsonstr),
- gprm = json[options.jsonGrid.config],
- jdata = json[options.jsonGrid.data], svdatatype;
- if (jdata) {
- svdatatype = gprm.datatype;
- gprm.datatype = "jsonstring";
- gprm.datastr = jdata;
- $($t).jqGrid(gprm).jqGrid("setGridParam", { datatype: svdatatype });
- } else {
- $($t).jqGrid(gprm);
- }
- }
- },
- xmld;
- switch (o.imptype) {
- case "xml":
- $.ajax($.extend({
- url: o.impurl,
- type: o.mtype,
- data: o.impData,
- dataType: "xml",
- context: o,
- complete: function (jqXHR) {
- if ((jqXHR.status < 300 || jqXHR.status === 304) && (jqXHR.status !== 0 || jqXHR.readyState !== 4)) {
- xmlConvert(jqXHR.responseXML, this);
- $($t).triggerHandler("jqGridImportComplete", [jqXHR, this]);
- if ($.isFunction(this.importComplete)) {
- this.importComplete(jqXHR);
- }
- }
- }
- }, o.ajaxOptions));
- break;
- case "xmlstring":
- // we need to make just the conversion and use the same code as xml
- if (o.impstring && typeof o.impstring === "string") {
- xmld = $.parseXML(o.impstring);
- if (xmld) {
- xmlConvert(xmld, o);
- $($t).triggerHandler("jqGridImportComplete", [xmld, o]);
- if ($.isFunction(o.importComplete)) {
- o.importComplete(xmld);
- }
- o.impstring = null;
- }
- }
- break;
- case "json":
- $.ajax($.extend({
- url: o.impurl,
- type: o.mtype,
- data: o.impData,
- dataType: "json",
- context: o,
- complete: function (jqXHR) {
- try {
- if ((jqXHR.status < 300 || jqXHR.status === 304) && (jqXHR.status !== 0 || jqXHR.readyState !== 4)) {
- jsonConvert(jqXHR.responseText, this);
- $($t).triggerHandler("jqGridImportComplete", [jqXHR, this]);
- if ($.isFunction(this.importComplete)) {
- this.importComplete(jqXHR);
- }
- }
- } catch (ignore) { }
- }
- }, o.ajaxOptions));
- break;
- case "jsonstring":
- if (o.impstring && typeof o.impstring === "string") {
- jsonConvert(o.impstring, o);
- $($t).triggerHandler("jqGridImportComplete", [o.impstring, o]);
- if ($.isFunction(o.importComplete)) {
- o.importComplete(o.impstring);
- }
- o.impstring = null;
- }
- break;
- }
- });
- },
- jqGridExport: function (o) {
- o = $.extend({
- exptype: "xmlstring",
- root: "grid",
- ident: "\t"
- }, o || {});
- var ret = null;
- this.each(function () {
- if (!this.grid) {
- return;
- }
- var key, gprm = $.extend(true, {}, $(this).jqGrid("getGridParam"));
- // we need to check for:
- // 1.multiselect, 2.subgrid 3. treegrid and remove the unneded columns from colNames
- if (gprm.rownumbers) {
- gprm.colNames.splice(0, 1);
- gprm.colModel.splice(0, 1);
- }
- if (gprm.multiselect) {
- gprm.colNames.splice(0, 1);
- gprm.colModel.splice(0, 1);
- }
- if (gprm.subGrid) {
- gprm.colNames.splice(0, 1);
- gprm.colModel.splice(0, 1);
- }
- gprm.knv = null;
- if (gprm.treeGrid) {
- for (key in gprm.treeReader) {
- if (gprm.treeReader.hasOwnProperty(key)) {
- gprm.colNames.splice(gprm.colNames.length - 1);
- gprm.colModel.splice(gprm.colModel.length - 1);
- }
- }
- }
- switch (o.exptype) {
- case "xmlstring":
- ret = "<" + o.root + ">" + xmlJsonClass.json2xml(gprm, o.ident) + "</" + o.root + ">";
- break;
- case "jsonstring":
- ret = "{" + xmlJsonClass.toJson(gprm, o.root, o.ident, false) + "}";
- if (gprm.postData.filters !== undefined) {
- ret = ret.replace(/filters":"/, "filters\":");
- ret = ret.replace(/\}\]\}"/, "}]}");
- }
- break;
- }
- });
- return ret;
- },
- excelExport: function (o) {
- o = $.extend({
- exptype: "remote",
- url: null,
- oper: "oper",
- tag: "excel",
- exportOptions: {}
- }, o || {});
- return this.each(function () {
- var pdata;
- if (!this.grid) {
- return;
- }
- if (o.exptype === "remote") {
- pdata = $.extend({}, this.p.postData, o.exportOptions);
- pdata[o.oper] = o.tag;
- window.location = o.url + (o.url != null && o.url.indexOf("?") >= 0 ? "&" : "?") + $.param(pdata);
- }
- });
- }
- });
- // end module grid.import
- }));
|