Calculated Columns / Running Totals

1,176 views
Skip to first unread message

Paul King

unread,
Jan 13, 2012, 9:23:33 PM1/13/12
to slic...@googlegroups.com

Hiya,

I'm building an accountancy app that uses SlickGrid for the transaction lists, and it's been absolutely awesome so far! :)

The first thing I've struggled with is the best approach to maintaining a 'running total' column.

This column should contain the sum of the values from another column for all rows, up to and including it, e.g.:

10    10
24    34
29    63

I'm loading the initial dataset via an ajax call, and can populate this column by iterating over the data before passing it to the dataView, but obviously this won't hold up when the grid is re-sorted.

Any advice, please?

Thanks,
- Paul

Michael Leibman

unread,
Jan 13, 2012, 9:50:16 PM1/13/12
to slic...@googlegroups.com
It sounds like you decided to do the calculation in the JavaScript on the client, which sounds good.
Processing it once before display should work fine unless you have a huge dataset with lots of these calculated fields.  If that's the case, you have lots of options still, but that's a separate question.  I don't see how sorting is an issue though.  Can't you just process the dataset again (and update the fields directly in your data array without reinitializing the DataView) when the user sorts by a different column?

Paul King

unread,
Jan 13, 2012, 10:14:30 PM1/13/12
to slic...@googlegroups.com

Hi Michael,

Thanks for your quick response :)
 
Processing it once before display should work fine unless you have a huge dataset with lots of these calculated fields.  If that's the case, you have lots of options still, but that's a separate question.  I don't see how sorting is an issue though.  Can't you just process the dataset again (and update the fields directly in your data array without reinitializing the DataView) when the user sorts by a different column?

Where's the best place to do this?  I'm currently playing with hooking grid.onSort, something like:

items = dataView.getitems();
// for each
  // update total
  // set item balance
// end
dataView.setItems(items);

This didn't seem to work that well, but I'll keep playing.  Is there a more appropriate way?

Thanks again for SlickGrid,
- Paul

Michael Leibman

unread,
Jan 13, 2012, 10:23:46 PM1/13/12
to slic...@googlegroups.com
In what way did it "not work well"?

Paul King

unread,
Jan 13, 2012, 10:33:24 PM1/13/12
to slic...@googlegroups.com
In what way did it "not work well"?

The grid values don't update.  Having played with it since they update if I click the header a bunch of times.  Suspect there's a call I'm missing to tell the grid to update itself after I've called setItems()?

Code if it helps:

  grid.onSort.subscribe(function(e, args) {      
    sortColumn = args.sortCol.field;      
    dataView.sort(dataSortFunction, args.sortAsc);      
     
----
    var balance = 0;      
    var items = dataView.getItems();      
    for(var i = 0 ; i < items.length ; i++ ) {      
      balance = balance + parseInt(items[i].t_amount, 10);      
      items[i].balance = balance;      
    }      
                                                                                          
    dataView.setItems(items);                                                             
----
  });                                    

- Paul

Michael Leibman

unread,
Jan 13, 2012, 10:42:32 PM1/13/12
to slic...@googlegroups.com
You need to call grid.invalidate();

Paul King

unread,
Jan 13, 2012, 10:45:42 PM1/13/12
to slic...@googlegroups.com

Perfect, thanks!


Altaf Hussain

unread,
Sep 3, 2013, 7:15:45 AM9/3/13
to slic...@googlegroups.com
Perfect!
Reply all
Reply to author
Forward
0 new messages