-------------------------------------------------------------------前端js数据------------------------------------------------------------
layui.config({
base: "../layuimini-master/lib/layui-v2.5.4/lay/modules/" //模块的所在路径
}).use(['jquery', 'eleTree'], function () {
var $ = layui.jquery;
var eleTree = layui.eleTree;
var el2 = eleTree.render({
elem: '.ele1',
url: "/???/???",
method: "POST",
//data: data,
showCheckbox: true,
renderAfterExpand: false,
accordion: true,
lazy: false,
response: { //对后台返回的数据格式重新定义
statusName: "status",
statusCode: 0,
dataName: "data"
},
//request: { //对于后台数据重新定义名字
// name: "label",
// key: "id",
// children: "children",
// checked: "checked",
// disabled: "disabled",
// isLeaf: "pid"
//},
parseData: function (res) {
console.log(res);
}
});
$("#sava").on("click", function () {
if ($.trim($("#RoleName_addedit").val()) == "") {
$.tbmAlertWarning("请输入权限名称!");
return;
}
if ($.trim($("#issh").val()) == "") {
$.tbmAlertWarning("请选择权限状态!");
return;
}
var check = el2.getChecked();
var yi = "";
var er = "";
var an = "";
//获取已经选中的数据
for (var i = 0; i < check.length; i++) {
//获取一级菜单id
//包含y
if (check[i].id.indexOf('y') > -1 && check[i].id.length < 6) {
yi += check[i].id+"^";
}
//获取二级菜单
if (check[i].id.indexOf('e') > -1 && check[i].id.length >= 6) {
if (check[i].id.indexOf('a') > -1) {
an +=check[i].id + "^";
}
if (check[i].id.indexOf('y')>-1) {
er +=check[i].id + "^";
}
}
}
var data = { "action_post": $("#action_post").val(), "id_addedit": $("#id_addedit").val(), "RoleName_addedit": $("#RoleName_addedit").val(), "issh": $("#issh").val(), "yiji": yi, "er": er, "an": an };//$("#mainform").serialize();
console.log(data);
var se = function () {
parent.thispagereload();
closethis();
}
var sfunc = function (text) {
if (text == "") {
$.tbmAlertSuccess("保存成功", se);
} else {
$.tbmAlertWarning(text);
}
};
$.ajText_withOutUrl(data, sfunc);
})
-------------------------------------------------------------------后端数据处理--------------------------------------------------------
string action_post = this.GetRequestStr("action_post");
string RoleName_addedit = this.GetRequestStr("RoleName_addedit");
int id_addedit = this.GetRequestInt("id_addedit");
//一级菜单
string[] yi = this.GetRequestStr("yiji").Split('^');
//二级菜单
string[] er = this.GetRequestStr("er").Split('^');
//按钮菜单
string[] an = this.GetRequestStr("an").Split('^');
int state = this.GetRequestInt("issh");
switch (action_post)
{
case "add":
//查询是否菜单名重复
DataTable dt = DBHelper.getDataTable("SELECT * FROM pn_db.????WHERE rolename=?rolename",
DBHelper.CreateParameter("rolename", RoleName_addedit));
List<string> SQLStringList = new List<string>();
if (dt.Rows.Count > 0)
{
return Content("角色重复!");
}
else
{
//将一级级菜单和选中的按钮插入到pn_base_roles表中
for (int i = 0; i < er.Length - 1; i++)
{
var yiid = 0;
var erid = 0;
//根据分隔符,分隔开
string[] s = er[i].Split(',');
//判断是否为成对的一级菜单额二级菜单
if (s.Length == 2)
{
//二级id
if (s[0].IndexOf('e') > -1)
{
erid = s[0].Split(':')[1].ToInt32();
}
//一级id
if (s[1].IndexOf('y') > -1)
{
yiid = s[1].Split(':')[1].ToInt32();
}
SQLStringList.Add("insert into pn_db.???(userid,menuid,status,parentid) values(" + dtid + "," + erid + ",0," + yiid + ")");
}
}
//将二级菜单和选中的按钮插入到pn_base_menus 表中
for (int i = 0; i < an.Length - 1; i++)
{
var erid = 0;
var anid = 0;
//根据分隔符,分隔开
string[] s = an[i].Split(',');
if (s.Length == 2)
{
if (s[0].IndexOf('a') > -1)
{
anid = s[0].Split(':')[1].ToInt32();
}
if (s[1].IndexOf('e') > -1)
{
erid = s[1].Split(':')[1].ToInt32();
}
SQLStringList.Add("insert into pn_db.???(menuid,buttonid) values(" + erid + "," + anid + ")");
}
}
}
int fhrows = DBHelper.ExecuteSqlTran(SQLStringList);
if (fhrows == SQLStringList.Count)
{
int rows = DBHelper.ExecuteSql(@"INSERT INTO pn_db.????
(rolename,status,createtime) values (?rolename,?status,NOW())",
DBHelper.CreateParameter("rolename", RoleName_addedit),
DBHelper.CreateParameter("status", state));
if (rows > 0)
return Content("");
}
代码写的比较low,仅供参考