jQuery: What to do with the list that sortable('serialize') returns?

前端 未结 8 1502
我寻月下人不归
我寻月下人不归 2020-12-23 20:01

With jQuery I\'m retrieving positions of a sortable list using \'serialize\', like this:

var order = $(\'ul\').sortable(\'serialize\');

The vari

8条回答
  •  半阙折子戏
    2020-12-23 20:31

    Lets say you were ordering news items, and your page sent this to "?id[]=2&id[]=3&id[]=1&id[]=4&id[]=5" your php code.

    $_POST['id'] # would be [2,3,1,4,5]
    
    // Now we need to update the position field of these items    
    
    foreach($_POST['id'] as $i=>$id ):
       // For our first record, $i is 0, and $id is 2.
    
       $post = Post::get($id); # Do you own database access here
       $post->position = $i; # Set the position to its location in the array
       $post->save(); # Save the record
    endforeach
    

提交回复
热议问题