构建组件后的Fire事件

时间:2013-09-18 18:22:27

标签: jquery json jqtree

我正在以这种方式在项目中使用jqTree

  1. 使用“.root”类动态添加“p”元素到页面。
  2. 单击按钮时,为每个'p.root'调用jqTree 元件。
  3. 添加每个li后,使用onCreateLi事件添加id 处理程序。
  4. 创建所有树之后,添加一个类以突出显示那些 发生不止一次。
  5. 最后一步是我遇到麻烦的地方。这是我正在使用的代码:

            $('#compare-button').click(function(event){
                event.preventDefault();
                var last = false;
                $('#output p.root').each(function(){
                    if ($(this).is(':last-child'))
                    { last = true; } // set last = true on the last p.root
                    $(this).tree({
                        dataUrl: 'http://path/to/service/',
                        autoOpen: true,
                        dragAndDrop: false,
                        onCreateLi: function(node, $li){
                            $li.attr("value", node.id);
    
                            if (total_count.hasOwnProperty(node.id)) {  total_count[node.id]++; }
                            else {  total_count[node.id] = 1; }
    
                            if (last && $li.is(':last-child')) // Once we're done with our calls to .tree(), fire the highlighting code.
                            {
                                $.each(total_count, function(key, value){
                                    if (value > 1) { $('li[value="' + key +'"] span').addClass('highlight'); }
                                });
                            }
                        } // end of onCreateLi
                    }) // end of tree() 
                }); // end of each()                
            }); // end of click()
    

    问题是'onCreateLi'在每个li之后触发,因此它刚创建的那个总是是最后一个兄弟。我还在.each()的末尾尝试了一个.load(),但它似乎打断了树的构建(我猜它在构建树之前就被触发了)。

    如果jqTree有.afterLoad()事件处理程序,这会容易得多。

    编辑:等等,这实际上可能正常......

    编辑2:在Firefox中运行,而不是在IE中运行。

    编辑3:我将以不同的方式提出这个问题,因此更多不熟悉jqTree的jQuery专家可以回答这个问题。我正在使用jQuery.each()循环遍历每个'p.root'元素。完成循环后,我需要激活一些代码。我尝试了jQuery.each()。after(),但它似乎打断了树的构建。

1 个答案:

答案 0 :(得分:0)

也许您可以使用tree.init事件。在加载并呈现树之后调用该事件。