jQuery MiniUI 開發教程 樹形控制項 樹形:懶載入樹(五)

來源:互聯網
上載者:User

懶載入樹
                    
參考樣本:懶載入樹


                     
建立代碼                   

<ul id="tree1" class="mini-tree" url="../data/TreeService.aspx?method=LoadNodes" style="width:300px;height:200px;padding:5px;"
    showTreeIcon="true" textField="name" idField="id" onbeforeload="onBeforeTreeLoad"
        >       
</ul>
     
          
服務端返回資料

[{
    id: "form",
    text: "Form",
    ......
    isLeaf: false,                            //是否葉子節點:+和-號
    expanded: false                            //節點處於收縮狀態
},
......
]
其中,isLeft 說明此節點是否有下一級節點。expanded 表示此節點處於摺疊狀態。          
          
懶載入事件         

當使用者點擊"+"表徵圖時,會自動載入下一級節點,此時會把當前節點id傳遞到後台,也可以攔截載入事件,增加額外屬性:

function onBeforeTreeLoad(e) {
    var tree = e.sender;    //樹控制項
    var node = e.node;      //當前節點
    var params = e.params;  //參數對象

    //可以傳遞自訂的屬性
    params.myField = "123"; //後台:request對象擷取"myField"
}

          
服務端處理       

服務端通過request擷取"id"屬性後,載入此節點的下一級節點數組,並通過JSON返回。

String id = Request["id"];
if (String.IsNullOrEmpty(id)) id = "-1";

//擷取下一級節點
String sql = "select * from plus_file where pid = '" + id + "' order by updatedate";
ArrayList folders = DBUtil.Select(sql);

//判斷節點,是否有子節點。如果有,則處理isLeaf和expanded。
for (int i = 0, l = folders.Count; i < l; i++)
{
    Hashtable node = (Hashtable)folders[i];
    String nodeId = node["id"].ToString();

    String sql2 = "select * from plus_file where pid = '" + nodeId + "' order by updatedate";
    ArrayList nodes = DBUtil.Select(sql2);

    if (nodes.Count > 0)
    {  www.2cto.com
        node["isLeaf"] = false;
        node["expanded"] = false;
    }

}

//返回JSON
String json = PluSoft.Utils.JSON.Encode(folders);
Response.Write(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.