zoukankan      html  css  js  c++  java
  • ztree + ashx +DataTable +Oracle

        问题描述

           好久没有使用ztree了,刚才在使用ztree做导航时遇到了几个小问题:

               1、返回数据源是undefined 。

               2、数据出现后树结构没有出现(pIdKey单词拼写错误).

               3、在使用Oracle查询时,Oracle将所有列名转化为大写,我在JSON处理过程中手动将字段处理成小写。

     js代码:    

    <script type="text/javascript">
            var selectNode; // ztree选中节点
            var treeObj;
            var settings = {
                data: {
                    key: {
                        name: "name"
                    },
                    simpleData: {
                        enable: true,
                        idKey: "id",
                        pIdKey: "pid"
    
                    }
                },
                callback: {
    
                }
            };
    
            function AjaxZtree() {
                $.ajax({
                    type: 'GET',
                    url: '../Analysis/Handler/Tree.ashx?action=report',
                    cache: true,
                    async: false,
                    dataType: "text",
                    ContentType: "application/json; charset=utf-8",
                    success: function (data) {
                        $.fn.zTree.init($("#ReportTree"), settings, (new Function('return' + data))());
                        treeObj = $.fn.zTree.getZTreeObj("ReportTree");
                    },
                    error: function () {
                        alert('Error');
                    }
                });
            }
    
            $(document).ready(function () {
                AjaxZtree();
            });
        </script>
    

    ashx代码

    public void ProcessRequest(HttpContext context)
            {
                context.Response.ContentType = "text/plain";
                string action = context.Request.QueryString["action"];
    
                if (action == "report")
                {
                    var result = reportibll.ZtreeJSON();
                    string json = ConvertJson.ToJson(result);
                    context.Response.Write(json);
                }
                else
                { 
                    
                }
    
                
            }
    

    JSON处理代码

     public static string ToJson(DataTable dt)
            {
                StringBuilder jsonString = new StringBuilder();
                jsonString.Append("[");
                DataRowCollection drc = dt.Rows;
                for (int i = 0; i < drc.Count; i++)
                {
                    jsonString.Append("{");
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        string strKey = dt.Columns[j].ColumnName.ToLower();
                        string strValue = drc[i][j].ToString();
                        Type type = dt.Columns[j].DataType;
                        jsonString.Append(""" + strKey + "":");
                        strValue = StringFormat(strValue, type);
                        if (j < dt.Columns.Count - 1)
                        {
                            jsonString.Append(strValue + ",");
                        }
                        else
                        {
                            jsonString.Append(strValue);
                        }
                    }
                    jsonString.Append("},");
                }
                jsonString.Remove(jsonString.Length - 1, 1);
                jsonString.Append("]");
                return jsonString.ToString();
            }
    

      

       

  • 相关阅读:
    经常使用排序算法时间复杂度和空间复杂度简析
    Android触碰事件
    opencv矩阵运算(2)
    [ACM] HDU 1400 Mondriaan&#39;s Dream (状态压缩,长2宽1长方形铺满)
    指针知识梳理8- 指针的指针
    Git学习笔记(一)
    Object-c Associated Object
    YTUOJ-计算该日在本年中是第几天(用户自己定义类型)
    MYSQL源代码编译的变动
    手机端小问题整理
  • 原文地址:https://www.cnblogs.com/sword-successful/p/3778318.html
Copyright © 2011-2022 走看看