Search code examples
javascriptslickgrid

slickgrid total


In slickgrid, I put a total aggregator sum. It sums all the rows from that column (percents) and works fine. But now I am facing that I want only get the totals from the active percents. Column active is other column that contains 0 or 1.

In the column definition:

{id: "percent", name: "Percent", field: "percent", minWidth: 60, sortable: true, groupTotalsFormatter: sumTotalsFormatter, editor: Slick.Editors.Text}

function sumTotalsFormatter(totals, columnDef, id) 
{
  return "Total: " + Math.round(totals.sum[columnDef.field]) + "%";
}

The aggregators:

  dataView.setAggregators([
    new Slick.Data.Aggregators.Avg("percent"),
    new Slick.Data.Aggregators.Sum("percent")
  ], false);

I have been trying different ways but no success (Putting a non showing second field to store only the active data, searching on how to modify an agreggator.. etc). Any idea?


Solution

  • (Answered in a question edit. Converted to a community wiki answer. See What is the appropriate action when the answer to a question is added to the question itself? )

    The OP wrote:

    Well I solved crawling in Firebug and retesting several times. Here is my solution:

    function sumTotalsFormatter(totals, columnDef) 
    {
      var suma      = 0;
      //current group...
      var groupo    = totals.group.value;
      for (var i=0; i<data.length; i++)
      {
        if(data[i].active == '1')
        {
    //data[i].us is the one who makes the groups...
            if(groupo == 1 && data[i].us == '1')
            {
                suma += parseInt(data[i].percent);
            }
            else if(groupo == 0 && data[i].us == '0')
            {
                suma += parseInt(data[i].percent);
            }
        }   
      }
      return "Total: " + suma + "%"; 
    }