Pergunta

I have a puzzle that has gone beyond me as I thought I understood how this works but clearly I don't. The function below has columns.push($('#squad'+count).val()); which contains "Squad " +count so you get Squad 1 2 3 4 5 etc etc. The next push contains names, Bob, Joe, John, Mark, etc etc.

So right now the below produces:

Squad 1|Bob,Joe,John,Mark|Squad 2|Cletus,Ray ray,Billy Joe|Squad 3|Fred,Barney,Wilma

I want it to output:

Squad 1,Bob,Joe,John,Mark|Squad 2,Cletus,Ray ray,Billy Joe|Squad 3,Fred,Barney,Wilma

I can't for the life of me figure this out. And I know its something simple but hours of searching has led me here. Any help is appreciated.

function getItems(exampleNr)
{
    var count = 0;
    var columns = [];
    $(exampleNr + ' ul.sortable-list').each(function(){
        columns.push($('#squad'+count).val());
        columns.push($(this).sortable('toArray').join(','));
        count++;
    });

    return columns.join('|');
}
Foi útil?

Solução

You're joining Squad # to the same columns array. The code you had before produced the result it did because columns is

  • Squad 1
  • Bob,Joe,John,Mark
  • Squad 2
  • Cletus,Ray ray,Billy Joe
  • Squad 3
  • Fred,Barney,Wilma

Which is why they were separated with pipes (|).

To fix it as you want it, you'll want Squad # along with the other names. You can achieve this by changing both columns.push lines to this one line:

columns.push( [$('#squad'+count).val()].concat($(this).sortable('toArray')).join(',') );

Effectively creating an array with the Squad # and concatenating the sorted ones at the end before joining them together with ,.

Outras dicas

Just combine the two pushs and place both data into one.

function getItems(exampleNr)
{
    var count = 0;
    var columns = [];
    $(exampleNr + ' ul.sortable-list').each(function(){
        columns.push($('#squad'+count).val()+','+$(this).sortable('toArray').join(','));
        count++;
    });

    return columns.join('|');
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top