C#无限分级实现,前端WEB页面接收,后台提供层级Json数据

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&#
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

smartsmile2012

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值