views:

21

answers:

2

[UPDATE]

Thanks guys,final code:

        var EUR_share_cost = 0;
        var USD_share_cost = 0;
        var GBP_share_cost = 0;
        var EUR_total_cost = 0;
        var USD_total_cost = 0;
        var GBP_total_cost = 0;

        $.ajax({
            url: '/producer/json/index/period/month/empties/'+empties+'/fields/'+fields+'/start/'+start+'/end/'+end+'',
            async: false,
            success: function(returned_values) {

                 $.each(returned_values.aaData, function(index, item) {

                    if (item[2] == 'EUR') {
                        EUR_share_cost  += parseFloat(item[5]);
                        EUR_total_cost  += parseFloat(item[3]);

                    } else if (item[2] == 'USD') {
                        USD_share_cost += parseFloat(item[5]);
                        USD_total_cost += parseFloat(item[3]);

                    } else if (item[2] == 'GBP') {
                        GBP_share_cost += parseFloat(item[5]);
                        GBP_total_cost += parseFloat(item[3]);
                    }
                });
            }
        });

        $('#EUR_share_cost').html(EUR_share_cost);
        $('#USD_share_cost').html(USD_share_cost);
        $('#GBP_share_cost').html(GBP_share_cost);

    }

});
+1  A: 

When you're in $.each(), the callback has 2 parameters, the first is the index (that incrementing number you're seeing), the second is the actual item, you'd actually want something like this:

 $.each(returned_values || {}, function(index, item) {
   console.log(item);
 });

I think overall you're looking for this:

var eur_total = 0;
$.each(returned_values && returned_values.aaData || {}, function(index, item) {
   if(item[2] == "EUR") eur_total += parseFloat(item[3]);
});
$('#EUR-total').val(eur_total);​

This would total up the third column...not sure which column you're after (maybe 5th?), you can give it a try here.

Nick Craver
+1  A: 

The first part of $.each is the index, the second part is the data see the documentation for details.

Your code should instead look like this:

$.ajax({
    url: '/area/json/index/period/month/',
    async: false,
    success: function(returned_values) {
        console.log(returned_values);
        $.each(returned_values || {}, function(index,item) {
            console.log(item);
        });
    }
})
Kristoffer S Hansen