分类:
EF
前面我们做了树形级别的,现在我们就来分析讲解一哈按钮级别的。
树形级别效果如下:
按钮级别分析:
首先我们要在控制器里再加两个方法,两个方法分别是:第一个:根据权限id查询按钮权限的方法,第二个:给予用户按钮权限授权
第一个方法代码如下: public ActionResult GetButtonPremissonByPId(int pid) { PremissonDAL premissonDAL = new PremissonDAL(); List<Premisson_Func> premissonList = premissonDAL.GetButtonPremissonByPId(pid); var index = 1; foreach (var item in premissonList) { item.Remark = string.Format("<input type=\"radio\" lay-filter=\"premissonradio\" name=\"sex{0}\" checked=\"\" value=\"1\" title=\"启动\" checked=\"\"><input type=\"radio\" lay-filter=\"premissonradio\" name=\"sex{0}\" value=\"0\" title=\"禁用\" checked=\"\"><input type=\"radio\" lay-filter=\"premissonradio\" name=\"sex{0}\" value=\"2\" title=\"禁用并隐藏\">", index); index++; } return Json(new { code = 0, msg = "成功", data = premissonList }, JsonRequestBehavior.AllowGet); } 第二个方法代码如下: public ActionResult SaveButtonPremisson(int? userid, int? premissid, int type) { PremissonDAL premissonDAL = new PremissonDAL(); Premisson_Func_X_Users func_x_users = new Premisson_Func_X_Users() { UserId = userid, PId = premissid, Type = type }; int count = premissonDAL.AddButtonPremisson(func_x_users); return Json(count); }
其次我们在要在DAL里面同样再加两个方法,两个方法分别是:第一个:添加按钮权限方法,我们用简单粗暴的方法先删,删了之后再添。第二个方法是:根据权限id查询按钮权限
第一个DAL方法代码如下: public int AddButtonPremisson(Premisson_Func_X_Users func_x_users) { oaEntities oae = new oaEntities(); oae.Database.ExecuteSqlCommand("delete from dbo.Premisson_Func_X_Users where userid=@userid and pid=@pid",new SqlParameter("userid",func_x_users.UserId),new SqlParameter("pid",func_x_users.PId)); oae.Premisson_Func_X_Users.Add(func_x_users); return oae.SaveChanges(); } 第二个DAL方法代码如下: public List<Premisson_Func> GetButtonPremissonByPId(int pid) { oaEntities oae = new oaEntities(); return oae.Premisson_Func.Where(a=>a.Parent==pid&&a.Tag=="button").ToList(); }
最后我们在百度里面去搜索layui找到如下方法,在获取对应需要的值即可
layui.use(['tree', 'util', 'form','table'], function () { var tree = layui.tree , layer = layui.layer , util = layui.util , form = layui.form var table = layui.table; //监听单选按钮事件 form.on('radio(premissonradio)', function (data) { console.log(data.elem); //得到radio原始DOM对象 //alert(data.value); //被点击的radio的value值 //授权类型 var type = data.value; //授权人 var userid = $("#usersel").val(); if (userid==null||userid=="") { alert("请选择授权人"); return; } //获取权限id var pid = $(data.elem).parents("tr").find("td").eq(0).find("div").html(); $.post('/Premisson/SaveButtonPremisson', { userid: userid, premissid: pid, type: type }, function () { }); }); //给树形提供值 tree.render({ elem: '#test12' , data: treenode , showCheckbox: true //是否显示复选框 , id: 'demoId1' , isJump: true //是否允许点击节点时弹出新窗口跳转 , click: function (obj) { var data = obj.data; //获取当前点击的节点数据 table.render({ elem: '#test' , url: '/Premisson/GetButtonPremissonByPId?pid=' + data.id , cellMinWidth: 80 //全局定义常规单元格的最小宽度,layui 2.2.1 新增 , cols: [[ { field: 'Id', width: 80, title: 'ID', sort: true } , { field: 'Name', width: 80, title: '权限' } , { field: 'Url', width: 210, title: '控制器', sort: true } , { field: 'Remark', title: '授权' } ]] });
在这里数据库设计非常重要。数据库代码如下:
use master go if exists (select 1 from sysdatabases where name='oa') drop database oa go create database oa go use oa go create table [dbo].[Users] ( Id int identity(1,1) not null, UserName nvarchar(64) null, Password nvarchar(64) null, UClass nvarchar(64) null, CheckType int null, IDCard nvarchar(64) null, BankId nvarchar(64) null, primary key clustered ( Id asc ) with (pad_index=off,statistics_norecompute=off,ignore_dup_key=off,allow_row_locks=on,allow_page_locks=on) on [primary] )on [primary] go set identity_insert [dbo].[Users] on insert [dbo].[Users] ([Id],[UserName],[Password],[UClass],[CheckType],[IDCard],[BankId]) values(1,'张黑黑','123456','T468',1,NULL,NULL); insert [dbo].[Users] ([Id],[UserName],[Password],[UClass],[CheckType],[IDCard],[BankId]) values(2,'张大黑','123456','T467',1,NULL,NULL); insert [dbo].[Users] ([Id],[UserName],[Password],[UClass],[CheckType],[IDCard],[BankId]) values(3,'张秋丽','123456','T469',2,NULL,NULL); insert [dbo].[Users] ([Id],[UserName],[Password],[UClass],[CheckType],[IDCard],[BankId]) values(4,'李斯文','123456','T470',1,NULL,NULL); insert [dbo].[Users] ([Id],[UserName],[Password],[UClass],[CheckType],[IDCard],[BankId]) values(5,'关羽','123456','T480',2,NULL,NULL); insert [dbo].[Users] ([Id],[UserName],[Password],[UClass],[CheckType],[IDCard],[BankId]) values(6,'丁原','123456','T479',3,NULL,NULL); insert [dbo].[Users] ([Id],[UserName],[Password],[UClass],[CheckType],[IDCard],[BankId]) values(7,'刘备','123456','T481',3,NULL,NULL); insert [dbo].[Users] ([Id],[UserName],[Password],[UClass],[CheckType],[IDCard],[BankId]) values(8,'曹操','123456','T465',3,NULL,NULL); insert [dbo].[Users] ([Id],[UserName],[Password],[UClass],[CheckType],[IDCard],[BankId]) values(9,'孙悟空','123456','T466',1,NULL,NULL); insert [dbo].[Users] ([Id],[UserName],[Password],[UClass],[CheckType],[IDCard],[BankId]) values(10,'猪八戒','123456','T464',2,NULL,NULL); select * from [dbo].[Users]; create table [dbo].[Premisson_Func_X_Users] ( Id int primary key identity(1,1) not null, UserId int not null, PId int not null, Type int null, ) go --张黑黑分配权限 insert into [dbo].[Premisson_Func_X_Users] values(1,1,null); insert into [dbo].[Premisson_Func_X_Users] values(1,4,null); insert into [dbo].[Premisson_Func_X_Users] values(1,5,null); insert into [dbo].[Premisson_Func_X_Users] values(1,6,null); --张大黑分配权限 insert into [dbo].[Premisson_Func_X_Users] values(2,2,null); insert into [dbo].[Premisson_Func_X_Users] values(2,7,null); insert into [dbo].[Premisson_Func_X_Users] values(2,8,null); insert into [dbo].[Premisson_Func_X_Users] values(1,2,null); insert into [dbo].[Premisson_Func_X_Users] values(1,7,null); insert into [dbo].[Premisson_Func_X_Users] values(1,8,null); insert into [dbo].[Premisson_Func_X_Users] values(3,1,null); --张秋丽分配权限 insert into [dbo].[Premisson_Func_X_Users] values(3,2,null); insert into [dbo].[Premisson_Func_X_Users] values(3,7,null); insert into [dbo].[Premisson_Func_X_Users] values(3,8,null); insert into [dbo].[Premisson_Func_X_Users] values(3,3,null); insert into [dbo].[Premisson_Func_X_Users] values(3,10,null); select * from [dbo].[Premisson_Func_X_Users]; create table [dbo].[Premisson_Func] ( Id int primary key identity(1,1) not null, Name nvarchar(64) not null, Url nvarchar(64) null, Parent int null, Tag nvarchar(32) null, Remark nvarchar(64) null, ) go insert into [dbo].[Premisson_Func] values('部门管理',NULL,NULL,null,null); insert into [dbo].[Premisson_Func] values('权限管理',NULL,NULL,null,null); insert into [dbo].[Premisson_Func] values('人事管理',NULL,NULL,null,null); insert into [dbo].[Premisson_Func] values('技术部','/home/index',1,null,null); insert into [dbo].[Premisson_Func] values('测试部','/home/DepartTwo',1,null,null); insert into [dbo].[Premisson_Func] values('实施部','/home/DepartThree',1,null,null); insert into [dbo].[Premisson_Func] values('菜单管理','#',2,null,null); insert into [dbo].[Premisson_Func] values('权限分配','/Premisson/GivePremisson',2,null,null); insert into [dbo].[Premisson_Func] values('角色管理','#',2,null,null); insert into [dbo].[Premisson_Func] values('考勤管理','#',3,null,null); insert into [dbo].[Premisson_Func] values('添加','/MenuManger/DoAdd',7,'button',null); insert into [dbo].[Premisson_Func] values('更新','/MenuManger/DoUpdate',7,'button',null); insert into [dbo].[Premisson_Func] values('删除','/MenuManger/DoDelete',7,'button',null); insert into [dbo].[Premisson_Func] values('查看','/MenuManger/DoSearch',7,'button',null); insert into [dbo].[Premisson_Func] values('打印','/MenuManger/DoStamp',7,'button',null); select * from [dbo].[Premisson_Func]; --查询用户id查询用户所拥有的权限 select * from dbo.Premisson_Func where Id in (select pid from dbo.Premisson_Func_X_Users where UserId=1)
这样我们就大功告成了,点击左边的菜单管理右边也能出现表格的样子了(这里不一定是表格,可以是其他的,div什么之类的都行。自己觉得好看就OK了)。效果如下:
(如果我这里没有选择授权人,那么它会弹一个框出来提示你:请选择授权人,选择了之后我们才能进行一系列的操作)。
在这里我选一个授权人是张大黑,我们把他更新禁用掉的,跟他启动
禁用时的效果如下:
启动时的效果如下:
在这里我们可以看到两个启动,我们再去数据库里看一下数据库刚才启动那个数据是否更新?
在这里我们可以看到数据库是更新了的,他的type为1就说明我们是更新成功了的。
type为1:启动
type为0:禁用
type为2:禁用并隐藏
评价
排名
6
文章
6
粉丝
16
评论
8
{{item.articleTitle}}
{{item.blogName}} : {{item.content}}
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2024TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术