2010-02-12 30 views
3

我想添加點擊事件到jstree的異步列表項。如何將點擊事件添加到jstree(jQuery插件)異步列表?

理想的結果是:當我單擊jstree中的項目時,項目的內容將作爲參數轉移到sql查詢中,然後執行查詢並在同一頁面中顯示結果集或在另一個頁面中。

雖然我不知道如何實現它。我在jquery.tree.js中找到了以下代碼。我想我應該修改這個事件。但我不知道如何。你能看到代碼並給我一些建議或指導嗎?

$("#" + this.container.attr("id") + " li a") 
.live("click.jstree", function (event) { // WHEN CLICK IS ON THE TEXT OR ICON 
    if(event.which && event.which == 3) return true; 
    if(_this.locked) { 
    event.preventDefault(); 
    event.target.blur(); 
    return _this.error("LOCKED"); 
    } 
    _this.select_branch.apply(_this, [event.target, event.ctrlKey || _this.settings.rules.multiple == "on"]); 
    if(_this.inp) { _this.inp.blur(); } 
    event.preventDefault(); 
    event.target.blur(); 
    return false; 
    }) 

頁代碼:

<script type="text/javascript" > 
    $(function() { 
     $("#async_json_1").tree({ 
      data : { 
       type : "json", 
       opts : { 
        url : "twodimen.php" 
       } 
      }, 
      callback:{ 
       onselect: function(node,tree){ 

       } 
      } 
     }); 
    }); 
</script> 

非常感謝。

回答

5

你可以使用回調方法onselect,通常需要當節點被點擊(即使您還可以通過腳本中選擇)

,如果你的節點(LI)具有以下形式的ID「node_1234地方」,則:

<script type="text/javascript" > 
$(function() { 
    $("#async_json_1").tree({ 
    data : { 
    type : "json", 
    opts : { 
    url : "twodimen.php" 
    } 
    }, 
    callback: { 
     onselect: function(node, tree) { 
     var id = $(node).attr("id").split("_")[1]; // that is 1234 
     $.ajax({ 
      url: "your/url", 
      data: "id="+id, 
      success: function(data){ 
       alert("Great"); 
      } 
     }); 
     } 
    } 
    }); 
}); 
</script> 

我才意識到,那也有做的更簡單的方法,你需要的東西:

<script type="text/javascript" > 
$(function() { 
    $("#async_json_1").tree({ 
    data : { 
    type : "json", 
    opts : { 
    url : "twodimen.php" 
    } 
    } 
    }); 
    $(".tree a").live("click", function(e) { 
    // your code here 
    }) 
}); 
</script> 
+0

@哈寶,謝謝。我需要在onselect部分做一些DOM操作? – 2010-02-12 14:37:01

+0

如果我理解你的問題是正確的,那麼不......你可以簡單地用$(node).attr(「id」)獲得節點id(來自li的id),並使用它作爲ajax調用來執行你的db-詢問 – harpax 2010-02-12 14:40:50

0
.delegate("a","click", function(e) { 
     console.log($(this).parent().attr("id").split("_")[1]); //get ID of clicked node 
    }) 
相關問題