Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

137
Visualizações
Multidimensional array pass to Ajax Sortable jQuery

I use sortable jQuery ui function. With this

 $(".draggable-right").sortable({
            connectWith: ".connected-sortable",
    

            update: function(event, ui) {
                var ordine_video = $(this).sortable('toArray', {
                    attribute: 'data-id'
                });


                // POST to server using $.post or $.ajax
                $.ajax({
                    data: {
                        'ordine_video': ordine_video,
                    },
                    type: 'post',
                    dataType: 'json',
                    url: 'save_playlist.php'
                });
                //     alert(idsInOrder);
                console.log(ordine_video);

            },


        }).disableSelection();

I can pass an array with attribute:data-id values in arrays as

["BMPR-00026.mp4", "BMPR-00026.mp4"]

If I want to pass an array like this with key and value

{"BMPR-00026.mp4":"value_1", "BMPR-00026.mp4": "value_2"}

how can do? I have the field in

<li class="ui-sortable-handle" data-id="BMPR-00026.mp4" style="" rel="value_1">element</li>
<li class="ui-sortable-handle" data-id="BMPR-00026.mp4" style="" rel="value_2">element</li>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Consider the following.

function serializeList(list) {
  var results = {};
  $("li", list).each(function(i, el) {
    results[$(el).attr("data-id")] = $(el).attr("rel");
  });
  return results;
}

$(".draggable-right").sortable({
  connectWith: ".connected-sortable",
  update: function(event, ui) {
    var ordine_video = serializeList(this);
    $.ajax({
      data: {
        'ordine_video': ordine_video,
      },
      type: 'post',
      dataType: 'json',
      url: 'save_playlist.php'
    });
    console.log(ordine_video);
  }
}).disableSelection();

This creates a Function, that you can pass a List to and it will iterate each List Item to build an Object.

There is a caveat, each data-id must be unique. Like so:

{
  "BMPR-00026.mp4": "value_1",
  "BMPR-00027.mp4": "value_2"
}

If they are the same, the value of that Index will be updated instead of adding a new entry.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda