简体   繁体   English

jQuery拖放可排序列表

[英]JQuery Drag and Drop Sortable List

So I have a bit of a problem. 所以我有一个问题。 I have created a drag and drop list within my WordPress plugin that is supposed to allow the user to order the categories, for the most Part I have this working... 我已经在WordPress插件中创建了一个拖放列表,该列表应该允许用户订购类别,在大多数情况下,我都可以使用此列表...

The table is populated from a MySQL database: 该表是从MySQL数据库填充的:

$results = $wpdb->get_results($sql);

    foreach($results as $result)
    {
    ?>
     <tr id="list_item_<?php echo $result->priority_order; ?>" class="list_item">
                <td>
                    <?php echo $result->name; ?>
                </td>
                <td>
                    <?php echo $result->priority_order; ?>
                </td>
            </tr>
    <?php
    }

and I am using 'jquery-ui-sortable' in order to allow the table to be sortable. 并且我正在使用“ jquery-ui-sortable”以允许表可排序。

here is my JQuery for making the list drag-able and to update the list: 这是我的JQuery,用于使列表可拖动并更新列表:

$('.list').sortable(
{
    items: '.list_item',
    opacity: 0.6,
    cursor: 'move',
    axis: 'y',
    update: function()
    {
        var order = $(this).sortable('serialize') + '&action=update_order';
        $.post(ajaxurl, order, function(response)
        {
            //alert(response);
        });
    }
});

here is the PHP used to update the database with the new order: 这是用于以新顺序更新数据库的PHP:

function save_order()
{
  global $wpdb;

  $table_name = $wpdb->prefix . "categories";
  $sql = "SELECT * FROM ".$table_name." ORDER BY priority_order ASC";

  $results = $wpdb->get_results($sql);
  $results = parseObjectToArray($results);

  $new_order = $_POST["list_item"];

  $counter = 0;
  foreach($new_order as $v)
  {
    if(isset($results[$v]["priority_order"]))
    {
        $update_sql = "UPDATE ".$table_name." SET priority_order='".$counter."' WHERE category_id='".$results[$v]["category_id"]."'";
        $wpdb->query($update_sql);

        $counter++;
    }
  }
  die();
}
add_action('wp_ajax_update_order','save_order');

// Converts StdObject to regular array
function parseObjectToArray($object)
{
$array = array();

  foreach($object as $o)
  {
    if (is_object($o))
    {
      $array[] = get_object_vars($o);
    }
  }
  return $array;
}

My problem is that while the list updates perfectly the firs time, the next serialized array that is sent assuming the page has not been manually refreshed is now updating incorrectly. 我的问题是,尽管列表完美地更新了第一时间,但假设页面尚未手动刷新,则发送的下一个序列化数组现在将错误地更新。 This is because the new list and the newly updated one from the database are no longer synchronized. 这是因为数据库中的新列表和新更新的列表不再同步。

This is because I am using the array index to match up what needs updating, but after the initial change, the new list index and the index coming from the array no loner match... 这是因为我正在使用数组索引来匹配需要更新的内容,但是在初始更改之后,新列表索引和来自数组的索引不再匹配...

Does anyone have any idea's on how I can solve this problem? 有人对我如何解决此问题有任何想法吗? I have looked around quite a bit and most of the code Ive found is similar to mine and I cant see any noticeable difference that might cause mine to break. 我已经四处查看了,我发现的大多数代码都与我的相似,并且我看不到任何可能导致我崩溃的明显区别。 Will I need to refresh the page after each update? 每次更新后是否需要刷新页面?

I found the answer myself yet again, haha... 我自己又找到了答案,哈哈...

I was concentrating to much on the pirority_order field, instead I sent each category ID and used SQL to ORDER BY priority_id, then simply with a counter I could assign a new order. 我将精力集中在pirority_order字段上,而是发送了每个类别ID并使用SQL到ORDER BY priority_id,然后只需使用一个计数器就可以分配新的订单。

global $wpdb;

$table_name = $wpdb->prefix . "categories";
$sql = "SELECT * FROM ".$table_name." ORDER BY priority_order ASC";

$results = $wpdb->get_results($sql);
$results = parseObjectToArray($results);

$new_order = $_POST["ebs_list_item"];

$counter = 0;
foreach($new_order as $v)
{
    $update_sql = "UPDATE ".$table_name." SET priority_order=".$counter." WHERE id=".$v;
    $wpdb->query($update_sql);

    $counter++;
}
die();

<tr id="list_item_<?php echo $result->id; ?>" class="list_item">
    <td>
        <?php echo $result->name; ?>
    </td>
    <td>
        <?php echo $result->priority_order; ?>
    </td>
</tr>

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

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