Question

I use multi-row grouping and put the totals in the grouping headers. I'm not using the totals in the totals rows. I see the the rows are grouped and where the totals would be there are empty rows. In my case there is an empty row after each of the child grouping and at the end there is an empty row for the parent totals.
How do I remove these totals rows? Thank you!

.cshtml:

  <div id="gridList" class="grid" style="width: 100%; height: 500px"></div>

.js

$(function() {
    var columns = [
    {
        id: "isExcluded",
        name: "Exclude",
        field: "isExcluded" /*, width: 120*/,
        formatter: Slick.Formatters.Checkmark,
        editor: Slick.Editors.Checkbox, sortable: true
    },
    {
        id: "symbol",
        name: "Symbol",
        field: "symbol",
        sortable: true /*, width: 120*/
    },
    {
        id: "price",
        name: "Price",
        field: "price",
        sortable: true
        //, groupTotalsFormatter: sumTotalsFormatter 
    },
    {
        id: "parent_name",
        name: "Parent Name",
        field: "parent_name",
        sortable: true /*, width: 120*/
    },

    {
        id: "description",
        name: "Description",
        field: "description",
        sortable: true,
        width: 120,
        editor: Slick.Editors.Text,
    },
    { id: "cancel_ind", 
      name: "Canceled", 
      field: "cancel_ind", 
      sortable: true, width: 80 }
];

function requiredFieldValidator(value) {
    if (value == null || value == undefined || !value.length) {
        return { valid: false, msg: "This is a required field" };
    } else {
        return { valid: true, msg: null };
    }
};

var options = {
    editable: true,
    enableAddRow: true,
    enableCellNavigation: true,
    asyncEditorLoading: false,
    autoEdit: true,
    enableExpandCollapse: true,
    rowHeight: 25
};

var sortcol = "parent_name";
var sortdir = 1;
var grid;
var data = [];
var groupItemMetadataProviderTrades = new Slick.Data.GroupItemMetadataProvider();
var dataView = new Slick.Data.DataView({ groupItemMetadataProvider:      groupItemMetadataProviderTrades });

dataView.onRowCountChanged.subscribe(function (e, args) {
    grid.updateRowCount();
    grid.render();
});

dataView.onRowsChanged.subscribe(function (e, args) {
    grid.invalidateRows(args.rows);
    grid.render();
});

function groupByParentAndSymbol() {
    dataViewTrades.setGrouping([
        {
            getter: "parent_name",
            formatter: function(g) {
                return "Parent:  " + g.value + "  <span style='color:green'>(" + g.count + " items)  Total: " + g.totals.sum.price + "</span>";
            },
            aggregators: [
                new Slick.Data.Aggregators.Sum("price")
            ],
            aggregateCollapsed: true
            ,lazyTotalsCalculation: true
        },
    {
        getter: "symbol",
        formatter: function(g) {
            return "Symbol:  " + g.value + "  <span style='color:green'>(" + g.count + " items)   Total: " + g.totals.sum.price + "</span>";
        },
        aggregators: [
            new Slick.Data.Aggregators.Sum("price")
        ],
        collapsed: true
        ,lazyTotalsCalculation: true
    }]);
};

grid = new Slick.Grid("#gridList", dataView, columns, options);
grid.registerPlugin(groupItemMetadataProviderTrades);
grid.setSelectionModel(new Slick.RowSelectionModel());

..... /*sorting support etc*/

 // use instead of the default formatter <---  removed not used.
 function sumTotalsFormatter(totals, columnDef) {
    var val = totals.sum && totals.sum[columnDef.field];
    //if (val != null) {
    //    return "total: " + ((Math.round(parseFloat(val) * 100) / 100));
    //}
    return "";
 }

// will be called on a button click (I didn't include the code as irrelevant)
var getDataList = function () {
    $.getJSON('/Home/GetData/', function (json) {
        data = json;
        dataView.beginUpdate();
        dataView.setItems(data);
        groupByParentAndSymbol();
        dataView.endUpdate();
        dataView.syncGridSelection(grid, true);
    });
 };

  getDataList();

});

Was it helpful?

Solution

Adding displayTOtalsRow: false to the dataview solved my problem - the total rows are not shown now.

 var dataView = new Slick.Data.DataView({ groupItemMetadataProvider: groupItemMetadataProviderTrades, displayTotalsRow: false });

OTHER TIPS

To answer simply to your question... Just remove the aggregators: [...], when I say remove you have 2 options, you can remove whatever is the array [...] or you could simply erase completely that object line (so removing completely the aggregators[...]).

Now if you want more explanation of how it works...Let's give you some definition so you'll understand better. An aggregate is a collection of items that are gathered together to form a total quantity. The total in question could be a sum of all the fields, an average or other type of aggregator you might define. So basically it's the action of regrouping by the column you chose and give the calculation result of the group. Alright now how does it work in the case of a SlickGrid? You need to define what kind of Aggregator you want to use (Avg, Sum, etc...) that goes inside your function groupByParentAndSymbol(), defining them will do the calculation BUT unless you attach it to the field, nothing will be displayed, so in the example you found it is very important to attach/bind this groupTotalsFormatter: sumTotalsFormatter to your columns definition, as for example:

...var columns = [{id: "cost", ...width: 90, groupTotalsFormatter: sumTotalsFormatter}, ...];

so let's recap... Once the Aggregator is define new Slick.Data.Aggregators.Sum("cost") it will do the calculation but unless it's bind to the field nothing will be displayed. As an extra option, the aggregateCollapsed (true/false) is to display the sub-total (avg, sum or whatever) inside or outside the group when you collapse.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top