简体   繁体   中英

JQuery UI Sortable and Laravel, Saving list to Database

I am ultimately trying to save the order of my list to my database, the problem I am having currently is that the Jquery is not receiving the id passed in when an item is moved.

This is my html:

    <ul id="playlist">
        @foreach (Playlist::find(1)->songs as $song)
            <li class="ui-state-default ui-sortable-handle" data-id="{{ $song->position }}"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>{{$song->songname}}.</li>
        @endforeach
     </ul>

This is my JavaScript

$(function() { $('#playlist').sortable().bind('sortupdate', function(e, ui) {

var id = $('ul.sortable li').map(function(){ 
    return $(this).data("id");
}).get();

$.ajax({
    type: "POST",
    url: "api-ch-order",
    dataType: "json",
    data: {id: id},
    success: function(order){
      console.log(id)
    }
});

}); });

This is my controller I had to test it:

    if(Request::ajax()){

      $id = Input::get('id');

      printf($id + "HEYYY");
    }

The thing is at the moment looking at the consoles on both chrome and firefox the console.log(id) is returning an empty array ie no info is being passed, why is this?

I think, the method you are using to apply this functionality not proper. and id attr must be a array not a integer. Might be its can work by changing implementation method.... see https://stackoverflow.com/a/15635201/1728836 with elegent example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM