jQuery easyui的tree用法

來源:互聯網
上載者:User

標籤:style   get   資料   os   類   cti   

由於easyui這個架構簡化了不少開發過程,經常被拿來開發後台,但是其實有一個樹型的控制項,用起來稍微複雜一點,總結了一下最基本的用法!首先是前台添加標籤如下:        <ul id="tt">        </ul> 其次添加引用包    <link href="~/Content/jquery-easyui-1.3.6/themes/default/easyui.css" rel="stylesheet" />    <link href="~/Content/jquery-easyui-1.3.6/themes/icon.css" rel="stylesheet" />    <script src="~/Content/jquery-easyui-1.3.6/jquery.min.js"></script>    <script src="~/Content/jquery-easyui-1.3.6/jquery.easyui.min.js"></script> 前台代碼如下:  <script>        isLoad = function () {            $("#tt").tree({                checkbox: false,                url: "/Home/GetArea",                loadFilter: function (data) {                    if (data) {                        alert(data);                        if (data.d){                            return data.d                        } else {                            return data;                        }                    }                }                //formatter: function (node) {                // return node.text;                //}                //animate: true,                //lines:true,                //onClick:function (node) {                // alert(node.text);                //},                //onBeforeExpand: function (node,param) {                // $("tt").tree("options").url = "/Home/GetSubAreaById?parentId="+node.id;                //}             });        }        $(function () {            isLoad();        });    </script> 這裡要說一下,loadfilter加上後就已經可以展示了,主要起過濾作用而如果不要loadfilter只要formatter的話,而可以自已定義需要顯示的欄位  以node.text表示顯示text的值加上animate:true,則展開收縮時會緩慢進行,有動畫的效果加上lines:true,則原先前面顯示的是小三角變為樹形的+號加上onClick:function (node) {                    alert(node.text);                }則可以單擊節點時顯示相應的屬性值 實現的後台代碼,寫了一個treehelper類//有需要可以在拼接上加標屬性等    public class TreeHelper    {        public static string GetDataString(List<TblArea> list, int id)        {            StringBuilder sb = new StringBuilder();            List<TblArea> tempList= list.Where(p=>p.AreaPId==id).ToList();            if (tempList.Count>0)            {                sb.Append("[");                for (int i = 0; i < tempList.Count; i++)                {                    string childString = GetDataString(list,tempList[i].AreaId);                    if (!string.IsNullOrEmpty(childString))                    {                        sb.Append("{ \"id\":\"" + tempList[i].AreaId + "\",\"text\":\"" + tempList[i].AreaName + "\",\"state\":\"closed\",\"children\":");                        sb.Append(childString);                    }                    else                    {                        if (tempList[i].AreaId%2==0)                        {                            sb.Append("{\"id\":\"" + tempList[i].AreaId + "\",\"text\":\"" + tempList[i].AreaName + "\"},");                        }                        else                        {                            sb.Append("{\"id\":\"" + tempList[i].AreaId + "\",\"text\":\"" + tempList[i].AreaName + "\"},");                        }                    }                }                sb.Replace(‘,‘,‘ ‘,sb.Length-1,1);                sb.Append("]},");            }            return sb.ToString();        }    }添加後台實現方法如下        public ContentResult GetArea()        {            nononodeleteImportantEntities db = new nononodeleteImportantEntities();            StringBuilder sb = new StringBuilder();            List<TblArea> province = db.TblArea.ToList();            if (province.Count > 0)            {                sb.Append(TreeHelper.GetDataString(province, 0));                sb = sb.Remove(sb.Length - 2, 2);            }            return Content(sb.ToString());       }    此類主要是用來拼接easyui 能識別的json資料的,為什麼要拼接,1因為官方所提供的就是這樣的,不容易出錯,只是寫起來稍顯費勁後期將試著改用對象直接序列化json,應該可以簡化不少有需要更多功能再去參考官方文檔 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.