jQuery的LG的自定义插件。

代码如下:
【jQuery的LG的自定义插件。】(function ($)
{


//全局系统对象
window['LG'] = {};


LG.cookies = (function ()
{
var fn = function ()
{
};
fn.prototype.get = function (name)
{
var cookieValuehttps://www.it610.com/article/= "";
var search = name + "=";
if (document.cookie.length > 0)
{
offset = document.cookie.indexOf(search);
if (offset != -1)
{
offset += search.length;
end = document.cookie.indexOf("; ", offset);
if (end == -1) end = document.cookie.length;
cookieValue = https://www.it610.com/article/decodeURIComponent(document.cookie.substring(offset, end))
}
}
return cookieValue;
};
fn.prototype.set = function (cookieName, cookieValue, DayValue)
{
var expire = "";
var day_value = https://www.it610.com/article/1;
if (DayValue != null)
{
day_value = https://www.it610.com/article/DayValue;
}
expire = new Date((new Date()).getTime() + day_value * 86400000);
expire = "; expires=" + expire.toGMTString();
document.cookie = cookieName + "=" + encodeURIComponent(cookieValue) + "; path=/" + expire;
}
fn.prototype.remvoe = function (cookieName)
{
var expire = "";
expire = new Date((new Date()).getTime() - 1);
expire = "; expires=" + expire.toGMTString();
document.cookie = cookieName + "=" + escape("") + "; path=/" + expire;
/*path=/*/
};


return new fn();
})();


//右下角的提示框
LG.tip = function (message) {
if (LG.wintip) {
LG.wintip.set('content', message);
LG.wintip.show();
} else {
LG.wintip = $.ligerDialog.tip({ content: message });
}
setTimeout(function () {
LG.wintip.hide()
}, 5000);
};


//预加载图片
LG.prevLoadImage = function (rootpath, paths) {
for (var i in paths) {
$('jQuery的LG的自定义插件。
文章图片
');
}
//绿色皮肤
if (p.green)
{
btn.removeClass("button2");
}
if (p.width)
{
btn.width(p.width);
}
if (p.click)
{
btn.click(p.click);
}
if (p.text)
{
$("span", btn).html(p.text);
}
if (typeof (p.appendTo) == "string") p.appendTo = $(p.appendTo);
btn.appendTo(p.appendTo);
};


//创建过滤规则(查询表单)
LG.bulidFilterGroup = function (form) {
if (!form) return null;
var group = { op: "and", rules: [] };
$(":input", form).not(":submit, :reset, :image,:button, [disabled]")
.each(function () {
if (!this.name) return;
if (!$(this).hasClass("field")) return;
if ($(this).val() == null || $(this).val() == "") return;
var ltype = $(this).attr("ltype");
var optionsJSON = $(this).attr("ligerui"), options;
if (optionsJSON) {
options = JSON2.parse(optionsJSON);
}
var op = $(this).attr("op") || "like";
//get the value type(number or date)
var type = $(this).attr("vt") || "string";
var value = https://www.it610.com/article/$(this).val();
var name = this.name;
//如果是下拉框,那么读取下拉框关联的隐藏控件的值(ID值,常用与外表关联)
if (ltype == "select" && options && options.valueFieldID) {
value = https://www.it610.com/article/$("#" + options.valueFieldID).val();
name = options.valueFieldID;
}
group.rules.push({
op: op,
field: name,
value: value,
type: type
});
});
return group;
};


//附加表单搜索按钮:搜索、高级搜索
LG.appendSearchButtons = function (form, grid) {
if (!form) return;
form = $(form);
//搜索按钮 附加到第一个li高级搜索按钮附加到 第二个li
var container = $('

').appendTo(form);
LG.addSearchButtons(form, grid, container.find("li:eq(0)"), container.find("li:eq(1)"));


};


//创建表单搜索按钮:搜索、高级搜索
LG.addSearchButtons = function (form, grid, btn1Container, btn2Container) {
if (!form) return;
if (btn1Container) {
LG.createButton({
appendTo: btn1Container,
text: '搜索',
click: function () {
var rule = LG.bulidFilterGroup(form);
if (rule.rules.length)
{
grid.set('parms', { where: JSON2.stringify(rule) });
} else
{
grid.set('parms', {});
}
grid.loadData();
}
});
}
if (btn2Container) {
LG.createButton({
appendTo: btn2Container,
width: 80,
text: '高级搜索',
click: function ()
{
grid.showFilter();
}
});
}
};


//快速设置表单底部默认的按钮:保存、取消
LG.setFormDefaultBtn = function (cancleCallback, savedCallback) {
//表单底部按钮
var buttons = [];
if (cancleCallback)
{
buttons.push({ text: '取消', onclick: cancleCallback });
}
if (savedCallback)
{
buttons.push({ text: '保存', onclick: savedCallback });
}
LG.addFormButtons(buttons);
};


//增加表单底部按钮,比如:保存、取消
LG.addFormButtons = function (buttons)
{
if (!buttons) return;
var formbar = $("body > div.form-bar");
if (formbar.length == 0)
formbar = $('').appendTo('body');
if (!(buttons instanceof Array))
{
buttons = [buttons];
}
$(buttons).each(function (i, o)
{
var btn = $(' ');
$("div.l-dialog-btn-inner:first", btn).html(o.text || "BUTTON");
if (o.onclick)
{
btn.bind('click', function ()
{
o.onclick(o);
});
}
if (o.width)
{
btn.width(o.width);
}
$("> div:first", formbar).append(btn);
});
};


//填充表单数据
LG.loadForm = function (mainform, options, callback)
{
options = options || {};
if (!mainform)
mainform = $("form:first");
var p = $.extend({
beforeSend: function ()
{
LG.showLoading('正在加载表单数据中...');
},
complete: function ()
{
LG.hideLoading();
},
success: function (data)
{
var preID = options.preID || "";
//根据返回的属性名,找到相应ID的表单元素,并赋值
for (var p in data)
{
var ele = $("[name=" + (preID + p) + "]", mainform);
//针对复选框和单选框 处理
if (ele.is(":checkbox,:radio"))
{
ele[0].checked = data[p] ? true : false;
}
else
{
ele.val(data[p]);
}
}
//下面是更新表单的样式
var managers = $.ligerui.find($.ligerui.controls.Input);
for (var i = 0, l = managers.length; i < l; i++)
{
//改变了表单的值,需要调用这个方法来更新ligerui样式
var o = managers[i];
o.updateStyle();
if (managers[i] instanceof $.ligerui.controls.TextBox)
o.checkValue();
}
if (callback)
callback(data);
},
error: function (message)
{
LG.showError('数据加载失败!
错误信息:' + message);
}
}, options);
LG.ajax(p);
};


//带验证、带loading的提交
LG.submitForm = function (mainform, success, error, data) {
if (!mainform)
mainform = $("form:first");
if (mainform.valid()) {
mainform.ajaxSubmit({
dataType: 'json',
success: success,
data: data,
beforeSubmit: function (formData, jqForm, options){
//针对复选框和单选框 处理
$(":checkbox,:radio", jqForm).each(function (){
if (!existInFormData(formData, this.name)){
formData.push({ name: this.name, type: this.type, value: this.checked });
}
});
for (var i = 0, l = formData.length; i < l; i++){
var o = formData[i];
if (o.type == "checkbox" || o.type == "radio"){
o.value = https://www.it610.com/article/$("[name=" + o.name + "]", jqForm)[0].checked ? "true" : "false";
}
}
},
beforeSend: function (a, b, c){
LG.showLoading('正在保存数据中...');
},
complete: function (result){
LG.hideLoading();
},
error: function (result){
LG.tip('发现系统错误
错误码:' + result.status);
}
});
}
else{
LG.showInvalid();
}
function existInFormData(formData, name){
for (var i = 0, l = formData.length; i < l; i++){
var o = formData[i];
if (o.name == name) return true;
}
return false;
}
};


//提示 验证错误信息
LG.showInvalid = function (validator) {
validator = validator || LG.validator;
if (!validator) return;
var message = '存在' + validator.errorList.length + '个字段验证不通过,请检查!';
//top.LG.tip(message);
$.ligerDialog.error(message);
};


//表单验证
LG.validate = function (form, options) {
if (typeof (form) == "string")
form = $(form);
else if (typeof (form) == "object" && form.NodeType == 1)
form = $(form);


options = $.extend({
errorPlacement: function (lable, element)
{
if (!element.attr("id"))
element.attr("id", new Date().getTime());
if (element.hasClass("l-textarea"))
{
element.addClass("l-textarea-invalid");
}
else if (element.hasClass("l-text-field"))
{
element.parent().addClass("l-text-invalid");
}
$(element).removeAttr("title").ligerHideTip();
$(element).attr("title", lable.html()).ligerTip({
distanceX: 5,
distanceY: -3,
auto: true
});
},
success: function (lable) {
if (!lable.attr("for")) return;
var element = $("#" + lable.attr("for"));


if (element.hasClass("l-textarea")) {
element.removeClass("l-textarea-invalid");
} else if (element.hasClass("l-text-field")) {
element.parent().removeClass("l-text-invalid");
}
$(element).removeAttr("title").ligerHideTip();
}
}, options || {});
LG.validator = form.validate(options);
return LG.validator;
};


LG.loadToolbar = function (grid, toolbarBtnItemClick) {
LG.ajax({
loading: '正在加载工具条中...',
type: 'AjaxSystem',
url: base + '/rbac/operate!toolBarButton.ajax',
data: { HttpContext: true, url: location.pathname },
success: function (data) {
if (!grid.toolbarManager) return;
if (!data || !data.length) return;
var items = [];
for (var i = 0, l = data.length; i < l; i++){
var o = data[i];
items[items.length] = {
click: toolbarBtnItemClick || LG.toolbarBtnItemClick,
text: o.name,
html: o.html,
img: base + "/" + o.btnIcon,
id: o.id,
url: o.url
};
items[items.length] = { line: true };
}
grid.toolbarManager.set('items', items);
}
});
};

LG.toolbarBtnItemClick = function(item) {
eval(item.html);
};

//关闭Tab项,如果tabid不指定,那么关闭当前显示的
LG.closeCurrentTab = function (tabid) {
if (!tabid) {
tabid = $("#framecenter > .l-tab-content > .l-tab-content-item:visible").attr("tabid");
}
if (tab) {
tab.removeTabItem(tabid);
}
};


//关闭Tab项并且刷新父窗口
LG.closeAndReloadParent = function (tabid, parentMenuNo) {
LG.closeCurrentTab(tabid);
var menuitem = $("#mainmenu ul.menulist li[menuno=" + parentMenuNo + "]");
var parentTabid = menuitem.attr("tabid");
var iframe = window.frames[parentTabid];
if (tab) {
tab.selectTabItem(parentTabid);
}
if (iframe && iframe.f_reload) {
iframe.f_reload();
} else if (tab) {
tab.reload(parentTabid);
}
};


//覆盖页面grid的loading效果
LG.overrideGridLoading = function () {
$.extend($.ligerDefaults.Grid, {
onloading: function () {
LG.showLoading('正在加载表格数据中...');
}, onloaded: function () {
LG.hideLoading();
}
});
};


//根据字段权限调整 页面配置
LG.adujestConfig = function (config, forbidFields) {
if (config.Form && config.Form.fields) {
for (var i = config.Form.fields.length - 1; i >= 0; i--) {
var field = config.Form.fields[i];
if ($.inArray(field.name, forbidFields) != -1)
config.Form.fields.splice(i, 1);
}
}
if (config.Grid && config.Grid.columns) {
for (var i = config.Grid.columns.length - 1; i >= 0; i--) {
var column = config.Grid.columns[i];
if ($.inArray(column.name, forbidFields) != -1)
config.Grid.columns.splice(i, 1);
}
}
if (config.Search && config.Search.fields)
{
for (var i = config.Search.fields.length - 1; i >= 0; i--)
{
var field = config.Search.fields[i];
if ($.inArray(field.name, forbidFields) != -1)
config.Search.fields.splice(i, 1);
}
}
};


//查找是否存在某一个按钮
LG.findToolbarItem = function (grid, itemID)
{
if (!grid.toolbarManager) return null;
if (!grid.toolbarManager.options.items) return null;
var items = grid.toolbarManager.options.items;
for (var i = 0, l = items.length; i < l; i++)
{
if (items[i].id == itemID) return items[i];
}
return null;
};




//设置grid的双击事件(带权限控制)
LG.setGridDoubleClick = function (grid, btnID, btnItemClick) {
//btnItemClick = btnItemClick || toolbarBtnItemClick;
//if (!btnItemClick) return;
grid.bind('dblClickRow', function (rowdata) {
var item = LG.findToolbarItem(grid, btnID);
if (!item) return;
grid.select(rowdata);
eval(item.html);
//btnItemClick(item);
});
};


//将选中的记录转换成ID数组
LG.getIds = function (selected){
var ids = new Array();
for (var i = 0; i< selected.length; i++){
ids.push(selected[i].id);
};
return ids;
};

LG.ajaxByJSON = function ajaxByJSON(_url,_data,_async, callBack, errorCallBack){
jQuery.ajax({
url: _url,
cache: false,
async: _async,
type: 'POST',
data: _data,
dataType: 'json',
error: function(){
if(errorCallBack){
errorCallBack();
}
alert("jQuery.ajax 执行失败!");
},
success: function(json){
if(callBack) callBack(json);
}
});
}

LG.search = function(grid, p){
p = $.extend({
beforeSearch: null, //搜索之前调用的函数
afterSearch: null //搜索之后调用的函数
}, p || {});
$("#search").click(function(){
var self = this;
this.p = p;
if ($.isFunction(self.p.beforeSearch)) {
self.p.beforeSearch();
}

var searchData = https://www.it610.com/article/getWarpPara("functions");
delete searchData[""];
var flag = false;
for (var key in searchData) {
if (searchData[key].trim() != "") {
flag = true;
break;
}
}
if (flag) {
$("#search_btn_reset").css("display", "inline");
grid.options.parms = searchData;
grid.loadData(true);
if ($.isFunction(self.p.afterSearch)) {
self.p.afterSearch();
}
}
});
$("#search_btn_reset").click(function(){
clearSearchWeiget("functions");
grid.options.parms = null;
grid.loadData(true);
$(this).css("display", "none");
});
};
})(jQuery);
//根据id清空搜索框
function clearSearchWeiget(id){
var inputs = $("#"+id).find("input[type!='button'], select");
for (var i = 0; i < inputs.size(); i++) {
var input = $(inputs[i]);
//如果是checkbox
if(input.attr("type") == "checkbox" && input.val() != ''){
input.attr("checked", false);
continue;
} else if(input.attr("type") == "radio"){
if (input.attr("checked")) {
input.attr("checked", false);
}
continue;
} else if (input.val() != '') {
input.val("");
continue;
}
}
};

    推荐阅读