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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/cuisongliu/article/details/49274867

之前因为要使用easyui的from控件load功能,从学习班看到代码,但是不能load到easyui的控件,修正版代码:

$.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 count = loadOther(name, value);
                        if (!count) {
                            $("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 loadOther(name, val){
                var count = 0;
                var pp = ['textbox','numberbox','slider'];
                for(var i=0; i<pp.length; i++){
                    var p = pp[i];
                    var f = $(target).find('input['+p+'Name="'+name+'"]');
                    if (f.length){
                        f[p]('setValue', val);
                        count += f.length;
                    }
                }
                return count;
            }
            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;
                        }
                    }
                }
            };
        };
    }
});

使用方法

$('#fromId').form('myLoad',{});
阅读更多

没有更多推荐了,返回首页