非完善版:
(function ($) {
$.fn.editform = function (options, formdata) {if (typeof options == 'string') {
var method = $.fn.editform.methods[options];
if (method) {
return method(this, param);
}
}
// this.each(function () {
var op = $.extend({}, $.fn.editform.defaults, options);
var item;
if (op.formtype == "window") {
item = CreateWindow(formdata);
} else {
item = CreatePanel(formdata);
}
console.info($(item));
$(item).appendTo($(op.container));
// });
$.parser.parse($(op.container));
};
$.fn.editform.methods = {
"submit": function (jq, param) {
$.fn.editform.submit(param);
},
"clear": function (jq, param) {
$.fn.editform.clear(param);
}
}
$.fn.editform.submit = function (formname) {
$("#" + formname).submit();
};
$.fn.editform.clear = function (formname) {
$("#" + formname).form('clear');
};
$.fn.editform.defaults = {
container: 'body',
cols: 1,
formtype:'window',
};
function CreatePanel(formdata) {
var win = $("#" + formdata.FormName + "div");
if ($(win).length != 0) {
$(win).panel({});
return;
}
var itemwin = $('<div id="' + formdata.FormName + 'div" class="easyui-panel" data-options="fit:true,border:false"> </div>');
var form = CreateItemForm(formdata);
var buttonDiv = AddButonDiv(formdata);
$(form).appendTo(itemwin);
$(buttonDiv).appendTo(itemwin);
return itemwin;
};
function CreateWindow(formdata) {
var win = $("#" + formdata.FormName + "div");
if ($(win).length != 0) {
$(win).window({});
return;
}
var itemwin = $('<div id="' + formdata.FormName + 'div" class="easyui-window" data-options="iconCls:\'icon-save\',modal:true"> </div>');
var form = CreateItemForm(formdata);
var buttonDiv = AddButonDiv(formdata);
console.info($(form));
$(form).appendTo(itemwin);
$(buttonDiv).appendTo(itemwin);
return itemwin;
};
function CreateItemForm(formdata) {
var form = $('<form id="' + formdata.FormName + 'form" method="post"></form>');
var table = AddformItem(formdata);
$(table).appendTo(form);
return table;
};
function AddButonDiv(formdata) {
var buttondiv = $('<div style="text-align:center;padding:5px"></div>');
var submitbutton = $(' <a href="javascript:void(0)" class="easyui-linkbutton" οnclick="$.fn.editform.submit(\'' + formdata.FormName + 'form\')">Submit</a>');
var resetbutton = $('<a href="javascript:void(0)" class="easyui-linkbutton" οnclick="$.fn.editform.clear(\'' + formdata.FormName + 'form\')">Clear</a>');
submitbutton.appendTo(buttondiv);
resetbutton.appendTo(buttondiv);
return buttondiv;
}
function AddformItem (formdata) {
var cols = formdata.Items;
//创建table
var table = $('<table cellpadding="5"></table>');
var tr = $("<tr></tr>");
$(tr).appendTo(table);
for (var i = 0; i < cols.length; i++) {
if (i % 2 == 0) {
var tr = $("<tr></tr>");
$(tr).appendTo(table);
}
var obj = $('<input id="' + formdata.FormName + '.' + cols[i].ItemField + '" name="' + formdata.FormName + '.' + cols[i].ItemField + '" />');
switch (cols[i].ItemType) {
case "ValidateBox":
$(obj).addClass("easyui-validatebox");
break;
case "TextBox":
$(obj).addClass("easyui-textbox");
break;
case "DateBox":
$(obj).addClass("easyui-datebox");
break;
case "ComboBox":
$(obj).addClass("easyui-combobox");
break;
default:
break;
}
//data-options处理
var optionsArray = new Array();
var options = "";
if (cols[i].Required) {
optionsArray[optionsArray.length] = "required:true";
}
if (cols[i].ValidType != undefined && cols[i].ValidType != "") {
optionsArray[optionsArray.length] = "validType:" + cols[i].ValidType;
}
if (cols[i].URL != undefined && cols[i].URL != "") {
optionsArray[optionsArray.length] = "url:'" + cols[i].URL + "'";
}
if (cols[i].ValueField != undefined && cols[i].ValueField != "") {
optionsArray[optionsArray.length] = "valueField:'" + cols[i].ValueField + "'";
}
if (cols[i].TextField != undefined && cols[i].TextField != "") {
optionsArray[optionsArray.length] = "textField:'" + cols[i].TextField + "'";
}
if (cols[i].Data != undefined && cols[i].Data != "") {
optionsArray[optionsArray.length] = "data:" + cols[i].Data;
}
if (optionsArray.length > 0) {
$(obj).attr("data-options", optionsArray.join(","));
}
var tdlable = $("<td>" + cols[i].ItemName + ":</td>");
$(tdlable).appendTo(tr);
var td = $("<td></td>");
$(obj).appendTo(td);
$(td).appendTo(tr);
}
return table;
//增加提交按钮
// var formname = "ff";
// var form = CreateForm(formname);
// $(table).appendTo(container);
// $(form).appendTo(itemwin);
//添加进容器
};
})(jQuery);