关于今天easyui的坑 DateGrid兼容$ref问题,版本easyui 1.2.2

总结了一下,我从开始接触easyui每天都在爬坑,今天也不例外,今天这个主要是把easyui的js修改了一下,所以记录一下,省的忘记

今天遇到了一个问题,就是我后台红的hibernate 以及alibaba的json做的json返回,用过的人都知道这会出现$ref错误,

需要用前端的一个js来反编译一下

一下是前端的反编译代码,原文忘记是在哪位前端大神那里找来的了以下是js

var FastJson = {
		isArray : function(a) {
			return "object" == typeof a
					&& "[object array]" == Object.prototype.toString.call(a)
							.toLowerCase();
		},
		isObject : function(a) {
			return "object" == typeof a
					&& "[object object]" == Object.prototype.toString.call(a)
							.toLowerCase();
		},
		format : function(a) {
			if (null == a)
				return null;
			"string" == typeof a && (a = eval("(" + a + ")"));
			return this._format(a, a, null, null, null);
		},
		_randomId : function() {
			return "randomId_" + parseInt(1E9 * Math.random());
		},
		_getJsonValue : function(a, c) {
			var d = this._randomId(), b;
			b = "" + ("function " + d + "(root){") + ("return root." + c + ";");
			b += "}";
			b += "";
			var e = document.createElement("script");
			e.id = d;
			e.text = b;
			document.body.appendChild(e);
			d = window[d](a);
			e.parentNode.removeChild(e);
			return d
		},
		_format : function(a, c, d, b, e) {
			d || (d = "");
			if (this.isObject(c)) {
				if (c.$ref) {
					var g = c.$ref;
					0 == g.indexOf("$.")
							&& (b[e] = this._getJsonValue(a, g.substring(2)));
					return
				}
				for ( var f in c)
					b = d, "" != b && (b += "."), g = c[f], b += f, this
							._format(a, g, b, c, f);
			} else if (this.isArray(c))
				for (f in c)
					b = d, g = c[f], b = b + "[" + f + "]", this._format(a, g,
							b, c, f);
			return a
		}
	};
我开始是想找找easyui DateGrid有没有在接受数据之后就执行的方法,然后我找了一下api发现有,可是可能是版本太低的原因,并没有起作用,所以

我是把这段代码粘贴到了jquery.easyui.min.1.2.2.js里面,放到最下面就行了

然后需要在js里面找到$.fn.datagrid.defaults开头的一个datagrid默认属性的构造器

添加一个新的属性,我的叫isFastJson

然后在$.fn.datagrid.parseOptions开头的配置文件读取方法中加上读取你的配置的方法

//替换属性
							isFastJson:(t.attr("isFastJson") ? t
									.attr("isFastJson") == "true" : undefined),

大概就是这个样子,然后还需要最后一步,就是当dataGrid得到返回的数据时判断是不是需要格式化

function _482() {
			$.ajax({
				type : opts.method,
				url : opts.url,
				data : _480,
				dataType : "json",
				success : function(data) {
					if(opts.isFastJson)
						FastJson.format(data);
					setTimeout(function() {
						_483();
					}, 0);
					_3f6(_47d, data);
					setTimeout(function() {
						_46f(_47d);
					}, 0);
				},
				error : function() {
					setTimeout(function() {
						_483();
					}, 0);
					if (opts.onLoadError) {
						opts.onLoadError.apply(_47d, arguments);
					}
				}
			});
		}

可能因为版本不一样所以会有所差异,但是应该也是可以找到的

然后在dataGrid生成时加上该属性就行。需要格式化赋值true 不需要时赋值false

datagrid({
							title:'统计列表' ,
							fit : false,
							dataType : 'json',
							striped : true, //奇偶行是否区分
							singleSelect : true, //单选模式
							rownumbers : true, //行号
							isFastJson : true,
							loadMsg: '数据正在加载,请耐心的等待...' ,

至此,$ref问题已经解决了,可能高版本就可以通过别的方法来结局了,这个版本我没有找到拦截数据的地方,如果有人知道,欢迎指导



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值