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();
        }

  

 

 

 

   

相关文章