2012-04-20 10 views
2

单击复选框旁边节点的文本时,我希望该复选框切换其选中状态。通常这是通过<label for="whatever"></label>完成的。但由于jsTree不使用真正的复选框,这可能会怎么做?在jsTree中,使节点文本可点击的最佳方式是什么(用于检查复选框)

当创建与$('#mytree').jstree({'plugins':['checkbox']})树,如果我把.delegate("a", "click", function (event, data) { $(event.target).find('.jstree-checkbox').click() })结束,它的工作。但是有一个明显的延迟。

因此,我提出了一个问题,点击相应的文本时,检查框的最佳方法是什么?

+0

你已经提到JsTree不使用真正的复选框 - 但这只是默认行为。你知道吗,你可以配置复选框插件使用真正的复选框? (请参阅http://www.jstree.com/documentation/checkbox) – 2012-05-09 17:14:23

+0

是的,但它仍然不适用于该选项。无论如何,通过“使用真正的复选框”,我的意思是它不允许你用复选框和标签构建一个ul/li树,这将是允许标签点击的好方法。 – Raj 2012-05-10 17:02:48

回答

0

我同意你使用的点击处理器技术。我没有看到提到的延迟,但这可能是因为我没有发布任何东西回到服务器。

如果复选框是纯粹的可视化,那么也许该类可以直接修改。这应该比抛出另一个点击事件更快。

$(event.target).parent("li:first").toggleClass("jstree-unchecked").toggleClass("jstree-checked"); 
0

jstree的复选框插件似乎在检查/取消选中节点时执行其他操作。我认为以下应该做你要找的内容:

$(function(){ 
    // Initialize jstree 
    var $tree = $('.tree-target').jstree({ 
     "plugins": ["checkbox"] 
    }); 

    // Bind an event to the anchor tag within each tree entry 
    // Note: '.on' is used here as this will ensure that any 
    // dynamically-generated entries will also use this 'click' 
    // event. 
    $tree.on('click', 'li.tree > a', function (e) { 
     e.stopPropagation(); 

     // Find the first parent 'tree' element 
     var $node = $(this).parents('li.tree').eq(0); 

     // Toggle the state of the current 'tree' element 
     // Note: The third parameter (in this case 'toggle') 
     // can be any value other than true or false 
     $tree.jstree('change_state', $node, 'toggle'); 
     return false; 
    }); 
});​ 

在一个侧面说明,我相信你的方法导致显著延迟的原因可能与你的click事件绑定到每一个<a/>做标记在页面上。这会在尝试查找适当的事件目标时导致大量开销。

相关问题