Javascript Jquery,筛选和隐藏元素,如何将这些“.hidden”元素排序到底

Javascript Jquery,筛选和隐藏元素,如何将这些“.hidden”元素排序到底,javascript,jquery,sorting,Javascript,Jquery,Sorting,目前,我有一个ApacheWicket导航树,其中显示了一个根节点和几个子节点,还有一个用于过滤树项的简单文本输入 过滤后,通过添加css类隐藏不匹配的项。这很有效 在应用过滤器后,我无法将未隐藏的项目排序到顶部,因此在反复尝试了一个多小时后,我想:让我们再次向StagzOverflowz的那些友好和愿意的人询问 更新:我为此发布了一个JSFIDLE: 这棵树看起来像这样: <input type="text" id="filter"/> <div class="navigat

目前,我有一个ApacheWicket导航树,其中显示了一个根节点和几个子节点,还有一个用于过滤树项的简单文本输入

过滤后,通过添加css类隐藏不匹配的项。这很有效

在应用过滤器后,我无法将未隐藏的项目排序到顶部,因此在反复尝试了一个多小时后,我想:让我们再次向StagzOverflowz的那些友好和愿意的人询问

更新:我为此发布了一个JSFIDLE:

这棵树看起来像这样:

<input type="text" id="filter"/>
<div class="navigation"> 
  <wicket:panel>
   -RootNode
     -Child with a link inside
     -Child with a link inside
     -Child with a link inside
     -...
   </wicket:panel>
</div>
在这棵树上面,我有一个简单的文本输入,它在keyup上用javascript过滤这棵树:

function filterList(){
    var count = 0;

    /*
    * for every link item in the navigation tree check, if they match the search entry
    * and add the class ".hidden{ visibility: hidden;}" to it's enclosing parent 
    * element
    */
    jQuery(".navigation a").each(function () {
        if(jQuery(this).text().search(new RegExp(jQuery("#filter").val(),"i"))<0){
            jQuery(this).parents(".wicket-tree-content").addClass("hidden");
        }else{
            jQuery(this).parents(".wicket-tree-content").removeClass("hidden");
            count++;
        }
    });

   //Detach children from the navigation, sort them and append them again
   //(we have a <wicket:panel> element around the children)
   jQuery('.navigation').children().children().detach().sort(
        function(a,b) {
            var condA = jQuery(a).hasClass("hidden");
            var condB = jQuery(b).hasClass("hidden");
            var result =  0;

            if(condA == condB){
             result = -1;
            }
            if(condA != condB){
             result = 1;
            }

            return result;
    }).appendTo(jQuery('.navigation').children());
}

现在的诀窍是分离所有隐藏的元素并将它们附加到末尾


您能在JSFIDLE上发布一个示例吗?不清楚在JSFIDLE上难以说明的错误在哪里,但我会尝试。。。。我主要关心的是为什么排序算法不能正常工作。@NicolaPeluchetti JSFIDLE启动了。我以一种我认为更正确的方式更正了您的排序算法,但它仍然没有给我我想要的结果:未隐藏的值始终在最后我在这里更新了它: