开发者

Alert the dropping order - jquery

i have tree list( one parent and 4 children), if i change the children p开发者_Python百科osition alert box will display the order.

for example

i have children 1, 2, 3, 4 and if i drag the 4 child and place it in the 2 position

alert will display the order like 1, 4, 2, 3.

Alert the dropping order - jquery

and jsfiddle link: http://jsfiddle.net/thilakar/AXDQL/.


** mind reading mode on (please write question that are understandable withouth links **

You can do (you must use the stop function otherwise the alert is fired twice):

 $( ".droptrue" ).sortable({
    connectWith: "ul.mt",
    dropOnEmpty: true,
    stop: function(event, ui) {
        var order = ui.item.prevAll().length;
        alert(order);
        //$.post("updateDB.php", order, function(theResponse){
        //$("#contentRight").html(theResponse);
     //});                                                              
    }                                      
 });  

fiddle here : http://jsfiddle.net/nicolapeluchetti/AXDQL/2/

EDIT - to do what you want you could do this:

var addPositions = function() {
    $('.droptrue').each(function() {
        var position = 1;
        $(this).children().each(function() {

            $(this).data('position', position);
            position++;
        });
    });
};
$(document).ready(function() {
    var treeList = "";
    treeList = "<ul id=\"createTree\" class=\"droptrue1\">";
    for (var key in jsonObj) {
        //alert("key: " + key + ", value: " + jsonObj[key])
        for (var skey in jsonObj[key]) {
            treeList += ("<li class=\"listTree\" id=\"asdf\">" + skey + "<ul class=\"droptrue mt\">");
            for (var sskey in jsonObj[key][skey]) {
                for (var ssskey in jsonObj[key][skey][sskey]) {
                    treeList += ("<li class=\"innerList\">" + jsonObj[key][skey][sskey][ssskey] + "</li>");
                }
            }
            treeList += "</ul></li>";
        }
    }
    treeList += "</ul>";
    $('#tree').append(treeList);
    addPositions();



    $(".droptrue").sortable({
        connectWith: "ul.mt",
        dropOnEmpty: true,
        stop: function(event, ui) {
            var order = [];
            ui.item.closest('ul').children('li').each(function() {
                order.push($(this).data('position'));
                });
            alert(order.join(', '));
                //$.post("updateDB.php", order, function(theResponse){
            //$("#contentRight").html(theResponse);
            //});                                                              
            }
        });
    $("ul.droptrue1").sortable();
    });

Fiddle here: http://jsfiddle.net/nicolapeluchetti/AXDQL/6

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜