zTree控件的使用

最常用的使用方式是json格式

.net递归实现对象生成json格式字符串

代码:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using Newtonsoft.Json;

using SubSonic;

using System.Data;

using System.Text;



namespace WebApplication1

{

    public partial class Test2 : System.Web.UI.Page

    {

       

        protected void Page_Load(object sender, EventArgs e)

        {



            if (!IsPostBack)

            {

                ss();

            }

        }

        protected string JsonString { set; get; }





        /* [

       {name:"test1", open:true, children:[

          {name:"test1_1"}, {name:"test1_2"}]},

       {name:"test2", open:true, children:[

          {name:"test2_1"}, {name:"test2_2"}]}

       ];

         * */

        List<MyTree> list = new List<MyTree>();



        public void ss()

        {



            string sql = "  SELECT * FROM Test1.DBO.ceshi where Fidh='ROOT' ";

            DataTable dt1 = new DataTable();

            QueryCommand qc = new InlineQuery("Test1").GetCommand(sql);

            dt1 = DataService.GetDataSet(qc).Tables[0];



            for (int i = 0; i < dt1.Rows.Count; i++)

            {

                MyTree rootTree = new MyTree();

                rootTree.name = dt1.Rows[i]["Name"].ToString();

                aa(dt1.Rows[i]["Id"].ToString(),rootTree);

                list.Add(rootTree);

            }

            string s = JsonConvert.SerializeObject(list);

            JsonString = s;



        }



        public void aa(string id,MyTree rootTree)

        {  

        

            string sql1 = "  SELECT * FROM Test1.DBO.ceshi where Fidh='" + id + "' ";

            DataTable dt = new DataTable();

            QueryCommand qc1 = new InlineQuery("Test1").GetCommand(sql1);

            dt = DataService.GetDataSet(qc1).Tables[0];

            if (dt.Rows.Count > 0)

            {

                rootTree.children = new List<MyTree>();

                for (int j = 0; j < dt.Rows.Count; j++)

                {

                    MyTree childTree = new MyTree();

                    childTree.name = dt.Rows[j]["Name"].ToString();

                    aa(dt.Rows[j]["Id"].ToString(), childTree);

                    rootTree.children.Add(childTree);



                }

            }



            

                

        }



    }



    public class MyTree

    {

        public string name { get; set; }

        

        public List<MyTree> children { get; set; }

    }







   

}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test2.aspx.cs" Inherits="WebApplication1.Test2" %>



<!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">

    <title></title>

    <link href="css/demo.css" rel="stylesheet" type="text/css" />

    <link href="css/zTreeStyle/zTreeStyle.css" rel="stylesheet" type="text/css" />

    <script src="js/jquery-1.4.4.min.js" type="text/javascript"></script>

    <script src="js/jquery.ztree.core-3.5.js" type="text/javascript"></script>

    <script type="text/javascript">

        var setting = {};

        

//        var zNodes = [

//            { name: "父节点1 - 展开"

//                children: [

//                    { name: "父节点11 - 折叠",

//                        children: [

//                            { name: "叶子节点111" },

//                            { name: "叶子节点112" },

//                            { name: "叶子节点113" },

//                            { name: "叶子节点114" }

//                        ]

//                    },

//                    { name: "父节点12 - 折叠",

//                        children: [

//                            { name: "叶子节点121" },

//                            { name: "叶子节点122" },

//                            { name: "叶子节点123" },

//                            { name: "叶子节点124" }

//                        ]

//                    },

//                    { name: "父节点13 - 没有子节点" }

//                ]

//            },

//            { name: "父节点2 - 折叠",

//                children: [

//                    { name: "父节点21 - 展开", open: true,

//                        children: [

//                            { name: "叶子节点211" },

//                            { name: "叶子节点212" },

//                            { name: "叶子节点213" },

//                            { name: "叶子节点214" }

//                        ]

//                    },

//                    { name: "父节点22 - 折叠",

//                        children: [

//                            { name: "叶子节点221" },

//                            { name: "叶子节点222" },

//                            { name: "叶子节点223" },

//                            { name: "叶子节点224" }

//                        ]

//                    },

//                    { name: "父节点23 - 折叠",

//                        children: [

//                            { name: "叶子节点231" },

//                            { name: "叶子节点232" },

//                            { name: "叶子节点233" },

//                            { name: "叶子节点234" }

//                        ]

//                    }

//                ]

//            },

//            { name: "父节点3 - 没有子节点", isParent: true }



//        ];



         var zNodes =<%=JsonString %>;



        $(document).ready(function () {

            $.fn.zTree.init($("#treeDemo"), setting, zNodes);

        });



  



    

    </script>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <ul id="treeDemo" class="ztree">

        </ul>

    </div>

    </form>

</body>

</html>

 

你可能感兴趣的:(ztree)