Sort list items into vertically sorted columns with jQuery

[javascript]
// Split Browse Archive checkboxes into vertically alphabetized Omega columns.
var num_cols = 6;
// For each exposed filter that has checkboxes.
$(‘.views-widget .bef-select-as-checkboxes .bef-checkboxes’).each(function() {

// Figure out how many elements should be in each column.
var items_per_col = new Array();
var items = $(this).find(‘.form-item’);
var min_items_per_col = Math.floor(items.length / num_cols);
var difference = items.length – (min_items_per_col * num_cols);
for (var i = 0; i < num_cols; i++) {
if (i < difference) {
items_per_col[i] = min_items_per_col + 1;
} else {
items_per_col[i] = min_items_per_col;
}
}

// Assign the elements to the appropriate column.
for (var i = 0; i < num_cols; i++) {
$(this).append($(‘<ul class="checkbox-column grid-2"></ul>’).addClass(‘checkbox-column grid-2’));
for (var j = 0; j < items_per_col[i]; j++) {
var pointer = 0;
for (var k = 0; k < i; k++) {
pointer += items_per_col[k];
}
$(this).find(‘.checkbox-column’).last().append(items[j + pointer]);
}
}
});
[/javascript]

Leave a Reply