Blank when NaN in jqGrid cells
Asked Answered
S

3

6

How to set blank instead of NaN in jqGrid cells ? Using formatter ? Is there an example?

Stranger answered 21/11, 2009 at 15:29 Comment(4)
catb.org/~esr/faqs/smart-questions.html#bepreciseThistle
+1 to the above. raouf - Can you provide more information about your problem? Some code samples would probably help as well.Despotism
Thank you Justin! Displaying database numeric data on jqgrid with formatter:'currency' in colmodel works fine for not DBNull values. But when value is DBNull the word 'NaN' appears in jqgrid cell. the question is : How to do avoid 'NaN' and put a blank (space) instead.Stranger
take a look at [this post][1], it should answer your question [1]: #17024527Redeemable
D
6

This is REALLY old but the jqGrid documentation didn't have an easy answer and this question pulls up first in Google results when I was looking for the same answer.

I was able to display a blank cell instead of a 0 when using the predefined formatter option for an integer using this code:

{ name: 'Quantity', formatter: 'integer', formatoptions: { defaultValue: ''} }

The defaultValue is just set to blank.

Demigod answered 18/5, 2012 at 20:21 Comment(2)
Thank God that there was someone to ansawer "really" old question :). You saved my day even3 years later.Launderette
Add one more to the grateful group. :)Sendoff
R
1

I would not rewrite the custom formatter -- but override it (or make a new one)! That way, when a new version of jQgrid comes out, you don't overwrite your custom wrapper.

For example, my users don't want to see the value if it is 0, so I do this:

$.fn.fmatter.currency = function (cellval, opts) {
    if (cellval == 0) {
        return '';
    }
    var op = $.extend({},opts.currency);
    if(!isUndefined(opts.colModel.formatoptions)) {
        op = $.extend({},op,opts.colModel.formatoptions);
    }
    if(isEmpty(cellval)) {
        return op.defaultValue;
    }
    return $.fmatter.util.NumberFormat(cellval,op);
};

But I could also call it:

$.fn.fmatter.currencyNoZero

In your case, I would do this:

$.fn.fmatter.currency = function (cellval, opts) {
    if (cellval == null) {
        return '';
    }
    var op = $.extend({},opts.currency);
    if(!isUndefined(opts.colModel.formatoptions)) {
        op = $.extend({},op,opts.colModel.formatoptions);
    }
    if(isEmpty(cellval)) {
        return op.defaultValue;
    }
    return $.fmatter.util.NumberFormat(cellval,op);
};
Rahel answered 8/12, 2009 at 14:5 Comment(1)
I'd suggest to combine both, i.e. if (cellval == 0 || cellval == null) { return ''; }Coenzyme
D
0

On the server, before you return your data as XML/JSON/whatever, try setting DBNull values equal to a blank string.

Alternatively, if it is acceptable to display NULL values as 0, you could modify your SQL along these lines:

SELECT IsNull(my_amount, 0)
Despotism answered 22/11, 2009 at 16:15 Comment(1)
I think that the solution is in the formatter option of colmodel. There are two ways: Predefined and Custom formatter. In my case, i want to use the predefined 'currency' formatter, but with the possibility to return blank instead of 'NaN' when value is null. It seems that there is no way to avoid rewriting my custum currency formatter function.Thank you again Justin.Stranger

© 2022 - 2024 — McMap. All rights reserved.