Bob O wrote:
Basically i have a YUI datatable and i want to use jQuery to hide all
but a user generated number of rows that are random selected

Something like this might be what you're looking for:

    http://jsbin.com/ozafa/edit

The important part is this:

    var $rows = $("#myTable tbody tr");
    $("form").submit(function() {
        var count = parseInt($("#count").val());
        var chosen = 0;
        var total = $rows.length;
        $rows.each(function(index) {
            if (Math.random() < (count - chosen) / (total - index)) {
                $(this).fadeIn("slow");
                chosen++;
            } else {
                $(this).fadeOut("slow");
            }
        });
        return false;
    });

This is different from the previous suggestion, which kept randomly picking rows and if they weren't already chosen, added them to the list. This solution iterates through the rows, updating the probability of each one being chosen according to how many have already been chosen and how many we still want to choose. It might be a bit less efficient for 10 out of 50, but would probably be much more efficient at 40 out of 50, and it is entirely predictable, in that it run exactly one random call for each row in the table.

Cheers,

  -- Scott

Reply via email to