C#.NET分级选择实现
前端代码:
数据库表结构如下:
id pname parent
记得引用jquery和SelectPermission外部JS文件
<style type="text/css">
#showhide
{
width: 100%;
height: 100%;
background-color: #FFFFFF;
position: absolute;
left: 0;
top: 0;
display: none;
}
#selectPermission{ width:100%; line-height:25px; border-bottom:1px solid #EEEEEE; overflow:hidden; margin:10px 0; padding-bottom:10px}
#selectPermission li{ width:150px; padding-left:10px}
#selectPermission div{float:left; margin-right:10px}
#resultshow{ margin-top:10px; width:100%;border-bottom:1px solid #EEEEEE; overflow:hidden; padding-bottom:10px; margin-bottom:10px}
#resultshow li{ width:120px; float:left}
</style>
<script type="text/javascript">
function setpower(va) {
$("#selectPermission").empty();
$("#resultshow ul").empty();
$("#showhide").toggle();
var att = $("#showhide").css("display");
if (att == "block") {
$("#uid").val(va);
$.post('handler/userInfo.ashx', { 'parentId': "0" }, function (data) {
//alert(data);
var re = data.split('*');
if (re[0] != "notfind") {
var newdiv = "<div id='div"+re[0]+"'>" + re[1] + "</div>";
$("#selectPermission").append(newdiv);
var newdivs = "<div id='div" + re[0] + "'>" + re[2] + "</div>";
$("#selectPermission").append(newdivs);
} else {
var person = "<div id='div" + re[0] + "'>" + re[2] + "</div>";
$("#selectPermission").append(person);
}
});
$.post('handler/permissionSet.ashx', { 'userid': va }, function (data) {
addal(data);
});
}
}
function cancelClick() {
$("#showhide").hide();
}
</script>
<div id="showhide">
<div><b>选择权限:</b></div>
<div id="selectPermission"></div>
<div><b>已选权限:</b></div>
<div id="resultshow" style="clear:both">
<ul>
</ul>
</div>
<div style=" clear:both">
<input id="Button1" type="button" value="保存" onclick="sureClick();"/>
<input id="Button2" type="button" value="取消" onclick="cancelClick();"/>
</div>
</div>
分离JS(SelectPermission.js)代码:
function NextDivData(va, sta) {//向后台请求下一级权限
$.post('handler/userInfo.ashx', { 'parentId': va, 'checkState': sta }, function (data) {
//alert(data);
var alldata = data.split('*');
if (alldata[0] != "notfind") {
var newdiv = "<div id='div"+alldata[0]+"'>" + alldata[1] + "</div>";
$("#selectPermission").append(newdiv);
var person = "<div id='div" + alldata[0] + "'>" + alldata[2] + "</div>";
$("#selectPermission").append(person);
} else {
var person = "<div id='div" + alldata[0] + "'>" + alldata[2] + "</div>";
$("#selectPermission").append(person);
}
});
}
function laterClick(va) {//父节点单击事件
$(va).parent().css("background", "#DEECFD").siblings().css("background", "");
$(va).parent().parent().parent().nextAll().remove();
var ch = $(va).parent().find("[type='checkbox']");
var sta = "no";
if (ch.attr("checked")) {
sta = "yes";
}
var $find = $(va).find("input");
var getid = $find.val();
//alert(getid);
var ids = getid.split(',');
//$("#diffState").val(ids[1]);
NextDivData(ids[0], sta);
}
function checkState(va) {//部门复选框单击事件
if ($(va).attr("checked") == true) {
//alert('被选中');
$("#checkState").val('yes');
//$(va).parent().parent().parent().next().find("[type='checkbox']").attr("checked", "checked");
var $find = $(va).siblings().find("input");
var getid = $find.val(); //获取当前项目ID
var ids = getid.split(',');
$("#div" + ids[0]).find("[type='checkbox']").attr("checked", "checked");
//alert(ids[0]);
getPersonId(ids[0]);
} else {
$("#checkState").val('no');
//$(va).parent().parent().parent().next().find("[type='checkbox']").removeAttr("checked");
var $find = $(va).siblings().find("input");
var getid = $find.val(); //获取当前项目ID
var ids = getid.split(',');
$("#div" + ids[0]).find("[type='checkbox']").removeAttr("checked");
var getli = $(va).parent().parent().find("li input");
var lic = 0;
$.each(getli, function () {
var st = $(this).attr("checked");
if (st == true) {
lic++;
}
});
var sta = "no";
if (lic == 0) {
sta = "yes";
}
lostperson(ids[0],sta);
}
}
function getPersonId(pid) {//获取人员id和姓名
$.post('handler/userInfo.ashx', { 'getPer': pid }, function (data) {
if (data != "" && data != null) {
//alert(data);
var ald = data.split('*');
//alert(ald[1]); //父级ID
$("#div0").find("[id='"+ald[1]+"']").attr("checked", "checked");
var gd = ald[0].split('%');
gd = gd.splice(1, gd.length);
var li = $("#resultshow ul li");
var resul = "";
$.each(gd, function (i, value) {
var gd1 = value.split('$');
var tem = 0;
if (li.length > 0) {
$.each(li, function () {
var st = $(this).find("[type='checkbox']").attr("id");
//var st = $(this).find("[type='checkbox']");
//st = st.val().split(',');
//alert(st);
if (st == gd1[0]) {
tem++;
}
});
}
if (tem == 0) {
resul += "<li><input id='" + gd1[0] + "' type='checkbox' checked='checked' value='" + gd1[0] + "," + ald[1] + "' />" + gd1[1] + "</li>";
}
});
$("#resultshow ul").append(resul);
}
});
}
function lostperson(pid, val) {
$.post('handler/userInfo.ashx', { 'lost': pid }, function (data) {
if (data != "" && data != null) {
//alert(data);
var ald = data.split('*');
var cid = ald[0].split(',')
cid = cid.splice(0, cid.length - 1);
//alert(cid);
$.each(cid, function (data, key) {
//alert(key);
var li = $("#resultshow ul li");
$.each(li, function () {
var st = $(this).find("[type='checkbox']").attr("id");
//var st = $(this).find("[type='checkbox']");
//st = st.val().split(',');
//alert(st);
if (st == key) {
$(this).remove();
}
//alert(st);
});
});
if (val == "yes") {
$("#div0").find("[id='" + ald[1] + "']").removeAttr("checked");
}
}
});
}
function addal(va) {//加载原有权限
if (va != "") {
var ad = va.split('%');
var resul = "";
$.each(ad, function (i, val) {
var ad1 = val.split('$');
if (ad1[0] != "") {
resul += "<li><input id='" + ad1[0] + "' type='checkbox' checked='checked' value='" + ad1[0] + ",' />" + ad1[1] + "</li>";
}
});
$("#resultshow ul").append(resul);
}
}
function sureClick() {
var li = $("#resultshow ul li");
var uid = "";
var uname = "";
if (li.length > 0) {
$.each(li, function () {
var st = $(this).find("[type='checkbox']");
if (st.attr("checked")) {
uid += st.val();
}
});
//alert(uid);
}
//alert(uid);
var array = uid.split(",");
for (var i = 0; i < array.length; i++) {
if (array[i] != "") {
for (var j = i + 1; j < array.length; j++) {
if (array[i] == array[j]) {
array.splice(j, 1);
}
}
}
}
var uid = $("#uid").val();
var powerid = array.toString();
//alert(powerid);
$.post('handler/permissionSet.ashx', { 'powerid': powerid, 'uid': uid }, function (data) {
alert(data);
});
$("#resultshow ul").empty();
$("#showhide").hide();
}
一般处理程序(userInfo.ashx)代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Data;
namespace Milit.Web.SysConfig.handler
{
/// <summary>
/// userInfo 的摘要说明
/// </summary>
public class userInfo : IHttpHandler
{
BLL.Permissions bp = new BLL.Permissions();
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string parentid = context.Request["parentId"];//部门ID
string checkState = context.Request["checkState"];//否为选中状态
string getper = context.Request["getPer"];//部门ID,用于获取ID,NAME
string lostid = context.Request["lost"];//取消选择
if (lostid != null && lostid != "")
{
DataTable ddt = bp.GetAllList().Tables[0];
string lostlist = lostPersonID(lostid,ddt);
string pids = getpar(ddt, lostid);//获取所有父级ID
context.Response.Write(lostlist+"*"+pids);
}
if (getper != "" && getper != null)
{
DataTable ddt = bp.GetAllList().Tables[0];
string pin = getPersonIN(getper,ddt);
string pids = getpar(ddt, getper);//获取所有父级ID
context.Response.Write(pin+"*"+pids);
}
if (parentid != "" && parentid != null)
{
DataTable dt = bp.GetAllList().Tables[0];
bool bl = showboy(int.Parse(parentid));
if (bl)
{
string getdata = string.Empty;
if (parentid == "0")
{
getdata = initialpage(dt);
}
else
{
getdata = filltable(dt, parentid, checkState);
}
context.Response.Write(parentid+"*"+getdata);
}
else
{
//context.Response.Write("notfind*" + getPid(parentid, checkState, dt));
context.Response.Write("*");
}
}
}
private string initialpage(DataTable dt)
{
StringBuilder adddata = new StringBuilder("<ul>");
Boolean bo = true;
foreach (DataRow dr in dt.Rows)
{
if (dr["p_num"].ToString() == "0")
{
bo = showboy(int.Parse(dr["Id"].ToString()));
if (bo)
{
adddata.Append("<li><input id='"+dr["id"].ToString()+"' type='checkbox' onchange='checkState(this);'/><a href='#' onclick='laterClick(this);'><input id='hide1' type='hidden' value='" + dr["Id"].ToString() + ",yes' />+" + dr["Id"].ToString()
+ "-" + dr["permission"].ToString() + "</a></li>");
}
else
{
adddata.Append("<li><input id='" + dr["id"].ToString() + "' type='checkbox' onchange='checkState(this);'/><a href='#'><input id='hide1' type='hidden' value='" + dr["Id"].ToString() + ",no' />" + dr["Id"].ToString() + "-" + dr["permission"].ToString()
+ "</a></li>");
}
}
}
adddata.Append("</ul>*");
return adddata.ToString();
}
private string filltable(DataTable dt, string parid, string chst)
{
if (chst == "yes")//被选中
{
StringBuilder adddata = new StringBuilder();
foreach (DataRow dr in dt.Rows)
{
if (dr["p_num"].ToString() == parid)
{
bool bo = showboy(int.Parse(dr["Id"].ToString()));
if (bo)
{
adddata.Append("<li><input id='Checkbox1' type='checkbox' checked='checked' onchange='checkState(this);'/><a href='#' onclick='laterClick(this);'><input id='hide1' type='hidden' value='" + dr["Id"].ToString() + ",yes' />+" + dr["Id"].ToString()
+ "-" + dr["permission"].ToString() + "</a> ");
}
else
{
adddata.Append("<li><input id='Checkbox1' type='checkbox' checked='checked' onchange='checkState(this);'/><a href='#'><input id='hide1' type='hidden' value='" + dr["Id"].ToString() + ",no' />" + dr["Id"].ToString() + "-" + dr["permission"].ToString()
+ "</a> ");
}
}
}
adddata.Append("*");
return adddata.ToString();
}
else
{
StringBuilder adddata = new StringBuilder();
foreach (DataRow dr in dt.Rows)
{
if (dr["p_num"].ToString() == parid)
{
bool bo = showboy(int.Parse(dr["Id"].ToString()));
if (bo)
{
adddata.Append("<li><input id='Checkbox1' type='checkbox' onchange='checkState(this);'/><a href='#' onclick='laterClick(this);'><input id='hide1' type='hidden' value='" + dr["Id"].ToString() + ",yes' />+" + dr["Id"].ToString()
+ "-" + dr["permission"].ToString() + "</a></li>");
}
else
{
adddata.Append("<li><input id='Checkbox1' type='checkbox' onchange='checkState(this);'/><a href='#'><input id='hide1' type='hidden' value='" + dr["Id"].ToString() + ",no' />" + dr["Id"].ToString() + "-" + dr["permission"].ToString()
+ "</a></li>");
}
}
}
adddata.Append("*");
return adddata.ToString();
}
}
private Boolean showboy(int parId)//判断是否包函子节点
{
DataTable dt = bp.GetAllList().Tables[0];
Boolean bl = false;
for (int i = 0; i < dt.Rows.Count; i++)
{
if (dt.Rows[i]["p_num"].ToString() == parId.ToString())
{
bl = true;
}
}
return bl;
}
private string getPersonIN(string pid,DataTable ddt)//根据部门ID获取人员ID
{
//bool bl = showboy(int.Parse(pid));
//if (!bl)
//{
getchild(pid, ddt);
//}
foreach (DataRow dr in ddt.Rows)
{
if (dr["p_num"].ToString() == pid)
{
getPersonIN(dr["Id"].ToString(),ddt);
}
}
return personIN.ToString();
}
private void getchild(string paid, DataTable dt)
{
foreach (DataRow dr in dt.Rows)
{
if (dr["Id"].ToString() == paid)
{
if (dr["p_num"].ToString() != "0")
{
personIN.Append("%" + dr["Id"].ToString() + "$" + dr["permission"].ToString());
}
}
}
}
private string lostPersonID(string pid,DataTable ddt)//根据部门ID获取人员ID
{
//bool bl = showboy(int.Parse(pid));
//if (!bl)
//{
lostchild(pid, ddt);
//}
foreach (DataRow dr in ddt.Rows)
{
if (dr["p_num"].ToString() == pid)
{
lostPersonID(dr["Id"].ToString(),ddt);
}
}
return personIN.ToString();
}
private void lostchild(string paid, DataTable dt)
{
foreach (DataRow dr in dt.Rows)
{
if (dr["Id"].ToString() == paid)
{
//if (dr["p_num"].ToString() != "0")
//{
personIN.Append(dr["Id"].ToString() + ",");
//}
}
}
}
private StringBuilder personIN = new StringBuilder();
private string getPid(string aid, string sta, DataTable dt)
{
StringBuilder sub = new StringBuilder();
int ac = 0;
foreach (DataRow dr in dt.Rows)
{
if (dr["Id"].ToString() == aid)
{
if (dr["p_num"].ToString() != "0")
{
if (sta == "yes")
{
sub.Append("<li><input id='Checkbox1' type='checkbox' onchange='personClick(this);' checked='checked' value='" + dr["Id"].ToString() + "," + dr["permission"].ToString() + "' />" + dr["permission"].ToString() + "</li>");
ac++;
}
else
{
sub.Append("<li><input id='Checkbox1' type='checkbox' onchange='personClick(this);' value='" + dr["Id"].ToString() + "," + dr["permission"].ToString() + "' />" + dr["permission"].ToString() + "</li>");
ac++;
}
}
}
}
if (ac == 0)
{
return null;
}
else
{
return sub.ToString();
}
}
private StringBuilder spb = new StringBuilder();
private string getpar(DataTable dt,string id)//获取当前节点所有父节点
{
foreach (DataRow dr in dt.Rows)
{
if (dr["id"].ToString() == id)
{
foreach(DataRow drr in dt.Rows){
if(drr["id"].ToString()==dr["p_num"].ToString()){
if (spb.ToString() != "")
{
spb.Append(drr["id"].ToString() + ",");
}
else
{
spb.Append(drr["id"].ToString());
}
getpar(dt, drr["id"].ToString());
}
}
}
}
return spb.ToString();
}
public bool IsReusable
{
get
{
return false;
}
}
}
}