简体   繁体   English

使用Laravel 4+从jQuery / ajax保存序列化的可排序数据

[英]Saving serialised sortable data from jQuery/ajax using Laravel 4+

Having an issue working out the best way to save an ajax sortable list into my database. 有一个问题是将ajax可排序列表保存到我的数据库中的最佳方法。 I'm using HTML5Sortable and Laravel 4.2 framework. 我正在使用HTML5Sortable和Laravel 4.2框架。 The basic end result I'm trying to achieve is for a user to be able to click and drag the list items and reorder them and this new order will be saved in the database in a field called "order" so that when they return to the page later the new order will be present. 我想要实现的基本最终结果是用户能够单击并拖动列表项并对它们重新排序,这个新订单将被保存在数据库中的一个名为“order”的字段中,这样当它们返回时该页面稍后将出现新订单。

I have a list of chapters on the page like so: 我在页面上有一个章节列表,如下所示:

<ul class="sortable">
    @foreach ($chapters as $chapter)
      <li class="ui-state-default" data-order="{{ $chapter->order }}">{{ $chapter->title }}</li>
    @endforeach
</ul>

And I can drag and drop sort these using HTML5Sortable and then serialize this data and submit it via ajax POST, which fires off each time the list is reordered like so: 我可以使用HTML5Sortable对这些进行拖放排序,然后将这些数据序列化并通过ajax POST提交,每次重新排序列表时都会触发,如下所示:

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

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

    var uuid = "<?php echo$uuid; ?>";

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

My controller at /api-ch-order gets the data and saves the new order like so: 我的/ api-ch-order控制器获取数据并保存新订单,如下所示:

if(Request::ajax()){

  $uuid = Input::get('uuid');
  $order = Input::get('order');

  $i = 1;

  foreach($order as $position) {

    $chapter = Chapter::where('book_uuid', $uuid)->where('order', $position)->first();

    $chapter->order = $i;
    $chapter->save();

    $i++;

  }

}

THE PROBLEM 问题

The issue I'm having with this approach is that it wont work in some circumstances, the loop causes problems. 我使用这种方法的问题是它在某些情况下不起作用,循环会导致问题。

eg 例如

LOOP 1: FIND Chapter where order = 2 and change order to 1 循环1:查找订单= 2并将顺序更改为1的章节

LOOP 2: FIND Chapter where order = 1 and change it to 2 循环2:查找订单= 1的章节并将其更改为2

The problem here is that in LOOP 2 it is finding result of LOOP 1 and updating this instead... I think... This problem is confusing me quite a bit... But I'm 99% sure the foreach loop in my controller is the problem and I'm not sure the best approach to fix this. 这里的问题是在LOOP 2中它找到了LOOP 1的结果而更新了这个......我想......这个问题让我很困惑......但我99%肯定我的foreach循环控制器是问题,我不确定解决这个问题的最佳方法。

Resources to link to this question: 链接到此问题的资源:

THE ANSWER!!! 答案!!!

As r4xz mentioned, the answer is to use ID as the constant. 正如r4xz所提到的,答案是使用ID作为常量。

Eg 例如

HTML HTML

<ul class="sortable">
    @foreach ($chapters as $chapter)
      <li class="ui-state-default" data-id="{{ $chapter->id }}">{{ $chapter->title }}</li>
    @endforeach
</ul>

JS JS

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

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

    var uuid = "<?php echo$uuid; ?>";

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

PHP CONTROLLER PHP控制器

if(Request::ajax()){

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

  $i = 1;

  foreach($id as $val) {

    $chapter = Chapter::where('id', $val)->first();

    $chapter->order = $i;
    $chapter->save();

    $i++;

  }

}

The most common solution is to save position and entity id: 最常见的解决方案是保存位置和实体ID:

<li data-id="{{ $chapter->id }}" data-order="{{ $chapter->order }}">{{ $chapter->title }}</li>

Then in controller you simply use: 然后在控制器中你只需使用:

UPDATE chapter SET order = $order WHERE id = $id

This question was very helpfull and gives a selfanswer into it ! 这个问题非常有用,并给出了自我解决方案! GREAT ! 太棒了! Thanks ! 谢谢 !

Just for those in my case not using the same structure. 只是为了我的情况下不使用相同的结构。 This is what I did : 这就是我做的:

public function myRouteFunction(Request $request)
    foreach($ids as $key => $id) {
        $update = DB::table('mybase')->where('id', $id)->update(['sort_order' => $key]);
    }
}

Maybe this could help someone :) 也许这可以帮助别人:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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