arozwalak
5/19/2014 - 8:48 AM

JavaScript: Split list in columns

JavaScript: Split list in columns

function splitLists (listContainer, list, itemsSelector, newListClasses, listHeight) {
    var countries = list.addClass('temp').find(itemsSelector)
    var elHeight  = 0;
    var newList   = $('<ul class="' + newListClasses + '"/>');
    var listArray = [];
    var colHeight = parseInt((listHeight) ? listHeight : $(list).height() /8);

    elHeight = 0;
    var i = 0;

    countries.each(function(){
        console.log('wys elementu', elHeight);
        elHeight += $(this).height();
        console.log($(this).height(), elHeight, colHeight);

        if (elHeight >= colHeight) {

            if(newList.children().last().hasClass('head')) {
                var head = newList.children().last().remove();
                listArray.push(newList);
                console.log('new list');
                newList = $('<ul class="' + newListClasses + '"/>');
                newList.append(head);                
            }
            else {
                listArray.push(newList);
                console.log('new list');
                newList = $('<ul class="' + newListClasses + '"/>');                
            }
            elHeight = $(this).height();
        }
        newList.append($(this));
    });
    listArray.push(newList);

    for (var i = 0; i < listArray.length; i++) {
        listContainer.append(listArray[i]);
    }
    $('ul.temp').remove();
};