开发者

jquery sort a list according to div element status

I have got a list contains a div on each line. One of the elements of each div is a label. Labels contain in/out information for markers located in a map (also in a shaded area). I want them to be sorted according to labels in/out status. What I have done so far is having the code below. Users can change the scale of the shaded area which changes the status of the markers. Therefore, the list should be updated according to the markers' status on the map. All the markers IN should be on top of the list and the outs should be placed after 'INs'.

**I have got method doing this with 30 lines.I want to use this method.

A is my test loop code to ensure that I am having my in/Out labels html property correctly. B is the code which is not working at the moment. Can anyone familiar please have a look what is missing in the code.

Thanks in advance.

Html Code

<div id="divsec_13A North Buderim Bvd Buderim" name="13A North Buderim Bvd Buderim">
    <input type="checkbox" name="checkbox_13A  North Buderim Bvd Buderim" id="4" value="13A North Buderim Bvd Buderim">
    <a onclick="$.fn.centerMarker('13A  North Buderim Bvd Buderim');">13A  North Buderim Bvd Buderim</a>
    <label type="label" name="inOut_13A  North Buderim Bvd BuderimLabel" id="inOut_13A  North Buderim Bvd BuderimLabel" class="inOutControl">In</label>
</div>

PS: List is sth like this.

checkbox | address1 | In

checkbox | address2 | Out开发者_如何学编程

checkbox | address3 | In

checkbox | address4 | Out

A)

$(this.children()).each(function (ind){                 

      var html = $(this).children().filter(".inOutControl").html();
      alert(html);  
}); 

B)

   $("#list").sort(sortTest);

        var sortTest = function(a,b){   
            //return $(a).find(".inOutControl").html() > $(b).find(".inOutControl").html() ? 1 : -1;        
            return $(this, a).filter(".inOutControl").html() != $(this, b).filter(".inOutControl").html() ? 1 : -1;  
        };

PS: Similar to this, but I don't know why it is not working.


This works..just in case if anyone needs it...

$("#list").sortList();

$.fn.sortList = function(){
        var $list = $(this);
        var listItems = $list.children().get(); 
        listItems.sort( function(a,b){
            return $( a).find(".inOutControl").html() > $( b).find(".inOutControl").html() ? 1 : -1;  
        });
        $.each(listItems, function(idx, itm){$list.append(itm);});
        return this;
    };
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜