jQuery UI Widgets Forums Grid Aggregation not working with Cellsrenderer in jqxGrid

This topic contains 6 replies, has 3 voices, and was last updated by  bach 6 years, 11 months ago.

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author

  • vinothr
    Participant

    Hi, I have a column with cellsrenderer for calculation of quantity*price. the calculated value is displayed correctly. code is,

    var totalSpotrenderer = function(index, datafield, value, defaultvalue, column, rowdata) {
    var dataRecord = rowdata;
    var amount =parseInt(dataRecord['price'])*parseInt(dataRecord['qty']);
     return '<div style="text-align: right; margin-top: 5px;">' + amount + '</div>';
    }

    and I need sum of this column in aggregates. I have used aggregatesrenderer like this.

    aggregates: [{
                    'sum': function(aggregatedValue, currentValue, column, record) {
                        var total = currentValue * parseInt(record["amount"]);                    
                        return aggregatedValue + total;
                    }}],
                aggregatesrenderer: function(aggregates) {
                    var renderstring = aggregates["sum"];
                    return '<span style="margin-top: 4px; float: right;">' + renderstring + '</span>';
                }

    I am getting 0 in sum. because I get 0 in ‘record[“amount”]’. can any one help me out?

    Thanks in advance.

    Vinothr


    Dimitar
    Participant

    Hello Vinothr,

    Computed columns do not have actual cell values to be aggregated. For more information, please refer to the forum topic Computed Column that will sort and use the cellclass.

    Best Regards,
    Dimitar

    jQWidgets team
    http://www.jqwidgets.com/


    vinothr
    Participant

    hi Dimitar,
    thanks for your reply. I f that is the case, can we set value for computed column’s datafield like this
    $("#roGrid").jqxGrid('setcellvalue', rowindex, "amount",amount);

    I tried this also. I could not. an error comes like ‘stack is full’.

    thanks & regards
    vinothr


    Dimitar
    Participant

    Hi vinothr,

    We suggest you do this in the grid’s ready callback function, in a for loop. If the issue persists, please post a sample code from which we may determine its source.

    Best Regards,
    Dimitar

    jQWidgets team
    http://www.jqwidgets.com/


    bach
    Participant

    This is a very old topic, but I’m going to bump it because it’s always the first result of a google search when I’m struggling with this topic. I’ve been banging my head against this problem for a while now. For the past year my app has been using setcellvalue commands in place of cellsrenderer calculations because I couldn’t access the computed value of each row for a final aggregate sum. I (and my end users) finally got sick of the sluggish performance of the page and this was one of the root causes of the slowdown.
    I finally found a good, simple solution, and in case it can help out anyone else here it is:

    adding a class to the cellsrenderer function allows you to access the computed values even though they’re just in the hdml and not actually saved in the grid. My cellsrenderer adds a class called “totalme” in it’s return, something like this:
    return "<div class='jqx-right-align totalme'>" + total + "</div>";

    in an entirely separate grid where I want to show the sum of all of these computed cells, I use:

            var rowtotals = $(".totalme").map(function() {
                return $(this).text();
            }).get();
    

    to create an array of all the computed values. After that, you can loop through the values and do whatever with them.


    Dimitar
    Participant

    Hello bach,

    Thank you for the contribution. Please note, however, that there are cases in which this solution may be unreliable: for example, if the first grid has a vertical scrollbar or paging enabled, the $(".totalme") selector will get only the visible cells and this may result in an incorrect calculation.

    Please consider the alternative solution using beforeLoadComplete which should be faster than the one with setcellvalue: https://www.jqwidgets.com/community/topic/computed-column-that-will-sort-and-use-the-cellclass/#post-88557.

    Best Regards,
    Dimitar

    jQWidgets team
    http://www.jqwidgets.com/


    bach
    Participant

    Ah interesting. While those cells not being visible is an unlikely scenario in my app, it is still a possibility that I must account for. Unfortunately the beforeLoadComplete does not work for me in this instance, but I think I can use it elsewhere.
    Thanks for the tip!

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.