Sort contribution by date on display
Benjamin Renard

Benjamin Renard commited on 2014-01-16 19:40:05
Showing 2 changed files, with 21 additions and 9 deletions.

... ...
@@ -157,18 +157,19 @@ show_contributions=function(group,contributor_name) {
157 157
   tbody.html('');
158 158
   total=$($('#view-group #total-value')[0]);
159 159
   contributions=group.contributionsByContributorName(contributor_name);
160
+  contributions.reverse();
160 161
   if (contributions.length==0) {
161 162
     tbody.append('<tr><td colspan=3>Aucune contributions</td></tr>');
162 163
     total.html('0,00 €');
163 164
   }
164 165
   else {
165 166
     sum=0;
166
-    for (uuid in contributions) {
167
-      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>';
168
-      tbody.append('<tr><td>'+contributions[uuid].getTitle()+'</td><td>'+contributions[uuid].cost+' €</td>'+col_actions+'</tr>');
169
-      sum+=contributions[uuid].cost;
167
+    for (idx in contributions) {
168
+      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>';
169
+      tbody.append('<tr><td>'+contributions[idx].getTitle()+'</td><td>'+contributions[idx].cost+' €</td>'+col_actions+'</tr>');
170
+      sum+=contributions[idx].cost;
170 171
     }
171
-    total.html(sum+' €');
172
+    total.html(sum.toFixed(2)+' €');
172 173
   }
173 174
   
174 175
   $('.contribution_delete_btn').bind('click',on_contribution_delete_btn_click);
... ...
@@ -180,12 +180,23 @@ function Group(uuid,name,data) {
180 180
    * Contributions
181 181
    */
182 182
   this.contributionsByContributorName=function(name) {
183
-    var ret={};
183
+    var ret=[];
184 184
     for (uuid in this.contributions) {
185 185
       if (this.contributions[uuid].contributor.name==name) {
186
-        ret[uuid]=this.contributions[uuid];
186
+        ret.push(this.contributions[uuid]);
187
+      }
188
+    }
189
+    ret.sort(function(a,b) {
190
+      if (a.date==b.date) {
191
+        return 0;
187 192
       }
193
+      else if(a.date<b.date) {
194
+        return -1;
188 195
       }
196
+      else {
197
+        return 1;
198
+      }
199
+    });
189 200
     return ret;
190 201
   }
191 202
 
... ...
@@ -216,8 +227,8 @@ function Group(uuid,name,data) {
216 227
       var sum=0;
217 228
       c=this.contributors[idx].name;
218 229
       cl=this.contributionsByContributorName(c);
219
-      for (uuid in cl) {
220
-        sum+=cl[uuid].cost;
230
+      for (idx in cl) {
231
+        sum+=cl[idx].cost;
221 232
       }
222 233
       if (min==-1 || min>sum) {
223 234
           min=sum;
224 235