Sort contribution by date on display

This commit is contained in:
Benjamin Renard 2014-01-16 20:40:05 +01:00
parent 1380086723
commit 903c4f071b
2 changed files with 21 additions and 9 deletions

View file

@ -157,18 +157,19 @@ show_contributions=function(group,contributor_name) {
tbody.html(''); tbody.html('');
total=$($('#view-group #total-value')[0]); total=$($('#view-group #total-value')[0]);
contributions=group.contributionsByContributorName(contributor_name); contributions=group.contributionsByContributorName(contributor_name);
contributions.reverse();
if (contributions.length==0) { if (contributions.length==0) {
tbody.append('<tr><td colspan=3>Aucune contributions</td></tr>'); tbody.append('<tr><td colspan=3>Aucune contributions</td></tr>');
total.html('0,00 €'); total.html('0,00 €');
} }
else { else {
sum=0; sum=0;
for (uuid in contributions) { for (idx in contributions) {
col_actions='<td><div class="btn-group" data-uuid="'+contributions[uuid].uuid+'"><button type="button" class="btn btn-default contribution_edit_btn"><span class="glyphicon glyphicon-edit"></span></button><button type="button" class="btn btn-default contribution_delete_btn"><span class="glyphicon glyphicon-trash"></span></button></div></td>'; col_actions='<td><div class="btn-group" data-uuid="'+contributions[idx].uuid+'"><button type="button" class="btn btn-default contribution_edit_btn"><span class="glyphicon glyphicon-edit"></span></button><button type="button" class="btn btn-default contribution_delete_btn"><span class="glyphicon glyphicon-trash"></span></button></div></td>';
tbody.append('<tr><td>'+contributions[uuid].getTitle()+'</td><td>'+contributions[uuid].cost+' €</td>'+col_actions+'</tr>'); tbody.append('<tr><td>'+contributions[idx].getTitle()+'</td><td>'+contributions[idx].cost+' €</td>'+col_actions+'</tr>');
sum+=contributions[uuid].cost; sum+=contributions[idx].cost;
} }
total.html(sum+' €'); total.html(sum.toFixed(2)+' €');
} }
$('.contribution_delete_btn').bind('click',on_contribution_delete_btn_click); $('.contribution_delete_btn').bind('click',on_contribution_delete_btn_click);

View file

@ -180,12 +180,23 @@ function Group(uuid,name,data) {
* Contributions * Contributions
*/ */
this.contributionsByContributorName=function(name) { this.contributionsByContributorName=function(name) {
var ret={}; var ret=[];
for (uuid in this.contributions) { for (uuid in this.contributions) {
if (this.contributions[uuid].contributor.name==name) { if (this.contributions[uuid].contributor.name==name) {
ret[uuid]=this.contributions[uuid]; ret.push(this.contributions[uuid]);
} }
} }
ret.sort(function(a,b) {
if (a.date==b.date) {
return 0;
}
else if(a.date<b.date) {
return -1;
}
else {
return 1;
}
});
return ret; return ret;
} }
@ -216,8 +227,8 @@ function Group(uuid,name,data) {
var sum=0; var sum=0;
c=this.contributors[idx].name; c=this.contributors[idx].name;
cl=this.contributionsByContributorName(c); cl=this.contributionsByContributorName(c);
for (uuid in cl) { for (idx in cl) {
sum+=cl[uuid].cost; sum+=cl[idx].cost;
} }
if (min==-1 || min>sum) { if (min==-1 || min>sum) {
min=sum; min=sum;