【问题标题】:Sortable messes up my database ordering可排序搞乱了我的数据库排序
【发布时间】:2012-12-23 20:42:03
【问题描述】:

我使用 Jquery UI 作为前端,使用 PHP 和 MYSQL 作为后端。当我第一次打开排序页面时出现了我的问题,它弄乱了我的整个订单。例如:

id|text|orderid|
----------------  
1 |abc |2      |  
2 |bca |4      |  
3 |zxc |6      |  
4 |qwe |3      |  
5 |ads |1      |  
6 |iul |5      |  

变成:

id|text|orderid|
----------------
1 |abc |5      |  
2 |bca |1      |  
3 |zxc |3      |  
4 |qwe |6      |  
5 |ads |4      |  
6 |iul |2      |  

我的 index.php(HTML+JS):http://pastebin.com/5F66ncVF
我更新的b.php(PHP&mysql 查询):http://pastebin.com/3zhRvgvB

请帮忙?

【问题讨论】:

    标签: php javascript jquery mysql jquery-ui-sortable


    【解决方案1】:

    还没有时间真正检查您的数据,但这是我用来获取我的 ajax 调用数据的方法:

    var data = $('ol.sortable').nestedSortable('toHierarchy', {startDepthCount: 1});
    

    我明确设置了句柄和项目,以便可排序知道从哪里获取数据:

    $('ol.sortable').nestedSortable({
        handle: 'div',
        items: 'li',
        ...
    

    为了调试,我使用console.log(data);向它发送数据

    //for nested sortable
    function dump(arr,level) {
        var dumped_text = "";
        if(!level) level = 0;
    
        //The padding given at the beginning of the line.
        var level_padding = "";
        for(var j=0;j<level+1;j++) level_padding += "    ";
    
        if(typeof(arr) == 'object') { //Array/Hashes/Objects
            for(var item in arr) {
                var value = arr[item];
    
                if(typeof(value) == 'object') { //If it is an array,
                    dumped_text += level_padding + "'" + item + "' ...\n";
                    dumped_text += dump(value,level+1);
                } else {
                    dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
                }
            }
        } else { //Strings/Chars/Numbers etc.
            dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
        }
        return dumped_text;
    }
    

    也许先试一试,如果没有,我可以进一步研究您的代码......

    【讨论】:

    • 感谢您的回答,但是 nestedSortable 不是主要 JQuery UI 的功能,搜索它让我获得了多个结果,但这些结果都让我失望了。我的代码非常适合 ASC 排序,但是当通过 DESC 排序时,它会自己搞砸。
    • 尝试设置项目和处理,就像我的例子一样,只是使用 jquery 可排序:api.jqueryui.com/sortable/#option-itemsapi.jqueryui.com/sortable/#option-handle 的例子。此外,在加载可排序的所有内容后尝试执行此操作(因此在 $(document).ready(function(){ $( ".selector" ).sortable( "refreshPositions" ); });
    • 谢谢你,scott,但这仍然不能解决我的问题,我不认为这是一个 Javascript 问题,因为当我将 MYSQL 查询设置为 ASC 时,脚本可以完美运行,仅在当我尝试使用 DESC 的情况下,上述问题是否会引起丑陋的头。
    猜你喜欢
    • 1970-01-01
    • 2019-05-18
    • 1970-01-01
    • 2016-02-14
    • 1970-01-01
    • 1970-01-01
    • 2018-05-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多