2017-08-17 70 views
3

我試圖用sorteable用戶連接到列表和我有被編碼爲一種形式:JQuery的排序不張貼正確的表單數據

<form action={{ secure_url('send-data') }} method="POST" data-parsley-validate class="form-horizontal form-label-left"> 
    <input type="hidden" name="_token" value="{{ csrf_token() }}"> 
    <div class="row"> 
     <input type="submit" name="Salvar"> 
     <div class="col-md-6 col-sm-6 col-xs-6">Confirmados 
      <ul class="list-group list-group-sortable-connected connectedSortable" id="confirmados">@if(empty($confirmados)) <br /> @endif 
       @foreach ($users as $v) 
       @if (in_array($v->id, $confirmados)) 
       <li class="list-group-item list-group-item-success"><input type="hidden" name="confirmados[]" value="{{$v->id}}">{{$v->nome}}</li> 
       @endif 
       @endforeach 
      </ul> 
     </div> 
     <div class="col-md-6 col-sm-6 col-xs-6">Ausentes 
      <ul class="list-group list-group-sortable-connected connectedSortable" id="ausentes">@if(empty($ausentes)) <br /> @endif 
       @foreach ($users as $v) 
       @if (in_array($v->id, $ausentes)) 
       <li class="list-group-item list-group-item-danger"><input type="hidden" name="ausentes[]" value="{{$v->id}}">{{$v->nome}}</li> 
       @endif 
       @endforeach 
      </ul> 
     </div> 
     <div class="row"> 
      <div class="col-md-12 col-sm-12 col-xs-12">Pendentes 
       <ul class="list-group list-group-sortable-connected connectedSortable" id="pendentes"> 
        @foreach ($users as $v) 
        @if (!in_array($v->id, $ausentes) && !in_array($v->id, $confirmados)) 
        <li class="list-group-item list-group-item-info"><input type="hidden" name="pendentes[]" value="{{$v->id}}">{{$v->nome}}</li> 
        @endif 
        @endforeach 
       </ul> 
      </div> 
     </div> 
    </div> 
</form> 

而我的JS是這樣的:

<script type="text/javascript"> 
$(function() { 
    $("#ausentes, #confirmados, #pendentes").sortable({ 
     connectWith: ".connectedSortable", 
     receive: function(event, ui) { 
      if (ui.sender[0].id === 'pendentes') { 
       ui.item.removeClass("list-group-item-info"); 
      } else if (ui.sender[0].id === 'confirmados') { 
       ui.item.removeClass("list-group-item-success"); 
      } else if (ui.sender[0].id === 'ausentes') { 
       ui.item.removeClass("list-group-item-danger"); 
      } 

      if (this.id === 'pendentes') { 
       ui.item.addClass("list-group-item-info"); 
       ui.item.attr("name", "pendentes[]"); 
      } else if (this.id === 'confirmados') { 
       ui.item.addClass("list-group-item-success"); 
       ui.item.attr("name", "confirmados[]"); 
      } else if (this.id === 'ausentes') { 
       ui.item.addClass("list-group-item-danger"); 
       ui.item.attr("name", "ausentes[]"); 
      } 
      toastr.options.preventDuplicates = true; 
      $.ajax({ 
       url: $('.form-horizontal').attr('action'), 
       data: $('.form-horizontal').serialize(), 
       method: $('.form-horizontal').attr('method'), 
       success:function(data) { 
        $('#ajax').html(data); 

        toastr.success("Alterações salvas com sucesso"); 
       } 
      }); 
     }, 
    }).disableSelection(); 
}); 
</script> 

但是,當我拖放列表中的用戶後,當ajax執行該帖子時,它會繼續發送數據作爲其原始內容。它沒有得到修改。

如果所有用戶都在「pendentes」列表中,即使我更改爲「confirmados」列表,它也會將所有用戶都發送爲「pendentes」。

那是什麼我做了更改後得到:

array:2 [▼ 
    "_token" => "ynDyoSBCQ92jG5r5MKdItiuwq386GKaPU52rg2wv" 
    "pendentes" => array:11 [▼ 
    0 => "20500" 
    1 => "24261" 
    2 => "24908" 
    3 => "24666" 
    4 => "24667" 
    5 => "24263" 
    6 => "24264" 
    7 => "24265" 
    8 => "24266" 
    9 => "24267" 
    10 => "24268" 
    ] 
] 

回答

0

則會持續發送數據,因爲它是原來的內容。它沒有得到修改。

這是因爲使用的是:

$('.form-horizontal').serialize() 

如果你需要,你需要自己序列化對象的數組。例如,您可以選擇所有決案件和建立最終的數組:

var pendentesArr = $('#pendentes :input').serializeArray().reduce(function (acc, ele) { 
     acc.push(ele.value); 
     return acc; 
}, []); 

的片段:

$("#ausentes, #confirmados, #pendentes").sortable({ 
 
    connectWith: ".connectedSortable", 
 
    receive: function (event, ui) { 
 
     if (ui.sender[0].id === 'pendentes') { 
 
      ui.item.removeClass("list-group-item-info"); 
 
     } else if (ui.sender[0].id === 'confirmados') { 
 
      ui.item.removeClass("list-group-item-success"); 
 
     } else if (ui.sender[0].id === 'ausentes') { 
 
      ui.item.removeClass("list-group-item-danger"); 
 
     } 
 

 
     if (this.id === 'pendentes') { 
 
      ui.item.addClass("list-group-item-info"); 
 
      ui.item.attr("name", "pendentes[]"); 
 
     } else if (this.id === 'confirmados') { 
 
      ui.item.addClass("list-group-item-success"); 
 
      ui.item.attr("name", "confirmados[]"); 
 
     } else if (this.id === 'ausentes') { 
 
      ui.item.addClass("list-group-item-danger"); 
 
      ui.item.attr("name", "ausentes[]"); 
 
     } 
 
     //toastr.options.preventDuplicates = true; 
 
     var pendentesArr = $('#pendentes :input').serializeArray().reduce(function (acc, ele) { 
 
      acc.push(ele.value); 
 
      return acc; 
 
     }, []); 
 
     var ausentesArr = $('#ausentes :input').serializeArray().reduce(function (acc, ele) { 
 
      acc.push(ele.value); 
 
      return acc; 
 
     }, []); 
 
     var confirmadosArr = $('#confirmados :input').serializeArray().reduce(function (acc, ele) { 
 
      acc.push(ele.value); 
 
      return acc; 
 
     }, []); 
 
     var dataTobeSent = {_token: $('[name="_token"]').val(), pendentes: pendentesArr, 
 
      ausentes: ausentesArr, confirmados: confirmadosArr}; 
 

 
     console.log('dataTobeSent: ' + JSON.stringify(dataTobeSent)); 
 
     $.ajax({ 
 
      url: $('.form-horizontal').attr('action'), 
 
      data: $.param(dataTobeSent), 
 
      method: $('.form-horizontal').attr('method'), 
 
      success: function (data) { 
 
       $('#ajax').html(data); 
 

 
       toastr.success("Alterações salvas com sucesso"); 
 
      } 
 
     }); 
 
    }, 
 
}).disableSelection();
<link href="https://code.jquery.com/ui/1.12.0/themes/smoothness/jquery-ui.css" rel="stylesheet"/> 
 
<script src="https://code.jquery.com/jquery-1.12.3.min.js"></script> 
 
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script> 
 

 

 

 

 
<form action='' method="POST" data-parsley-validate class="form-horizontal form-label-left"> 
 
    <input type="hidden" name="_token" value="{{ csrf_token() }}"> 
 

 
    <div class="row"> 
 
     <input type="submit" name="Salvar"> 
 

 
     <div class="col-md-6 col-sm-6 col-xs-6">Confirmados 
 
      <ul class="list-group list-group-sortable-connected connectedSortable" id="confirmados"> 
 
       <li class="list-group-item list-group-item-success"><input type="hidden" name="confirmados[]" value="1">1 
 
       </li> 
 
       <li class="list-group-item list-group-item-success"><input type="hidden" name="confirmados[]" value="2">2 
 
       </li> 
 
      </ul> 
 
     </div> 
 
     <div class="col-md-6 col-sm-6 col-xs-6">Ausentes 
 
      <ul class="list-group list-group-sortable-connected connectedSortable" id="ausentes"> 
 
       <li class="list-group-item list-group-item-danger"><input type="hidden" name="ausentes[]" value="a">a 
 
       </li> 
 
       <li class="list-group-item list-group-item-danger"><input type="hidden" name="ausentes[]" value="b">b 
 
       </li> 
 
      </ul> 
 
     </div> 
 
     <div class="row"> 
 
      <div class="col-md-12 col-sm-12 col-xs-12">Pendentes 
 
       <ul class="list-group list-group-sortable-connected connectedSortable" id="pendentes"> 
 
        <li class="list-group-item list-group-item-info"><input type="hidden" name="pendentes[]" value="11">11 
 
        </li> 
 
        <li class="list-group-item list-group-item-info"><input type="hidden" name="pendentes[]" value="12">12 
 
        </li> 
 
        <li class="list-group-item list-group-item-info"><input type="hidden" name="pendentes[]" value="13">13 
 
        </li> 
 
       </ul> 
 
      </div> 
 
     </div> 
 
    </div> 
 
</form>