var ActiveCurdObject = null; var CurdObj = function() { this.list_table_id = ""; //单行数据信息 this.Row = null; //添加从表数据时,设置主表数据。此数据可能会在从表添加界面上被使用到 this.MasterData = null; this.opt = { //模块名称/文件夹名称 "module_name": "", "columns": [], //新增编辑窗口默认大小 "window_size": ['600px', '350px'], "window_title": ['新增/编辑窗口标题', '详情窗口标题'], "targetEle": {}, //名称列名 "nameColKey": "name", "idColKey": "", "height": "", //默认的数据table ID "table_datalist_control": "table_datalist_control", //默认的数据table工具栏ID "table_tools_bar": "table_tools_bar", //默认的数据table查询条件元素ID "table_search_panel": "table_search_panel", //默认的数据table操作模板ID "table_row_operator": "table_row_operator", "apiUrl": { "save_url": "", "read_url": "", "delete_url": "", "list_url": "" } }; this.Init = function(opt) { var thisObj = this; $.extend(this.opt, opt); this.list_table_id = this.opt.list_table_id || "t_" + new Date().getTime(); if (this.opt.module_name == "") { layer.msg("未正确配置模块名称参数:module_name"); return; } if ($("#" + this.opt.module_name).length != 1) { layer.msg("未正确设置模块" + this.opt.module_name + "对应的主控件!"); return; } this.opt.targetEle.list_data = "#" + this.opt.module_name + " #" + this.opt.table_datalist_control; if ($(this.opt.targetEle.list_data).length != 1) { layer.msg("未正确设置数据列表显示控件!"); return; } this.opt.targetEle.list_toolbar = "#" + this.opt.module_name + " #" + this.opt.table_tools_bar; var toolbar = $(this.opt.targetEle.list_toolbar); if (toolbar.length != 1) { this.opt.targetEle.list_toolbar = ""; } this.opt.targetEle.list_action = "#" + this.opt.module_name + " #" + this.opt.table_row_operator; if ($(this.opt.targetEle.list_action).length != 1) { this.opt.targetEle.list_action = ""; } this.opt.targetEle.list_search = "#" + this.opt.module_name + " #" + this.opt.table_search_panel; if ($(this.opt.targetEle.list_search).length != 1) { this.opt.targetEle.list_search = ""; } //绑定操作事件 layui.table.on('toolbar(' + $(this.opt.targetEle.list_data).attr('lay-filter') + ')', function(obj) { var data = obj.data; var layEvent = obj.event; if (layEvent == "add") { thisObj.New(thisObj.opt.window_title.length > 0 ? thisObj.opt.window_title[0] : ""); } }); layui.table.on('tool(' + $(this.opt.targetEle.list_data).attr('lay-filter') + ')', function(obj) { var data = obj.data; var layEvent = obj.event; var Id = thisObj.opt.idColKey == "" ? (data["id"] || data["ID"] || data["Id"] || data["iD"]) : data[thisObj.opt.idColKey]; if (layEvent === 'delete') { layer.confirm('
是否删除' + data[thisObj.opt.nameColKey] + "的数据记录?
", function(index) { obj.del(); thisObj.Delete(Id); }); } else if (layEvent === 'edit') { thisObj.Edit(thisObj.opt.window_title.length > 0 ? thisObj.opt.window_title[0] : "", Id); } else if (layEvent === "detail") { thisObj.Detail(thisObj.opt.window_title.length > 1 ? thisObj.opt.window_title[1] : "", Id); } else { //判断是否定义了对应的处理事件 if (layEvent != "") { eval(layEvent + "(data)"); } } }); }; this.New = function(title, pageurl, callback) { ActiveCurdObject = this; this.Row = null; if (pageurl == null || pageurl == "") pageurl = '/static/module/' + this.opt.module_name + '/add.html'; var curdObj = this; layui.use(['layer', 'form'], function() { layui.layer.open({ "type": 2, "resize": false, "scrollbar": false, "title": title == null ? "新增" : "新增" + title, "area": curdObj.opt.window_size, "content": pageurl }); if (callback) callback(); }); }; this.Edit = function(title, id, pageurl, callback) { if (id == null || id == "") { layer.msg("操作的数据编号不能为空!"); return; } ActiveCurdObject = this; this.Row = { "id": id }; if (pageurl == null || pageurl == "") pageurl = '/static/module/' + this.opt.module_name + '/add.html'; var curdObj = this; layui.use(['layer', 'form'], function() { layui.layer.open({ "type": 2, "resize": false, "scrollbar": false, "title": title == null ? "编辑信息" : "编辑" + title, "area": curdObj.opt.window_size, "content": pageurl }); if (callback) callback(); }); }; this.One = function(id, callback) { var thisObj = this; var paramlist = {}; if (thisObj.opt.idColKey != "") paramlist[thisObj.opt.idColKey] = id; else paramlist["id"] = id; //获取单行数据 $.getJSON(Global.AccessUrl + this.opt.apiUrl.read_url, paramlist, function(result) { thisObj.Row = null; if ((result.code == 0 || result.returncode == 200) && result.data != null && result.data.length > 0) { thisObj.Row = result.data[0]; } if (callback) callback(result); }) }; this.Detail = function(title, Id, pageurl, callback) { if (Id == null || Id == "") { layer.msg("操作的数据编号不能为空!"); return; } ActiveCurdObject = this; this.Row = { "id": Id }; if (pageurl == null || pageurl == "") pageurl = '/static/module/' + this.opt.module_name + '/detail.html'; var curdObj = this; layui.use(['layer', 'form'], function() { layui.layer.open({ type: 2, maxmin: true, resize: false, scrollbar: true, title: [title == null ? "详细信息" : title + "详细信息", "font-weight:700;color:red;"], area: curdObj.opt.window_size, content: pageurl, success: function(layero, index) { if (callback != null) callback(); //layer.full(index); //layero.find(".layui-layer-maxmin").remove(); //去年恢复按钮 } }); }); }; this.Save = function(parameter, callback) { var thisObj = this; layer.msg("正在提交数据中...", { time: 0 }); $.post(Global.AccessUrl + this.opt.apiUrl.save_url, parameter, function(returnData) { if (returnData.code == 0 || returnData.returncode == 200) { layer.msg("数据保存成功"); thisObj.RefrshZtree(null); if (callback != null) callback(); parent.layer.closeAll(); } else { layer.alert(returnData["msg"]); return; } }); }; this.Delete = function(Id, callback) { ActiveCurdObject = this; var thisObj = this; $.post(Global.AccessUrl + this.opt.apiUrl.delete_url, { "id": Id }, function(returnData) { if (returnData["code"] == 1 || returnData.returncode == 500) { layer.alert(returnData["msg"]); } else { layer.msg("数据删除成功"); thisObj.Reset(); thisObj.RemoveZtreeNode(null, Id); if (callback != null) callback(); } }); }; this.Reset = function() { ActiveCurdObject = this; if ($.trim(this.opt.targetEle.list_search) != "") { $(this.opt.targetEle.list_search).find("input,select").each(function() { var eleId = $.trim($(this).attr("id")); if (eleId != "") $(this).val(""); }); layui.form.render("select"); } layui.table.reload(this.list_table_id, { page: { pageIndex: 1, }, where: "" }); }; this.Query = function() { ActiveCurdObject = this; var whereCondition = {}; if ($.trim(this.opt.targetEle.list_search) != "") { $(this.opt.targetEle.list_search).find("input,select").each(function() { var eleId = $.trim($(this).attr("id")); var v = $.trim($(this).val()); if (eleId != "" && v != "") whereCondition[eleId] = v; }); } //执行重载 layui.table.reload(this.list_table_id, { page: { pageIndex: 1, }, where: whereCondition }); }; this.GetList = function(param, callback) { this._list("GET", param, callback); }; this.PostList = function(param, callback) { this._list("POST", param, callback); }; this.List = function(param, callback) { this.GetList(param, callback); }; this._list = function(method, param, callback) { var _url = Global.AccessUrl + this.opt.apiUrl.list_url; if (param != null) { var _tmpParamStr = []; for (var key in param) { if (key == "height") continue; _tmpParamStr.push(key + "=" + param[key]); } if (_tmpParamStr.length > 0) { if (_url.indexOf("?") == -1) _url = _url + "?" + _tmpParamStr.join("&"); else _url = _url + "&" + _tmpParamStr.join("&"); } } var parameter = { elem: this.opt.targetEle.list_data, url: _url, defaultToolbar: [], toolbar: this.opt.targetEle.list_toolbar, id: this.list_table_id, cols: this.opt.columns, method: method, done: function(res, curr, count) { if (callback != null) callback(res, curr, count); } } if (this.opt.height != null || this.opt.height != "") parameter["height"] = this.opt.height; Tools.BindTable(parameter); } this.RefrshZtree = function(treeId, nodeId, nodename) { ActiveCurdObject = this; if (Global.Ztree.zTreeObj == null) return; if (treeId != null && treeId != "") treeObj = $.fn.zTree.getZTreeObj(treeId); else treeObj = Global.Ztree.zTreeObj; //更新树节点 var nodes = treeObj.getSelectedNodes(); if (nodes.length > 0) { if (nodeId != null && nodeId != "" && nodes[0].id != nodeId) { var tNode = treeObj.getNodeByParam("id", nodeId, null); if (tNode != null && nodename != null) { //修改记录 tNode.title = nodename; } treeObj.updateNode(tNode, true); } else { if (!nodes[0].isParent) { //没有子节点的节点处理 nodes[0].isParent = true; treeObj.updateNode(nodes[0], true); } treeObj.reAsyncChildNodes(nodes[0], "refresh"); } } else { //更新树 treeObj.reAsyncChildNodes(null, "refresh"); } }; this.RemoveZtreeNode = function(treeId, nodeId) { ActiveCurdObject = this; if (Global.Ztree.zTreeObj == null) return; if (treeId != null && treeId != "") treeObj = $.fn.zTree.getZTreeObj(treeId); else treeObj = Global.Ztree.zTreeObj; //同步删除树节点 var tNode = treeObj.getNodeByParam("id", nodeId, null); if (tNode != null) { treeObj.removeNode(tNode, true); } }; } var Curd = { //控件标识。未设置时自动生成 list_table_id: "", opt: { //模块名称/文件夹名称 "module_name": "", "columns": [], //新增编辑窗口默认大小 "window_size": ['600px', '350px'], "targetEle": {}, //名称列名 "nameColKey": "name", "height": "", //默认的数据table ID "table_datalist_control": "table_datalist_control", //默认的数据table工具栏ID "table_tools_bar": "table_tools_bar", //默认的数据table查询条件元素ID "table_search_panel": "table_search_panel", //默认的数据table操作模板ID "table_row_operator": "table_row_operator" }, //单行数据信息 Row: null, //添加从表数据时,设置主表数据。此数据可能会在从表添加界面上被使用到 MasterData: null, Init: function(opt) { $.extend(this.opt, opt); this.list_table_id = this.opt.list_table_id || "t_" + new Date().getTime(); if (this.opt.module_name == "") { layer.msg("未正确配置模块名称参数:module_name"); return; } if ($("#" + this.opt.module_name).length != 1) { layer.msg("未正确设置模块" + this.opt.module_name + "对应的主控件!"); return; } this.opt.targetEle.list_data = "#" + this.opt.module_name + " #" + this.opt.table_datalist_control; if ($(this.opt.targetEle.list_data).length != 1) { layer.msg("未正确设置数据列表显示控件!"); return; } this.opt.targetEle.list_toolbar = "#" + this.opt.module_name + " #" + this.opt.table_tools_bar; if ($(this.opt.targetEle.list_toolbar).length != 1) { this.opt.targetEle.list_toolbar = ""; } this.opt.targetEle.list_action = "#" + this.opt.module_name + " #" + this.opt.table_row_operator; if ($(this.opt.targetEle.list_action).length != 1) { this.opt.targetEle.list_action = ""; } this.opt.targetEle.list_search = "#" + this.opt.module_name + " #" + this.opt.table_search_panel; if ($(this.opt.targetEle.list_search).length != 1) { this.opt.targetEle.list_search = ""; } layui.table.on('tool(' + $(this.opt.targetEle.list_data).attr('lay-filter') + ')', function(obj) { var data = obj.data; var layEvent = obj.event; var Id = data["id"] || data["ID"] || data["Id"] || data["iD"]; if (layEvent === 'delete') { layer.confirm('
是否删除' + data[Curd.opt.nameColKey] + "的数据记录?
", function(index) { obj.del(); Curd.Delete(Id); }); } else if (layEvent === 'edit') { Curd.Edit(Id); } else if (layEvent === "detail") { Curd.Detail(Id); } }); }, New: function(pageurl, callback) { this.Row = null; if (pageurl == null || pageurl == "") pageurl = '/static/module/' + this.opt.module_name + '/add.html'; var curdObj = this; layui.use(['layer', 'form'], function() { layui.layer.open({ type: 2, resize: false, scrollbar: false, title: "新增", area: curdObj.opt.window_size, content: pageurl }); if (callback) callback(); }); }, Edit: function(id, pageurl, callback) { if (id == null || id == "") { layer.msg("操作的数据编号不能为空!"); return; } this.Row = { "id": id }; if (pageurl == null || pageurl == "") pageurl = '/static/module/' + this.opt.module_name + '/add.html'; var curdObj = this; layui.use(['layer', 'form'], function() { layui.layer.open({ type: 2, resize: false, scrollbar: false, title: "编辑信息", area: curdObj.opt.window_size, content: pageurl }); if (callback) callback(); }); }, One: function(id, callback) { //获取单行数据 $.getJSON(Global.AccessUrl + this.opt.apiUrl.read_url, { "id": id }, function(result) { Curd.Row = null; if ((result.code == 0 || result.returncode == 200) && result.data != null && result.data.length > 0) { Curd.Row = result.data[0]; } if (callback) callback(result); }) }, Detail: function(Id, pageurl, callback) { if (Id == null || Id == "") { layer.msg("操作的数据编号不能为空!"); return; } this.Row = { "id": Id }; if (pageurl == null || pageurl == "") pageurl = '/static/module/' + this.opt.module_name + '/detail.html'; var curdObj = this; layui.use(['layer', 'form'], function() { layui.layer.open({ type: 2, maxmin: true, resize: false, scrollbar: true, title: ["详细信息", "font-weight:700;color:red;"], area: curdObj.opt.window_size, content: pageurl, success: function(layero, index) { if (callback != null) callback(); //layer.full(index); //layero.find(".layui-layer-maxmin").remove(); //去年恢复按钮 } }); }); }, Save: function(parameter, callback) { $.post(Global.AccessUrl + this.opt.apiUrl.save_url, parameter, function(returnData) { if (returnData.code == 0 || returnData.returncode == 200) { layer.msg("数据保存成功"); Curd.RefrshZtree(null); if (callback != null) callback(); parent.layer.closeAll(); } else { layer.alert(returnData["msg"]); return; } }); }, Delete: function(Id, callback) { $.post(Global.AccessUrl + this.opt.apiUrl.delete_url, { "id": Id }, function(returnData) { if (returnData["code"] == 1 || returnData.returncode == 500) { layer.alert(returnData["msg"]); } else { layer.msg("数据删除成功"); Curd.RemoveZtreeNode(null, Id); if (callback != null) callback(); } }); }, Reset: function() { if ($.trim(this.opt.targetEle.list_search) != "") { $(this.opt.targetEle.list_search).find("input,select").each(function() { var eleId = $.trim($(this).attr("id")); if (eleId != "") $(this).val(""); }); } layui.table.reload(this.list_table_id, { page: { pageIndex: 1, }, where: "" }); }, Query: function() { var whereCondition = {}; if ($.trim(this.opt.targetEle.list_search) != "") { $(this.opt.targetEle.list_search).find("input,select").each(function() { var eleId = $.trim($(this).attr("id")); var v = $.trim($(this).val()); if (eleId != "" && v != "") whereCondition[eleId] = v; }); } //执行重载 layui.table.reload(this.list_table_id, { page: { pageIndex: 1, }, where: whereCondition }); }, List: function(param, callback) { var _url = Global.AccessUrl + this.opt.apiUrl.list_url; if (param != null) { var _tmpParamStr = []; for (var key in param) { _tmpParamStr.push(key + "=" + param[key]); } if (_tmpParamStr.length > 0) { if (_url.indexOf("?") == -1) _url = _url + "?" + _tmpParamStr.join("&"); else _url = _url + "&" + _tmpParamStr.join("&"); } } var parameter = { elem: this.opt.targetEle.list_data, url: _url, toolbar: this.opt.targetEle.list_toolbar, id: this.list_table_id, cols: this.opt.columns, method: "GET", done: function(res, curr, count) { if (callback != null) callback(res, curr, count); } } if (this.opt.height != null || this.opt.height != "") parameter["height"] = this.opt.height; Tools.BindTable(parameter); }, RefrshZtree: function(treeId, nodeId, nodename) { if (Global.Ztree.zTreeObj == null) return; if (treeId != null && treeId != "") treeObj = $.fn.zTree.getZTreeObj(treeId); else treeObj = Global.Ztree.zTreeObj; //更新树节点 var nodes = treeObj.getSelectedNodes(); if (nodes.length > 0) { if (nodeId != null && nodeId != "" && nodes[0].id != nodeId) { var tNode = treeObj.getNodeByParam("id", nodeId, null); if (tNode != null && nodename != null) { //修改记录 tNode.title = nodename; } treeObj.updateNode(tNode, true); } else { if (!nodes[0].isParent) { //没有子节点的节点处理 nodes[0].isParent = true; treeObj.updateNode(nodes[0], true); } treeObj.reAsyncChildNodes(nodes[0], "refresh"); } } else { //更新树 treeObj.reAsyncChildNodes(null, "refresh"); } }, RemoveZtreeNode: function(treeId, nodeId) { if (Global.Ztree.zTreeObj == null) return; if (treeId != null && treeId != "") treeObj = $.fn.zTree.getZTreeObj(treeId); else treeObj = Global.Ztree.zTreeObj; //同步删除树节点 var tNode = treeObj.getNodeByParam("id", nodeId, null); if (tNode != null) { treeObj.removeNode(tNode, true); } } }