Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert single column to multiple columns

I have this function

function renderListSelecoes(data) {
    // JAX-RS serializes an empty list as null, and a 'collection of one' as an object (not an 'array of one')
    var list = data == null ? [] : (data.selecoes instanceof Array ? data.selecoes : [data.selecoes]);

    $('#selecaoList <tr><td>').remove();
    $.each(list, function(index, selecao) {
        $('#selecaoList').append('<tr><td><a href="#" data-identity="' + selecao.id_selecao + '">'+selecao.nome+'</a></td></tr>');
    });
}

that´s insert into each row in a table something, like this:

Brasil
Argentina
Colômbia
Uruguai
Suíça
Argélia
Costa do Marfim
Gana

how can I convert this to for example 2columns, like this:

Brasil         Costa do Marfim
Argentina      Gana
Colômbia       Argélia
Uruguai        Suiça

here is part of my code in html:

<div class="table-responsive container" >
        <table class="table">
            <tbody id="selecaoList">

            </tbody>
        </table>
    </div>
like image 638
seal Avatar asked Mar 27 '26 01:03

seal


1 Answers

Here is how you can do it:

function renderListSelecoes(data) {
    // JAX-RS serializes an empty list as null, and a 'collection of one' as an object (not an 'array of one')
    var list = data == null ? [] : (data.selecoes instanceof Array ? data.selecoes : [data.selecoes]);

    $('#selecaoList').empty();
    var tr = $( '<tr/>' ),
        td = $( '<td/>' ),
        row;
    $.each(list, function(index, selecao) {
        if( index % 2 === 0 ) {
            row = tr.clone();
            row.html( td.clone().html( selecao.nome ) );
        } else {
            row.append( td.clone().html( selecao.nome ) );
            $('#selecaoList').append( row );
        }
    });
}
like image 129
PeterKA Avatar answered Mar 28 '26 14:03

PeterKA



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!