• EasyUI TreeGrid DataTable转换数据实现案例


    C#部分

    /// <summary>
        /// Handler1 的摘要说明
        /// </summary>
        public class Handler1 : IHttpHandler
        {
    
            public void ProcessRequest(HttpContext context)
            {
                context.Response.ContentType = "text/plain";
                int OneID = int.Parse(context.Request["ID"]);
                string sql = "SELECT id, OneID, TreeID, Text, Cfj FROM MenuTree where OneID="+OneID+" order by TreeID,reorder,id ";
    
                DataTable dt = DbHelper.QueryDataTable(sql);
                          string json2 = TableToEasyUITreeJson(dt, "TreeID", "0", "id", "Text").Substring(12);
                context.Response.Write(json2.ToString());
            }
    
            /// <summary>
            /// 递归将DataTable转化为适合jquery easy ui 控件tree ,combotree 的 json
            /// 该方法最后还要 将结果稍微处理下,将最前面的,"children" 字符去掉.
            /// </summary>
            /// <param name="dt">要转化的表</param>
            /// <param name="pField">表中的父节点字段</param>
            /// <param name="pValue">表中顶层节点的值,没有 可以输入为0</param>
            /// <param name="kField">关键字字段名称</param>
            /// <param name="TextField">要显示的文本 对应的字段</param>
            /// <returns></returns>
            public static string TableToEasyUITreeJson(DataTable dt, string pField, string pValue, string kField, string TextField)
            {
                StringBuilder sb = new StringBuilder();
                string filter = string.Empty;
                if (pValue.ToString() == "")
                {
                    filter = string.Format("{0} is null", pField);
                }
                else
                {
                    filter = string.Format("{0}='{1}'", pField, pValue);
                }
                            DataRow[] drs = dt.Select(filter);
                if (drs.Length < 1)
                    return "";
                sb.Append(","children":[");
                foreach (DataRow dr in drs)
                {
                    string pcv = dr[kField].ToString();
                    sb.Append("{");
                    sb.AppendFormat(""id":"{0}",", dr[kField].ToString());
                    sb.AppendFormat(""text":"{0}"", dr[TextField].ToString());
                    sb.Append(TableToEasyUITreeJson(dt, pField, pcv, kField, TextField).TrimEnd(','));
                    sb.Append("},");
                }
                if (sb.ToString().EndsWith(","))
                {
                    sb.Remove(sb.Length - 1, 1);
                }
                sb.Append("]");
                return sb.ToString();
    
            }

    html 部分

    <able id="Table1" style="600px;height:400px"></table>  
                        <script type="text/javascript">
                            var dd = '[{"id":"0001","text":"1","children":[{"id":"00010001","text":"4","children":[{"id":"000100010001","text":"7","children":[{"id":"0001000100010001","text":"9","children":[{"id":"00010001000100010001","text":"10","children":[{"id":"000100010001000100010001","text":"11","children":[{"id":"0001000100010001000100010001","text":"12"}]}]}]}]},{"id":"000100010002","text":"8"}]},{"id":"00010002","text":"5"},{"id":"00010003","text":"6"}]},{"id":"0002","text":"2"},{"id":"0003","text":"3"}]';
    
                        $(function(){
                            $('#Table1').treegrid({
                                 url: '../Handler1.ashx?id=2',
                         
                             idField:'id',
                             treeField: 'text',   
                            columns:[[
                            { field: 'text', title: 'text',  200, align: 'left' },
                            ]]
                        });  
    
                        })
                        
        </script>
  • 相关阅读:
    ADO.NET 实体框架 资料收集
    sql server 根据执行计划查询耗时操作
    查看sql server数据库文件信息
    TypeScript
    使用mustache.js 模板引擎输出html
    C#委托多播、Lambda表达、多线程、任务
    PID控制器开发笔记之十三:单神经元PID控制器的实现
    C语言学习及应用笔记之五:C语言typedef关键字及其使用
    君子爱财
    PID控制器开发笔记之十二:模糊PID控制器的实现
  • 原文地址:https://www.cnblogs.com/huzhimin/p/3148562.html
Copyright © 2020-2023  润新知