zoukankan      html  css  js  c++  java
  • easyui和Ajax在mvc3中的权限设置

    权限管理,ps:抄的领导的,比较粗糙,一切挺好的,就是没有那个编辑用户权限时,默认选中原来已有的权限节点。

    效果如下:

    cshtml代码:

     1 @{
     2     ViewBag.Title = "学校后台权限设置";
     3     Layout = "~/Views/Shared/_Layout2.cshtml";
     4 }
     5 @section Head{
     6     <script src="@Url.Content("~/Scripts/BasicSchool_RightJS.js")" type="text/javascript"></script>
     7     <script type="text/javascript">
     8         $(function () {
     9             $("#provinceId").combobox({
    10                 url: "/BasicSchool_Right/GetTeacher",
    11                 editable: false,
    12                 valueField: "id",
    13                 textField: "userName",
    14                 panelHeight: "auto"
    15             });
    16 
    17         });
    18     </script>
    19 }
    20 <div region="center" style=" 550px; height: 300px; padding: 1px; overflow-y: hidden">
    21     <div id="grid">
    22     </div>
    23 </div>
    24 <div id="rightSet-window" style="padding: 10px; background: #fff; border: 1px solid #ccc; 850px; height: 350px; padding: 10px">
    25      
    26       <ul id="rightSet-ul-window"></ul>
    27       <a id="aRowId"></a>
    28       <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-ok" onclick="saveData()"> 保存</a>
    29 </div>
    30 
    31 <div id="search-window" title="搜索教师" style=" 350px; height: 250px; padding: 10px">
    32     <div style="padding: 10px; background: #fff; border: 1px solid #ccc;">
    33         <form method="post" id="Form2">
    34          <table>
    35             <tr>
    36                 <td style="text-align: right">
    37                     教师:
    38                 </td>
    39                 <td>
    40                     <select id="provinceId" class="easyui-combogrid" name="provinceId" style=" 200px;"
    41                         class="easyui-validatebox" required="true">
    42                     </select>
    43                 </td>
    44             </tr> 
    45             <tr>
    46                 <td style="text-align: right">
    47                     教师名称:
    48                 </td>
    49                 <td>
    50                     <input type="text" name="schoolName" id="schoolName" class="easyui-validatebox"
    51                         style=" 200px;" maxlength="25" />
    52                 </td>
    53             </tr>  
    54         </table>
    55         </form>
    56     </div>
    57     <div style="text-align: right; padding: 5px; vertical-align: bottom">
    58         <a href="javascript:void(0)" onclick="SearchOK()" id="btn-search" icon="icon-ok">搜索</a>
    59         <a href="javascript:void(0)" onclick="closeSearchWindow()" id="btn-search-cancel"
    60             icon="icon-cancel">取消</a>
    61     </div>
    62 </div>
    Index.cshtml

    后台Action代码:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Web;
     5 using System.Web.Mvc;
     6 using TeachAid.SpecialCloudEduPlatform.BLL;
     7 using TeachAid.SpecialCloudEduPlatform.Entity;
     8 using System.Web.Script.Serialization;
     9 using Newtonsoft.Json;
    10 
    11 namespace TeachAid.SpecialCloudEduPlatform.Web.Controllers
    12 {
    13     public class TreeViewLocation
    14     {
    15         public TreeViewLocation()
    16         {
    17             children = new HashSet<TreeViewLocation>();
    18         }
    19         public int id { get; set; }
    20         public string text { get; set; }
    21         public ICollection<TreeViewLocation> children { get; set; }
    22     }
    23 
    24     public class BasicSchool_RightController : Controller
    25     {
    26         BLLBasicMain_UserInfo userInfoBLL = new BLLBasicMain_UserInfo();
    27         BLLJsonHelper bjb = new BLLJsonHelper(); 
    28 
    29         public ActionResult Index()
    30         {
    31             return View();
    32         }
    33 
    34         /// <summary>
    35         /// 查询学校教师信息6
    36         /// </summary>
    37         /// <returns></returns>
    38         public ActionResult GetTeacher() 
    39         {
    40             IList<BasicMainUserInfo> userList = userInfoBLL.GetUserListBySchoolAndRold(((BasicMainUserInfo)Session["UserInfoLogin"]).schoolID, 1);
    41             JavaScriptSerializer json = new JavaScriptSerializer();
    42             string str = json.Serialize(userList); 
    43             return Content(str, "text/html;charset=UTF-8"); 
    44         }
    45 
    46         /// <summary>
    47         /// 教师信息列表
    48         /// </summary>
    49         /// <returns></returns>
    50         public ActionResult GetList() 
    51         {
    52             int pageIndex = int.Parse(Request.Form["page"].ToString());
    53             int pageSize = int.Parse(Request.Form["rows"].ToString());
    54             string biao = "View_BasicSchool_UserTeacherDetails";
    55             string zhid = "id,userName,RealName,pwd,types,schoolID,schoolName,bid,cId,basicMain_UserInfoID,basicMain_SchoolId,isDirector,titles,birthday,phone,sex,email,blogUrl,motto,photo,isActivation,addTime";
    56             string str = " schoolID=" + ((BasicMainUserInfo)Session["UserInfoLogin"]).schoolID + " and types=1 ";
    57             string json = bjb.GetJSON(pageIndex, pageSize, biao, zhid, "id", "desc", str);
    58             return Content(json, "text/html;charset=UTF-8");
    59         } 
    60 
    61         /// <summary>
    62         /// 获得树节点菜单
    63         /// </summary>
    64         /// <returns></returns>
    65         public ActionResult GetTreeJson() 
    66         {
    67             BLLBasicMainTreeView blltv = new BLLBasicMainTreeView();
    68             JavaScriptSerializer j = new JavaScriptSerializer();
    69             return Content(j.Serialize(blltv.GetTreeViewBySchoolId(((BasicMainUserInfo)Session["UserInfoLogin"]).schoolID).ToArray()), "text/html;charset=UTF-8"); 
    70         }
    71 
    72         /// <summary>
    73         /// 保存为当前用户分配的权限信息
    74         /// </summary>
    75         /// <param name="ids"></param>
    76         /// <param name="schoolid"></param>
    77         /// <returns></returns>
    78         public ActionResult CreateRightSet(string ids, string uid)
    79         {
    80             Message msg = new Message(true, "保存信息成功!");
    81             BLLBasicMainTreeView bllMainTree = new BLLBasicMainTreeView();
    82             bllMainTree.InsertNodeForSchoolUsers(int.Parse(uid), ids);
    83             return Content(JsonConvert.SerializeObject(msg), "text/html;charset=UTF-8");
    84         } 
    85     }
    86 }
    BasicSchool_RightController.cs

    js代码:

      1 // 全局变量
      2 var grid;
      3 var dlg_Edit;
      4 var dlg_Edit_form;
      5 var searchWin;
      6 var searchForm;
      7 var virpath = ""; //网站的虚拟目录 如:/ShopManager
      8 
      9 
     10 $(function () {
     11     grid = $('#grid').datagrid({
     12         title: '权限分配',
     13         iconCls: 'icon-save',
     14         methord: 'post',
     15         url: '/BasicSchool_Right/GetList/',
     16         sortName: 'id',
     17         sortOrder: 'desc',
     18         idField: 'id',
     19         pageSize: 20,
     20         striped: true, //奇偶行是否区分
     21         columns: [[
     22                     { field: 'id', title: '编号',  100, sortable: true },
     23                     { field: 'userName', title: '教师登录名',  100, sortable: true },
     24                     { field: 'RealName', title: '真实姓名',  100, sortable: true },
     25                     { field: 'addTime', title: '添加时间',  120, sortable: true }, 
     26                     {field: 'schoolID', title: '权限管理',  50, rowspan: 2, align: 'left',
     27                      formatter: function (value, rec) {
     28                          return '<a style="color:blue;cursor:pointer;" href="#" onclick="editRightSet()">编辑</a>';
     29                      }
     30                     }
     31                   ]],
     32         fit: true,
     33         pagination: true,
     34         rownumbers: true,
     35         fitColumns: true,
     36         singleSelect: false,
     37         toolbar: [{
     38             text: '高级搜索',
     39             iconCls: 'icon-search',
     40             handler: OpensearchWin
     41         }, '-', {
     42             text: '所有',
     43             iconCls: 'icon-search',
     44             handler: showAll
     45         }], onDblClickRow: function (rowIndex, rowData) {  //datagrid 行双击事件
     46 
     47 
     48         }, onClickRow: function (value, rec) {  //datagrid 行双击事件
     49             $('#aRowId').val(rec.id); // 给schoolid 影藏控件赋值
     50         }, onHeaderContextMenu: function (e, field) {
     51             e.preventDefault();
     52             if (!$('#tmenu').length) {
     53                 createColumnMenu();
     54             }
     55             $('#tmenu').menu('show', {
     56                 left: e.pageX,
     57                 top: e.pageY
     58             });
     59         }
     60 
     61     });
     62 
     63     dlg_Edit = $('#rightSet-window').dialog({
     64         closed: true,
     65 
     66         toolbar: [{
     67             text: '保存',
     68             iconCls: 'icon-save',
     69             handler: saveData
     70         }, '-', {
     71             text: '关闭',
     72             iconCls: 'icon-no',
     73             handler: function () {
     74                 dlg_Edit.dialog('close');
     75             }
     76         }]
     77 
     78     });
     79 
     80 
     81     dlg_Edit_form = dlg_Edit.find('form');
     82 
     83     $('#btn-search,#btn-search-cancel').linkbutton();
     84     searchWin = $('#search-window').window({
     85         iconCls: 'icon-search',
     86         closed: true,
     87         modal: true
     88     });
     89     searchForm = searchWin.find('form');
     90     $('body').layout();
     91 });
     92 
     93 //设置权限
     94 function editRightSet() { 
     95     var rightSetWin = $('#rightSet-window').window({
     96         title: "设置权限",
     97         closed: true,
     98         modal: true,
     99         minimizable: false
    100     });
    101     rightSetWin.window('open');
    102     GetRightTree();
    103     rightSetWin.form('clear');
    104 }
    105 
    106 //获得权限设置
    107 function GetRightTree() {
    108     $('#rightSet-ul-window').tree({
    109         checkbox: true,
    110         url: '/BasicSchool_Right/GetTreeJson',
    111         //cascadeCheck: false, 选择此节点不需要父节点
    112         onClick: function (node) {
    113             $(this).tree('toggle', node.target);
    114         }, onContextMenu: function (e, node) {
    115             e.preventDefault();
    116             $('#rightSet-ul-window').tree('select', node.target);
    117         }
    118     });
    119 }
    120 
    121 
    122 function createColumnMenu() {
    123     var tmenu = $('<div id="tmenu" style="100px;"></div>').appendTo('body');
    124     var fields = grid.datagrid('getColumnFields');
    125     for (var i = 0; i < fields.length; i++) {
    126         $('<div iconCls="icon-ok"/>').html(fields[i]).appendTo(tmenu);
    127     }
    128     tmenu.menu({
    129         onClick: function (item) {
    130             if (item.iconCls == 'icon-ok') {
    131                 grid.datagrid('hideColumn', item.text);
    132                 tmenu.menu('setIcon', {
    133                     target: item.target,
    134                     iconCls: 'icon-empty'
    135                 });
    136             } else {
    137                 grid.datagrid('showColumn', item.text);
    138                 tmenu.menu('setIcon', {
    139                     target: item.target,
    140                     iconCls: 'icon-ok'
    141                 });
    142             }
    143         }
    144     });
    145 }
    146 
    147 
    148  
    149 //保存用户的权限节点
    150 function saveData() { 
    151     var nodes = $('#rightSet-ul-window').tree('getChecked');
    152     var str = '';
    153     for (var i = 0; i < nodes.length; i++) {
    154         if (str != '') str += ',';
    155         str += nodes[i].id;
    156     }
    157     if (str.length > 0) {
    158         $.ajax({ 
    159             url: virpath + '/BasicSchool_Right/CreateRightSet/?ids=' + str + '&uid=' + $('#aRowId').val(),
    160             type: 'post',
    161             error: function () {
    162                 Msgalert('错误', '失败!', 'error');
    163             },
    164             success: function (re) {
    165                 Msgalert('提示', '保存成功!', '提示');
    166                 dlg_Edit.dialog('close');
    167             }
    168         });
    169     } else {
    170         Msgalert('提示', '请选择你要设置的模块!', '提示');
    171     }
    172 
    173 } 
    174 
    175 function showAll() {
    176     grid.datagrid({ url: virpath + '/BasicSchool_Right/GetList/' });
    177 }
    178 function OpensearchWin() {
    179     searchWin.window('open');
    180     searchForm.form('clear');
    181     $('#Enable2').combobox('setValue', true); //默认设置开启搜素
    182 }
    183 
    184 function SearchOK() {
    185     var Name = $("#Name").val();
    186     var Id = $("#TypeId2").combobox("getValue");
    187     var bl = $('#Enable2').combobox('getValue'); //默认设置开启搜素
    188     var PId = $("#pid").val(); //ID
    189     var Price = $("#Price").val(); //价格
    190     //    alert(Name + "==" + Id + "==" + MId + "==" + bl + "==" + CId+"=="+Code);
    191     searchWin.window('close');
    192     grid.datagrid({ url: virpath + '/Admin/SeachProductInfo/', queryParams: { ProductName: Name, typeId: Id, ProductId: PId, RealPrice: Price, en: bl} });
    193     //经过测试queryParams参数可以提交到后台通过FormCollection获取 也可以Request["ProductName"]=?获取
    194 }
    195 function closeSearchWindow() {
    196     searchWin.window('close');
    197 }
    BasicSchool_RightJS.js

    Action里调用的Bll里的方法,有一点儿需要注意的就是:使用的递归方法,循环插入节点,返回节点列表

    BLL层代码:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using TeachAid.SpecialCloudEduPlatform.Entity;
     6 
     7 namespace TeachAid.SpecialCloudEduPlatform.BLL
     8 {
     9     public class BLLBasicMainTreeView
    10     {
    11         private List<BasicMainTreeView> models = new List<BasicMainTreeView>();
    12         public List<BasicMainTreeView> GetTreeView()
    13         {
    14             DAL.DALBasicMainTreeView dalView = new DAL.DALBasicMainTreeView();
    15             models = dalView.GetTreeView();
    16             List<BasicMainTreeView> modp = new List<BasicMainTreeView>();
    17             BasicMainTreeView modelParent = new BasicMainTreeView();
    18             modelParent.id = -1;
    19             modelParent.text = "权限设置";
    20             GetTree(0, modelParent);
    21             modp.Add(modelParent);
    22             return modp;
    23         }
    24         private void GetTree(int parentID, BasicMainTreeView moParent)
    25         {
    26             List<BasicMainTreeView> modelInfo = models.Where(Model => Model.parentID == parentID).ToList();
    27             foreach (BasicMainTreeView model in modelInfo)
    28             {
    29                 BasicMainTreeView mo = new BasicMainTreeView();
    30                 mo = model;
    31                 moParent.children.Add(mo);
    32                 GetTree(mo.id, mo);
    33             }
    34         }
    35 
    36         /// <summary>
    37         /// 根据学校ID,查询该学校的所有权限节点
    38         /// </summary>
    39         /// <param name="schoolId">学校id</param>
    40         /// <returns></returns>
    41         public List<BasicMainTreeView> GetTreeViewBySchoolId(int schoolId)
    42         {
    43             DAL.DALBasicMainTreeView treeDAL = new DAL.DALBasicMainTreeView();
    44             models = treeDAL.GetTreeViewBySchoolId(schoolId);
    45             List<BasicMainTreeView> treeList = new List<BasicMainTreeView>();
    46             BasicMainTreeView treeParent = new BasicMainTreeView();
    47             treeParent.id = -1;
    48             treeParent.text = "权限设置";
    49             GetTree(0, treeParent);
    50             treeList.Add(treeParent);
    51             return treeList;
    52         }
    53 
    54         /// <summary>
    55         /// 
    56         /// </summary>
    57         /// <param name="schoolId">学校ID</param>
    58         /// <param name="nodeID">权限节点 例如:1,2,3,4......</param>
    59         public void InsertNodeID(int schoolId, string nodeID)
    60         {
    61             DAL.DALBasicMainTreeView dalView = new DAL.DALBasicMainTreeView();
    62             dalView.InsertNodeID(schoolId, nodeID);
    63         }
    64 
    65 
    66         /// <summary>
    67        /// 为学校用户分配权限节点
    68        /// </summary>
    69        /// <param name="userId">用户id</param>
    70        /// <param name="nodeId">节点id</param>
    71         public void InsertNodeForSchoolUsers(int userId, string nodeId) 
    72         {
    73             DAL.DALBasicMainTreeView dalView = new DAL.DALBasicMainTreeView();
    74             dalView.InsertNodeForSchoolUsers(userId, nodeId);
    75         }
    76     }
    77 }
    BLLBasicMainTreeView.cs

    方法:

    private void GetTree(int parentID, BasicMainTreeView moParent)
    {
    List<BasicMainTreeView> modelInfo = models.Where(Model => Model.parentID == parentID).ToList();
    foreach (BasicMainTreeView model in modelInfo)
    {
    BasicMainTreeView mo = new BasicMainTreeView();
    mo = model;
    moParent.children.Add(mo);
    GetTree(mo.id, mo);
    }
    }里的 models.Where(Model => Model.parentID == parentID).ToList();刚开始挺让我费解的[ps:我太笨了,,] 有大仙儿网友说:就类似于:select * from model where model.parentID=parentID   查询条件是parentID

    千万别忘了。。。

  • 相关阅读:
    GitCam一款Gif动画制作软件
    IniParse解析类
    一个网页设计师应该考虑的9件事
    Backit轻松为您的网站创建备份
    [翻译]Web开发牛人访谈:你们都在用什么?
    简单文件服务器
    一个网站访问速度测试工具
    GS运维常用工具及文档
    Oracle快速收集AWR的方案
    Oracle调整内存参后报ORA-00844和ORA-00851
  • 原文地址:https://www.cnblogs.com/ZQiuMei/p/3205408.html
Copyright © 2011-2022 走看看