ztree的使用

来源:互联网 发布:qq空间网络音乐 编辑:程序博客网 时间:2024/05/01 03:53

前台

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="WebForzTree.Index" %>

<!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="Styles/zTreeStyle/zTreeStyle.css" rel="stylesheet" type="text/css" />
    <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
    <script src="Scripts/jquery.ztree.core-3.5.js" type="text/javascript"></script>

 

<script type="text/javascript">

  function showIconForTree(treeId, treeNode) {
            return !treeNode.isParent;
        };
        function zTreeOnClick(event, treeId, treeNode) {
            alert(treeNode.cId);
        }
        var setting = {
            callback: {
                onClick: zTreeOnClick
            },
            view: {
                showIcon: showIconForTree
            },
            data: {
                key: {
                    children: "children",
                    name: "cName",
                    title: "",
                    url: "url"
                },
                simpleData: {
                    enable: true
                }
            }
        };
        $(document).ready(function () {

            $.ajax({
                type: "post",
                contentType: "application/json",
                url: "Index.aspx/FindAll",
                data: "{}",
                dataType: "json",
                success: function (result) {
                    var zNodes = result.d; //接受webservice里面传过来的list
                    $.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>

后台

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Services;
using System.Data.SqlClient;
using System.Configuration;
using System.Text;

namespace WebForzTree
{
    public partial class Index : System.Web.UI.Page
    {
        private static string connectionString = ConfigurationManager.ConnectionStrings["DBConnString"].ToString();
        public string NodeData;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            { }
        }

        ///webservice里面的一个方法,在jquery用ajax方法调用;
        [WebMethod]
        public static List<dCategory> FindAll()
        {
            Index ind = new Index();
            SqlConnection connection = new SqlConnection(connectionString);
            string sqlText = "select * from dCategory where cParentId=0";
            List<dCategory> list = new List<dCategory>();
            list =ind.GetCategoryForParentID(sqlText);
            ind.GetSeasons(ref list);
            return list;
        }
        private List<dCategory> GetCategoryForParentID(string sql)
        {
            List<dCategory> user = new List<dCategory>();
            SqlDataReader reader = null;
            reader = ExecuteReader(sql);
            while (reader.Read())
            {
                user.Add(ObjToListC(reader));
            }
            return user;
        }
        public void GetSeasons(ref List<dCategory> list)
        {
            foreach (dCategory season in list)
            {
                //通过上级ID获取子级,然后添加到Children中
                List<dCategory> lstSeason = GetCategoryForChildID(season.cId);
                if (lstSeason.Count > 0)
                {
                    season.children = lstSeason;
                    GetSeasons(ref lstSeason);
                }
            }
        }
        public List<dCategory> GetCategoryForChildID(string dParentId)
        {
            List<dCategory> result = new List<dCategory>();
            SqlDataReader sqlReader = null;
            string strSql = String.Format(@"select * from dCategory where cParentId={0}", dParentId);
            sqlReader = ExecuteReader(strSql);
            while (sqlReader.Read())
            {
                result.Add(ObjToListC(sqlReader));
            }
            sqlReader.Close();
            return result;
        }
        #region MyRegion
        private dCategory ObjToListC(SqlDataReader reader)
        {
            dCategory model = new dCategory();
            if (reader != null)
            {
                if (reader["cId"] != null && reader["cId"].ToString() != "")
                {
                    model.cId = reader["cId"].ToString();
                }
                if (reader["cName"] != null && reader["cName"].ToString() != "")
                {
                    model.cName = reader["cName"].ToString();
                }
                if (reader["cParentId"] != null && reader["cParentId"].ToString() != "")
                {
                    model.cParentId = reader["cParentId"].ToString();
                }
            }
            return model;
        }
        private SqlDataReader ExecuteReader(string strSQL)
        {
            SqlConnection connection = new SqlConnection(connectionString);
            SqlCommand cmd = new SqlCommand(strSQL, connection);
            try
            {
                connection.Open();
                SqlDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                return myReader;
            }
            catch (System.Data.SqlClient.SqlException e)
            {
                throw e;
            }
        }
        #endregion
    }

}

 

 

原创粉丝点击