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.
Usage example:
(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'});
When altering the includeRows option of the plugin when calling it, how do you get it to exclude multiple rows? Thank you.
ReplyDelete['row1','row2','row3']
ReplyDeleteHmm. 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:
Delete$('#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.
Thank you for your bug report. To fix this issue replace line
Deleteif ($.inArray(data.index, includeRows)) {
with
if ($.inArray(data.index, o.includeRows) > -1) {
That fixed it. Thank you very much for your help.
Delete