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.
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
精彩评论