EasyUI - 操作 Tree 控制項

來源:互聯網
上載者:User

標籤:

效果:

 

HTML代碼:

  • 使用了模板頁
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">    <div id="Accordions" class="easyui-accordion">        <div title="所有使用者" data-options="iconCls:‘icon-save‘,selected:true" style ="padding:0 0 0 10px;">            <ul id="Tree"></ul>        </div>        <div title="密碼操作" data-options="iconCls:‘icon-reload‘">            content2         </div>        <div title="搜尋操作" data-options="iconCls:‘icon-reload‘">            content3         </div>    </div></asp:Content>

 

JavaScript代碼:

$(function () {    $(‘#Tree‘).tree({        url: "Json/Tree_Node.ashx",        datatype: JSON,        lines: true,        animate: true,        onClick: function (node) {            alert(node.text + node.id);        }    });})

 

ashx代碼(一般處理常式):

using System.Data;using System.Web;using EasyUI.DAL;using System.Text;namespace EasyUI.Json{    /// <summary>    /// Tree_Node 的摘要說明    /// </summary>    public class Tree_Node : IHttpHandler    {        DataSet ds = new DataSet();        public void ProcessRequest(HttpContext context)        {            context.Response.ContentType = "text/plain";            DataTable dt = SelectDT();            string json = GetTreeJsonByTable(dt, "ID", "CategoryName", "Url", "ParentID", "1000");            context.Response.Write(json);            context.Response.End();        }        #region 根據DataTable產生EasyUI Tree Json樹結構        StringBuilder result = new StringBuilder();        StringBuilder sb = new StringBuilder();        /// <summary>          /// 根據DataTable產生EasyUI Tree Json樹結構          /// </summary>          /// <param name="tabel">資料來源</param>          /// <param name="idCol">ID列</param>          /// <param name="txtCol">Text列</param>          /// <param name="url">節點Url</param>          /// <param name="rela">關係欄位</param>          /// <param name="pId">父ID</param>          private string GetTreeJsonByTable(DataTable tabel, string idCol, string txtCol, string url, string rela, object pId)        {            result.Append(sb.ToString());            sb.Clear();            if (tabel.Rows.Count > 0)            {                sb.Append("[");                string filer = string.Format("{0}=‘{1}‘", rela, pId);                DataRow[] rows = tabel.Select(filer);                if (rows.Length > 0)                {                    foreach (DataRow row in rows)                    {                        sb.Append("{\"id\":\"" + row[idCol] + "\",\"text\":\"" + row[txtCol] + "\",\"attributes\":\"" + row[url] + "\",\"state\":\"open\"");                        if (tabel.Select(string.Format("{0}=‘{1}‘", rela, row[idCol])).Length > 0)                        {                            sb.Append(",\"children\":");                            GetTreeJsonByTable(tabel, idCol, txtCol, url, rela, row[idCol]);                            result.Append(sb.ToString());                            sb.Clear();                        }                        result.Append(sb.ToString());                        sb.Clear();                        sb.Append("},");                    }                    sb = sb.Remove(sb.Length - 1, 1);                }                sb.Append("]");                result.Append(sb.ToString());                sb.Clear();            }            return result.ToString();        }        #endregion        #region 建立資料        /// <summary>        /// 擷取資料庫中分類資料        /// </summary>        /// <returns></returns>        protected static DataTable SelectDT()        {            DataTable dt = new System.Data.DataTable();            dt = SQLHelper.ExecuteTable("select ID, CategoryName, Url, ParentID from Tb_APCategory", System.Data.CommandType.Text);            return dt;        }        #endregion        public bool IsReusable        {            get            {                return false;            }        }    }}

 

格式化資料為Json資料(核心代碼,從上面截取):

#region 根據DataTable產生EasyUI Tree Json樹結構StringBuilder result = new StringBuilder();StringBuilder sb = new StringBuilder();/// <summary>  /// 根據DataTable產生EasyUI Tree Json樹結構  /// </summary>  /// <param name="tabel">資料來源</param>  /// <param name="idCol">ID列</param>  /// <param name="txtCol">Text列</param>  /// <param name="url">節點Url</param>  /// <param name="rela">關係欄位</param>  /// <param name="pId">父ID</param>  private string GetTreeJsonByTable(DataTable tabel, string idCol, string txtCol, string url, string rela, object pId){    result.Append(sb.ToString());    sb.Clear();    if (tabel.Rows.Count > 0)    {        sb.Append("[");        string filer = string.Format("{0}=‘{1}‘", rela, pId);        DataRow[] rows = tabel.Select(filer);        if (rows.Length > 0)        {            foreach (DataRow row in rows)            {                sb.Append("{\"id\":\"" + row[idCol] + "\",\"text\":\"" + row[txtCol] + "\",\"attributes\":\"" + row[url] + "\",\"state\":\"open\"");                if (tabel.Select(string.Format("{0}=‘{1}‘", rela, row[idCol])).Length > 0)                {                    sb.Append(",\"children\":");                    GetTreeJsonByTable(tabel, idCol, txtCol, url, rela, row[idCol]);                    result.Append(sb.ToString());                    sb.Clear();                }                result.Append(sb.ToString());                sb.Clear();                sb.Append("},");            }            sb = sb.Remove(sb.Length - 1, 1);        }        sb.Append("]");        result.Append(sb.ToString());        sb.Clear();    }    return result.ToString();}#endregion
View Code

 

完成。

EasyUI - 操作 Tree 控制項

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.