1

I have a JqGrid whith a data footer where I'd like to put the sum of the column. I'd also like that that when the cell value equal zero in the grid, it will be color in grey. To do so I've creatted a custom formatter:

    currencyFmatter = function (cellValue, options, rowObject) {
    if (cellValue == 0)
        return '<span class="cellWithoutBackground" style="color:#E1E1E1;">' +
            $.fn.fmatter('number', cellValue, options, rowObject)
          + '</span>';
    return $.fn.fmatter('number', cellValue, options, rowObject);
};

Which I call like this

{ name: 'Name', index: 'Name', align: 'center', width: '200px' },
                    { name: 'January', index: 'Months["January"].Amount', align: 'center', 
                      formatter:currencyFmatter}

The coloration is good but the sum in the data footer don't work anymore it always says 0.

loadComplete: function () {

                var janSum = $('#jqgEndYear').jqGrid('getCol', 'January', false, 'sum');
                var febSum = $('#jqgEndYear').jqGrid('getCol', 'February', false, 'sum');
                var marSum = $('#jqgEndYear').jqGrid('getCol', 'March', false, 'sum');
                var aprSum = $('#jqgEndYear').jqGrid('getCol', 'April', false, 'sum');
                var maySum = $('#jqgEndYear').jqGrid('getCol', 'May', false, 'sum');
                var junSum = $('#jqgEndYear').jqGrid('getCol', 'June', false, 'sum');
                var julSum = $('#jqgEndYear').jqGrid('getCol', 'July', false, 'sum');
                var augSum = $('#jqgEndYear').jqGrid('getCol', 'August', false, 'sum');
                var sepSum = $('#jqgEndYear').jqGrid('getCol', 'September', false, 'sum');
                var octSum = $('#jqgEndYear').jqGrid('getCol', 'October', false, 'sum');
                var novSum = $('#jqgEndYear').jqGrid('getCol', 'November', false, 'sum');
                var decSum = $('#jqgEndYear').jqGrid('getCol', 'December', false, 'sum');

                $('#jqgEndYear').jqGrid('footerData', 'set', { Code: 'Total:',
                    January: janSum,
                    February: febSum,
                    March: marSum,
                    April: aprSum,
                    May: maySum,
                    June: junSum,
                    July: julSum,
                    August: augSum,
                    September: sepSum,
                    October: octSum,
                    November: novSum,
                    December: decSum
                });
            }
        });

If you have any idea of why the sum won't calculate, I be pleased to read it. Thanks in advance for your help

LotuX
  • 374
  • 1
  • 11
  • 30

1 Answers1

2

You have to define unformat (unformatter) additionally to formatter to make the method getCol working with columns having custom formatter. See the line of the source code of getCol method.

Oleg
  • 220,925
  • 34
  • 403
  • 798
  • @LotuX: `unformat` function should return the value which `getCol` can use in the `sum` operation. In your case I would recommend you to use `cellattr` instead of custom formatter. – Oleg Feb 03 '12 at 12:52
  • @LotuX: The code could be something like the following `cellattr: function (rowId, cellValue) { return cellValue == 0 ? ' class="cellWithoutBackground" style="color:#E1E1E1;"' : ''; }`. You can use predefined formatter `'number'`. – Oleg Feb 03 '12 at 13:03
  • Great Oleg, It's perfect I didn't know you can use cellatr. I works like a charm and the best is that I can use currency which is even better – LotuX Feb 03 '12 at 13:13
  • @LotuX: You are welcome! The usage of `cellatr` is the best if you want set only some custom attributes like `style`, `class`, `title`, `onclick` and so on depend on contain of the cell or other cells in the row. Because it doesn't changed the cell contain you can use any formatter which you need. The feature come as extension of [this my old suggestion](http://www.trirand.com/blog/?page_id=393/feature-request/custom-cell-tooltips-used-title-property-of-colmodel-as-the-function/#p21897). – Oleg Feb 03 '12 at 13:25
  • And if I want to have this style also in the footerrow is there an easy way? – LotuX Feb 03 '12 at 13:29
  • @LotuX: Do you mean to use the same formatter for the footer row like for the grid body? It defines the last parameter of `footerData`. If you mean `cellatr`, then it will be never used for the footer row. – Oleg Feb 03 '12 at 13:46
  • Yeah I was meaning the cellatr, the goal is when the sum equal zero then it will be gray like the other cell in the grid. – LotuX Feb 03 '12 at 14:03
  • @LotuX: You can insert HTML fragments in the data which you place in the footer with respect of `footerData`. It seem for me the only way. – Oleg Feb 03 '12 at 15:42