0

I have a main list with two columns. Within these columns, each div has multiple classes, when you select one of those classes, I want to order the columns so they are even. Here's an example of what I'm starting with:

<div class="left">
  <div class="dot blue">blue one</div>
  <div class="dot red">red one</div>
  <div class="dot orange">orange one</div>
  <div class="dot red">red two</div>
  <div class="dot red">red three</div>
</div>
<div class="right">
  <div class="dot red">red four</div>
  <div class="dot blue">blue two</div>
  <div class="dot blue">blue three</div>
  <div class="dot blue">blue four</div>
  <div class="dot orange">orange two</div>
</div>

After clicking a toggling button that I have for "red", I want to hide everything except the red dots, but sort the red ones evenly between the two columns. I can hide everything correctly, but not sure how to make the four red divs even between the "left" and "right" divs. Here's the output I would like to have:

<div class="left">
  <div class="dot red">red one</div>
  <div class="dot red">red two</div>
</div>
<div class="right">
  <div class="dot red">red three</div>
  <div class="dot red">red four</div>
</div>

Thanks in advance.

4

3 回答 3

0

Move all the .reds to the left, then find the halfway point and move the second half of the group back to the right:

$('.left,.right').find('.dot:not(.red)').hide(); /* or .remove() */
var $red = $('.left,.right').find('.dot.red').appendTo('.left');
var len = Math.round($red.length/2);
$('.left .dot.red:gt('+(len-1)+')').appendTo('.right'); /* :gt is zero-based */

http://jsfiddle.net/mblase75/nnhBp/

http://api.jquery.com/gt-selector

于 2013-02-21T16:09:27.597 回答
0

You could use

$(function(){
    var dots = $('.dot'),
        left = $('.left'),
        right = $('.right');

    $('button').on('click', function(){
        var filter = $(this).data('filter'); // here you define which class to remain (on demo i have added a data-filter attribute on the buttons)
        var filtered = dots.detach().filter('.' + filter),
            half = Math.ceil(filtered.length / 2);

        filtered.each(function(index){
            var target = left;
            if (index >= half){
                target = right;
            }
            $(this).appendTo( target );
        });

    });
});

Demo at http://jsfiddle.net/hnaUK/1/

于 2013-02-21T16:10:28.333 回答
0

I know you got some options but I worked on this for a bit and figured I'd share mine too:

This is my jQuery:

    $(document).ready(function () {
        $("button").click(function () {
            var classVal = "." + $(this).val();

            $(".dot").hide();
            $(classVal).show();

            var halfOf = ($(classVal).length / 2)-1;

            $(classVal).appendTo(".left");
            $(classVal+":gt('"+halfOf+"')").appendTo(".right");
        });                
    });

Here is the HTML, all the same but I had to add the buttons you talked about:

<div class="left">
    <div class="dot blue">blue one</div>
    <div class="dot red">red one</div>
    <div class="dot orange">orange one</div>
    <div class="dot red">red two</div>
    <div class="dot red">red three</div>
</div>
<div class="right">
    <div class="dot red">red four</div>
    <div class="dot blue">blue two</div>
    <div class="dot blue">blue three</div>
    <div class="dot blue">blue four</div>
    <div class="dot orange">orange two</div>
</div>
<div id="controls">
    <button value="red" type="button">Red</button>
    <button value="blue" type="button">Blue</button>
    <button value="orange" type="button">Orange</button>
</div>

And then some simple CSS:

.left {
    float: left;
    border: 1px solid #000000;
}
.right {
    float: left;
    border: 1px solid #000000;
}
#controls {
    clear: both;
}

Along with the fiddle:

http://jsfiddle.net/CTCPZ/

于 2013-02-21T17:19:18.873 回答