jQuery UI可排序,然后将顺序写入数据库


jQuery UI Sortable, then write order into a database

我想使用jQuery UI sortable函数允许用户设置订单,然后在更改时,将其写入数据库并更新它。有人可以写一个例子来说明这是如何做到的吗?

jQuery UI sortable特性包含一个serialize方法来完成此操作。这真的很简单。下面是一个快速示例,一旦元素的位置发生变化,就会将数据发送到指定的URL。

$('#element').sortable({
    axis: 'y',
    update: function (event, ui) {
        var data = $(this).sortable('serialize');
        // POST to server using $.post or $.ajax
        $.ajax({
            data: data,
            type: 'POST',
            url: '/your/url/here'
        });
    }
});

它所做的是使用元素id创建一个元素数组。所以,我通常这样做:

<ul id="sortable">
   <li id="item-1"></li>
   <li id="item-2"></li>
   ...
</ul>

当您使用serialize选项时,它将创建一个POST查询字符串,如:item[]=1&item[]=2等。因此,如果您在id属性中使用—例如—数据库id,那么您可以简单地遍历post数组并相应地更新元素的位置。

例如,在PHP中:

$i = 0;
foreach ($_POST['item'] as $value) {
    // Execute statement:
    // UPDATE [Table] SET [Position] = $i WHERE [EntityId] = $value
    $i++;
}

jsFiddle示例

我想这可能也有帮助。A)它被设计成在每次排序后发送回服务器时将有效载荷保持在最小。(而不是每次发送所有元素或迭代服务器可能丢弃的许多元素)B)我需要在不损害元素的id/名称的情况下发送自定义id。这段代码将从asp.net服务器获得列表,然后在排序之后,只有2个值将被发送回来:排序元素的db id和它旁边被删除的元素的db id。根据这两个值,服务器可以很容易地识别新的位置。

<div id="planlist" style="width:1000px">
    <ul style="width:1000px">
       <li plid="listId1"><a href="#pl-1">List 1</a></li>
       <li plid="listId2"><a href="#pl-2">List 1</a></li>
       <li plid="listId3"><a href="#pl-3">List 1</a></li>
       <li plid="listId4"><a href="#pl-4">List 1</a></li>
    </ul>
    <div id="pl-1"></div>
    <div id="pl-2"></div>
    <div id="pl-3"></div>
    <div id="pl-4"></div>
</div>
<script type="text/javascript" language="javascript">
    $(function () {
        var tabs = $("#planlist").tabs();
        tabs.find(".ui-tabs-nav").sortable({
            axis: "x",
            stop: function () {
                tabs.tabs("refresh");
            },
            update: function (event, ui) {
                //db id of the item sorted
                alert(ui.item.attr('plid'));
                //db id of the item next to which the dragged item was dropped
                alert(ui.item.prev().attr('plid'));
                //make ajax call
            }
        });
    });
</script>

你很幸运,我在我的CMS中使用了相同的东西

当您想要存储订单时,只需调用JavaScript方法saveOrder()。它会向saveorder.php发出一个AJAX POST请求,当然你也可以将它作为一个常规表单发布。

<script type="text/javascript">
function saveOrder() {
    var articleorder="";
    $("#sortable li").each(function(i) {
        if (articleorder=='')
            articleorder = $(this).attr('data-article-id');
        else
            articleorder += "," + $(this).attr('data-article-id');
    });
            //articleorder now contains a comma separated list of the ID's of the articles in the correct order.
    $.post('/saveorder.php', { order: articleorder })
        .success(function(data) {
            alert('saved');
        })
        .error(function(data) { 
            alert('Error: ' + data); 
        }); 
}
</script>
<ul id="sortable">
<?php
//my way to get all the articles, but you should of course use your own method.
$articles = Page::Articles();
foreach($articles as $article) {
    ?>
    <li data-article-id='<?=$article->Id()?>'><?=$article->Title()?></li>
    <?
}               
?>   
</ul>
   <input type='button' value='Save order' onclick='saveOrder();'/>
在saveorder.php

;请记住,我删除了所有的验证和检查。

<?php
$orderlist = explode(',', $_POST['order']);
foreach ($orderlist as $k=>$order) {
  echo 'Id for position ' . $k . ' = ' . $order . '<br>';
}     
?>

这是我的例子。

https://github.com/luisnicg/jQuery-Sortable-and-PHP

您需要在更新事件中捕获订单

    $( "#sortable" ).sortable({
    placeholder: "ui-state-highlight",
    update: function( event, ui ) {
        var sorted = $( "#sortable" ).sortable( "serialize", { key: "sort" } );
        $.post( "form/order.php",{ 'choices[]': sorted});
    }
});

我可以通过遵循jsFiddle上接受的答案和相关示例来更改行。但是由于一些未知的原因,我在"停止或更改"操作后无法获得id。但是在JQuery UI页面上发布的例子对我来说很好。你可以点击这里的链接

试试这个解决方案:http://phppot.com/php/sorting-mysql-row-order-using-jquery/其中新订单被保存在某个html元素中。然后将带有这些数据的表单提交给某个PHP脚本,然后用for循环遍历它。

注意:我必须添加另一个类型为INT(11)的db字段,它在每次迭代中更新(时间戳)-它用于脚本知道哪一行最近更新,否则您最终会得到混乱的结果。