using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Data;
using System.Collections;
using System.Web.UI;
using System.Web.UI.WebControls;
using RBAC;
using Newtonsoft.Json;
public partial class DepartmentConfig : BaseRolePage
{
protected DataTable dtTree = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dtTree = Maticsoft.DBUtility.DbHelperSQL.Query("select * from SY_DEPARTMENT").Tables[0];
}
}
#region 获取无限分级数据
/// <summary>
/// 获取无限分级数据
/// </summary>
/// <returns></returns>
protected string GetLevelJson()
{
//获取全部集合
List<DepartModel> allModel = new List<DepartModel>();
DataTable dt = dtTree.Copy();
for (int i = 0; i < dt.Rows.Count; i++)
{
DataRow dr = dt.Rows[i];
DepartModel model = new DepartModel();
model.id = (int)dr["departID"];
model.dname = dr["departName"].ToString();
model.fid = (int)dr["fatherDepartID"];
allModel.Add(model);
}
//筛选顶级分类
var topItems = allModel.Where(e => e.fid == 0).ToList(); //顶级分类
List<DepartModel> topModels = new List<DepartModel>();
foreach (var item in topItems)
{
DepartModel topModel = new DepartModel();
topModel.id = item.id;
topModel.dname = item.dname;
topModel.fid = item.fid;
LoopToAppendChildren(allModel, topModel);
topModels.Add(topModel);
}
string json = JsonConvert.SerializeObject(topModels);
return json;
}
/// <summary>
/// 继续追溯子类
/// </summary>
/// <param name="allList">全部数据</param>
/// <param name="curItem">当前节点</param>
protected void LoopToAppendChildren(List<DepartModel> allList, DepartModel curItem)
{
var subItems = allList.Where(ee => ee.fid == curItem.id).ToList();
curItem.childs = new List<DepartModel>();
curItem.childs.AddRange(subItems);
foreach (var subItem in subItems)
{
LoopToAppendChildren(allList, subItem);
}
}
#endregion
#region 数据实体类
public class DepartModel
{
public int id;
public string dname;
public int fid;
public List<DepartModel> childs;
}
#endregion
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DepartmentConfig.aspx.cs" Inherits="DepartmentConfig" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server&#