views:

621

answers:

3

I have small problem.

What I want to achieve is adding sum of values from above elements to each one. For example every position have got it own time value hidden in attribute. I can get it by using jQuery

var time = $(ui.draggable).attr("time");

now we got 4 positions with time as follows:

  1. 432 sec
  2. 123 sec
  3. 5634 sec
  4. 654 sec

Now I want to sum it like this:

1. 432
2. 432+123
3. 432+123+5634
4. 432+123+5634+654

Any ideas how can I do this?

This is my code:

$(document).ready(function(){

    $(".trash").sortable({
     tolerance: 'touch',
     receive: function (event, ui) {
     ui.item.remove();
           }
    });
    $(".DragContainer_dest").sortable({helper:'clone',
     opacity: 0.5,
     connectWith: '.trash',
     scroll: true,
        update : function () { 
        var order = $('.DragContainer_dest').sortable('serialize',{key:'string'}); 
        $.post('includes/ajaxify.php',order+'&action=update');    
        var time = $(this).attr("time");
        },
     out : function () { 
         ui.item.remove();
        }
    });

    $("div[class=DragContainer] .DragBox").draggable({helper:'clone'}); 

    $(".DragContainer_dest").droppable({
        accept: ".DragBox",
        tolerance: "touch",
        drop: function(ev, ui) {
           $(this).append($(ui.draggable).clone().addClass("added"));  
        }
    });
  });

I want every element dropped or sorted in DragContainer_dest to sum values from other elements above him. Can this be done?

A: 

As I can understand, you need to collect all siblings of a node that come before the element in question and collect their attributes. Let's try this:

var nodeSiblings = $(this).parent().children();
// We need to find the index of our element in the array
var stopIndex = nodeSiblings.index(this);
var time = 0;

nodeSiblings.each( function(index, element){
    if (index > stopIndex) return;
    var attribute = parseInt($(element).attr("time"), 10);
    if (!isNaN(attribute)) time += attribute;
});

// Here you have the sum of all time attributes
alert(time);
Igor Zinov'yev
I've added Your code to the drop function but unfortunetly alert is returning "NaN". What does tis mean?
PsychoX
And stopIndex is always "1"
PsychoX
As far as I can see, you need to add this code to the update function. This snippet was written having in mind that `this` is a dropped element.If it alerts NaN, it means that some of the elements doesn't have a "time" attribute, or it is empty.
Igor Zinov'yev
I have edited the code so it won't alert `NaN` anymore. But you will have to see that `nodeSiblings` contain your draggable elements that you want to collect attributes from.
Igor Zinov'yev
I believe that I'm to stupid to do this... Can You please look at my code? http://lukasz.webh.pl/test.html
PsychoX
Check this out:http://pastebin.com/m70e32532I have edited your script so it counts the "time" attributes, but you will have to figure out what to do when you swap items that are already inside the "waste bin".
Igor Zinov'yev
Thank You very much :). Is it posible for time to update on every "sortable" update? Or should I remove "sortable"?. Because now on every sorting update it's adding time over and over again.
PsychoX
Maybe if you can determine the starting position of the dragged element, you can later decide whether to count the time or not. Like if it was in the wastebin - then do not do a count. It all depends on what you plan to achieve. And to know where an element was before dragging, I would put a callback on drag start and check out the parent of a dragged element.
Igor Zinov'yev
A: 

Maybe add a global variable called timeSum and then increase it for each element dropped?

var timeSum = 0;

$(".DragContainer_dest").droppable({
    drop: function(ev, ui) {
       timeSum += parseInt($(ul.draggable).attr('time'));
    }
});
David
A: 

If you're using parseInt as instructed elsewhere, don't forget to include the radix:

parseInt($(ul.draggable).attr('time'), 10);

If you have a zero at the start it will treat it as octal unless you specify the radix.

James Wiseman
This belongs in a comment.
Matt Ball