123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598 |
- 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('<div style="text-align:center;">是否删除<span style="color:red;font-weight:700;padding:0 2px;">' + data[thisObj.opt.nameColKey] + "</span>的数据记录?</div>", 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('<div style="text-align:center;">是否删除<span style="color:red;font-weight:700;padding:0 2px;">' + data[Curd.opt.nameColKey] + "</span>的数据记录?</div>", 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);
- }
- }
- }
|