Jqgrid show totals in the footer plugin

This plug-in automatically calculates sum of the all numeric rows in your grid and displays it in the new "Totals" row at the bottom of the grid. jqgrid 4.3.2+ is required.

(function($) {
    $.jgrid.extend({
        showTotals: function(options) {
            var o = $.extend({
                total: "Totals",
                totalRow: undefined,
                includeRows: []                
            }, options || {})
            return this.each(function() {
                if (!this.grid) {
                    return;
                }
                var self = this;
                var showTotals = function() {
                    var totals = {};
                    $.each(self.p.colModel, function(index, data) {
                        if (o.totalRow && o.totalRow == data.index) {
                            totals[data.index] = o.total;
                        } else {
                            var sum = NaN;
                            if (o.includeRows.length) {
                                if ($.inArray(data.index, o.includeRows) > -1) {
                                    sum = $(self).jqGrid('getCol', data.index, false, 'sum');
                                }
                            } else {
                                var maxPresision = 0;
                                $.each($(self).jqGrid('getCol', data.index, false), function(index, value) {
                                    if (isNaN(value)) {
                                        sum = NaN;
                                        return false;
                                    }
                                    if (value === "") {
                                        return;
                                    }
                                    if (!sum) {
                                        sum = 0;
                                    }
                                    sum += parseFloat(value);
                                    var pointPosition = value.indexOf('.'), valuePrecision = 0;
                                    if (pointPosition > -1) {
                                        valuePrecision = value.length - pointPosition - 1;
                                    }
                                    if (valuePrecision > maxPresision) {
                                        maxPresision = valuePrecision;
                                    }
                                });
                                if (maxPresision > 0 && sum) {
                                    sum = sum.toFixed(maxPresision);
                                }
                            }
                            if (!isNaN(sum)) {
                                totals[data.index] = sum;
                            }
                        }
                    });
                    $(self).jqGrid('footerData', 'set', totals);
                };
                $(this).bind('jqGridAfterLoadComplete', showTotals);
                //Already loaded?
                if (this.rows.length) {
                    showTotals();
                }
            });
        }
    });
})(jQuery);

Usage example:

    var grid = $("#grid").jqGrid({
        datatype: 'local',
        width: 800,
        height: 'auto',
        data: <?php echo json_encode($data) ?>,
        colNames: ['col1', 'col2', 'col3', 'col4'],
        colModel: [{
            name: 'col1',
            index: 'col1',
        }, {
            name: 'col2',
            index: 'col2',
        }, {
            name: 'col3',
            index: 'col3',
        }, {
            name: 'col4',
            index: 'col4',
        }],
        footerrow: true //!important
    });
    grid.jqGrid('showTotals', {totalRow: 'col1'});

Comments

  1. When altering the includeRows option of the plugin when calling it, how do you get it to exclude multiple rows? Thank you.

    ReplyDelete
  2. Replies
    1. Hmm. When trying that it only excludes the first row, and seems to just ignore that I put in any others. To show you my example:

      $('#list').jqGrid('showTotals', {totalRow: 'id', includeRows:['zip','duration']});

      It only excludes zip but leaves in the duration row or any other row listed after the first. Thank you for your help.

      Delete
    2. Thank you for your bug report. To fix this issue replace line
      if ($.inArray(data.index, includeRows)) {
      with
      if ($.inArray(data.index, o.includeRows) > -1) {

      Delete
    3. That fixed it. Thank you very much for your help.

      Delete

Post a Comment

Popular posts from this blog

Memory efficient array permutation in PHP 5.5 using generators

How to dump http request headers with PHP under CGI/FastCGI SAPI

Zend Framework 2 AJAX: return JSON response from controller action. The proper way.