DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> JavaScript綜合知識 >> Javascript樹型菜單的方法
Javascript樹型菜單的方法
編輯:JavaScript綜合知識     

 看到園友發表了一篇關於JS樹的文章,看了之後,覺得應該可以精簡不少代碼,

想到了以前做的項目裡面用到了一個JS樹,於是試著修改了一下,現把代碼貼出來與大家共享,還請大家多扔磚頭!

 

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

    <script src="JS/Tree.js" type="text/javascript"></script>

</head>

<body>

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

    <div>

<%--    <asp:TreeView ID="TreeView1" runat="server" ShowLines="True">

    </asp:TreeView>--%>

    <div id="treeOrgan"></div>

    </div>

    </form>

    <script type="text/javascript" language="javascript">

        var tree;

        window.onload = function()

        {

            if (tree != "") {

                tree = new treeview("treeview", "../images/Tree", false, false);

                var parNode = new node("人員列表", "人員列表", "", "0", "F", false, "");

                //caption, title, url, target, tag,check,callback

                tree.add(parNode);

                tree.create(document.getElementById("treeOrgan"));

 

                InitNode(0, parNode);

                parNode.expand();

            }

        }

        function InitNode(id, parNode) {

            if (parNode.nodes.length > 0) {

                parNode.toggle();

            }

            else {

                //parNode.DeleteChildren();

                var result = WebApplicationJsTree._Default.GetJson(id).value;

                if (result != null && result != "") {

                    var data = eval('(' + result + ')');

                    for (var i = 0; i < data.length; i++) {

                        var pn = new node(data[i].NAME, data[i].NAME, "", "P", data[i].id, true, "");

                        parNode.add(pn);

                    }

                }

            }

        }

        treeview.prototype.onnodeclick = function(sender) {

            InitNode(sender.tag, sender);

            sender.select();

            sender.expand();

            return false;

        }

</script>

</body>

</html>

其實個人覺得用TreeView+linq to list也就連接一次服務器,對於小數據量性能也應該蠻高的,代碼也在源碼包裡面

在與客戶端交互上面,用服務器控件可以加上

tn.Text = "<span onclick="treeNodeClick(" + tn.ID + ")">" + tn.Name;

數據庫結構截圖:

Javascript樹型菜單的方法

FF下截圖:

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved