jQuery UI保存可排序列表 [英] JQuery UI Saving Sortable List

查看:67
本文介绍了jQuery UI保存可排序列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道之前曾有人问过这个问题,但是解决方案对我不起作用.我正在尝试将项目的新顺序保存到数据库中.

I know this question has been asked before but the solutions did not work for me. I am trying to save the new ordering of items to the database.

我已经对其进行了相当大的简化,但这是它的基本思想.我有一个嵌入了可排序列表的表单.

I have simplified it very considerably but this is the basic idea of it. I have a form with a sortable list embedded in it.

<form id="itemlist">
    <ul id="itemsort">
       <li id="Item_1">Item<input type="hidden" name="itemid[]" value="itemsRowID01"/></li>
       <li id="Item_2">Item<input type="hidden" name="itemid[]" value="itemsRowID02"/></li>
       <li id="Item_3">Item<input type="hidden" name="itemid[]" value="itemsRowID03"/></li>
       <li id="Item_4">Item<input type="hidden" name="itemid[]" value="itemsRowID04"/></li>
    </ul>
</form>

我已加载JQuery和JQuery UI,以下代码启用了可排序列表功能,并将项目ID和新排序顺序发布到php脚本中. "editor"变量是在加载时设置的公共变量,它工作正常.排序工作正常,但是重新排序列表时,发布的neworder值似乎没有改变.

I have JQuery and JQuery UI Loaded and the The Following code enables the sortable list function and posts the item ids and New sort order to a php script. the "editor" variable is a public variable that is set on load it works fine. The sorting works fine but the neworder value that posts doesn't seem to change when I re-order the list.

//sorting feature  
    $("#itemsort").live('hover', function() {
        $("#itemsort").sortable({ 
            opacity:.5,
            update : function () {          

                var neworder =  $('#itemsort').sortable('serialize');
                var inputs = serializePost('#itemlist');

                $.post("core/actions.php",{
                   'order': editor,
                   'inputs': inputs,
                   'neworder': neworder},function(){

                       alert("Order saved.", 1);

                });
            } 
        });
    });

关于actions.php ...

On actions.php...

    if(isset($_POST['order'])){

            //set a variable for each post
            $batchid = $_POST['inputs']['itemid'];

            parse_str($_POST['neworder'], $neworder);

            //count the number of entries to be ordered
            $count = count($batchid);        

            //use the count to create an incremental loop for each item to be updated.
            $i=0;
            while ($i <= $count) {

   $query ="UPDATE {$_POST['order']} SET order=$neworder[item][$i] WHERE id=$batchid[$i]";
                ++$i;
            }
        }

我不确定为什么我为每个项目获得的订单都不会改变.

I'm not sure why the order I get for each item will not change.

有什么想法吗?

-L

推荐答案

$("#list").live('hover', function() {
        $("#list").sortable({

            update : function () {

                var neworder = new Array();

                $('#list li').each(function() {    

                    //get the id
                    var id  = $(this).attr("id");
                    //create an object
                    var obj = {};
                    //insert the id into the object
                    obj[] = id;
                    //push the object into the array
                    neworder.push(obj);

                });

                $.post("pagewhereyouuselist.php",{'neworder': neworder},function(data){});

            }
        });
    });

然后在您的PHP文件中,或在本示例中为"pagewhereyouuselist.php"

Then in your PHP file, or in this example "pagewhereyouuselist.php"

$neworderarray = $_POST['neworder'];
//loop through the list of ids and update your db
foreach($neworderarray as $order=>$id){    
    //you prob jave a connection already i just added this as an example
    $con = mysql_connect("host","username","password");

    if (!$con){
         die('Could not connect: ' . mysql_error());
    }

    mysql_select_db("my_db", $con);

    mysql_query("UPDATE table SET order = {$order} WHERE id = {$id}");
    mysql_close($con);

}

应该这样做 我没有测试它,因为它是一个示例连接.我实际使用的实际脚本更特定于我的程序,这是显示概念的简化版本

that should do it i didn't test it as it is an example connection. the actual script I am actually using is more specific to my program this is a simplified version to show the concept

这篇关于jQuery UI保存可排序列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆