Easyui Form增加myLoad方法,使其支持二级数据对象

时间:2024-10-21 10:08:14

$.extend($.fn.form.methods, {
myLoad : function (jq, param) {
return jq.each(function () {
load(this, param);
});

function load(target, param) {
if (!$.data(target, "form")) {
$.data(target, "form", {
options : $.extend({}, $.fn.form.defaults)
});
}
var options = $.data(target, "form").options;
if (typeof param == "string") {
var params = {};
if (options.onBeforeLoad.call(target, params) == false) {
return;
}
$.ajax({
url : param,
data : params,
dataType : "json",
success : function (rsp) {
loadData(rsp);
},
error : function () {
options.onLoadError.apply(target, arguments);
}
});
} else {
loadData(param);
}
function loadData(dd) {
var form = $(target);
var formFields = form.find("input[name],select[name],textarea[name]");
formFields.each(function(){
var name = this.name;
var value = jQuery.proxy(function(){try{return eval('this.'+name);}catch(e){return "";}},dd)();
var rr = setNormalVal(name,value);
if (!rr.length) {
var f = form.find("input[numberboxName=\"" + name + "\"]");
if (f.length) {
f.numberbox("setValue", value);
} else {
$("input[name=\"" + name + "\"]", form).val(value);
$("textarea[name=\"" + name + "\"]", form).val(value);
$("select[name=\"" + name + "\"]", form).val(value);
}
}
setPlugsVal(name,value);
});
options.onLoadSuccess.call(target, dd);
$(target).form("validate");
};
function setNormalVal(key, val) {
var rr = $(target).find("input[name=\"" + key + "\"][type=radio], input[name=\"" + key + "\"][type=checkbox]");
rr._propAttr("checked", false);
rr.each(function () {
var f = $(this);
if (f.val() == String(val) || $.inArray(f.val(), val) >= 0) {
f._propAttr("checked", true);
}
});
return rr;
};
function setPlugsVal(key, val) {
var form = $(target);
var cc = ["combobox", "combotree", "combogrid", "datetimebox", "datebox", "combo"];
var c = form.find("[comboName=\"" + key + "\"]");
if (c.length) {
for (var i = 0; i < cc.length; i++) {
var combo = cc[i];
if (c.hasClass(combo + "-f")) {
if (c[combo]("options").multiple) {
c[combo]("setValues", val);
} else {
c[combo]("setValue", val);
}
return;
}
}
}
};
};
}
});