Loading

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

  

 

 

 

   

posted @ 2014-06-09 17:18  歪头儿在北京  阅读(1670)  评论(0编辑  收藏  举报