JQGRID show blank instead of Null
Asked Answered
J

2

2

Am using JQGrid and am getting Null displayed in the grid as it comes from DB. I can change the query to return Blank value.

But am try to handle using JQGrid. How do i replace null by blank values in Grid.

I don want to show NULL to users instead show blank.

How do i achieve this in JQGrid ?

Thanks

Jinajingle answered 10/6, 2013 at 12:34 Comment(0)
C
7

It's probably best that you handle this server-side, but if you want to do it in jqGrid, you could use a custom formatter that converts null to the empty string. (I'm not sure if you are actually getting back the value null or the String "NULL", so I handled both cases):

var nullFormatter = function(cellvalue, options, rowObject) {
    if(cellvalue === undefined || isNull(cellvalue) || cellvalue === 'NULL') {
        cellvalue = '';
    }

    return cellvalue;
}

$("#myGridContainer").jqGrid({
    ....
    colModel: [{
        label: 'Name',
        name:'name',
        index:'name',
        formatter:nullFormatter
    }, {
        label: 'Next Column',
        name:'nextCol',
        index:'nextCol',
        formatter: nullFormatter
    }, ...],
    ....
}
Charron answered 10/6, 2013 at 12:43 Comment(2)
null and undefined should be converted automatically fiddleClydeclydebank
I am sure that the server code return "null" or "NULL" instead of null which would corresponds to JSON format. So the server code have an error. Fixing of server code with [ISNULL][1] function of SQL or another fixing of code would be the best solution. If one use custom formatter then aotoencode: true option will not work for the column. I would recommend include explicit call of $.jgrid.htmlEncode in nullFormatter formatter and define unformat function too.Ahrendt
S
3

I had the same problem.

Also, I wanted jqGrid to display my figures with thousand separators and 2 decimal places, but using the default 'number' formatter caused any nulls (from the database) to get displayed as "0.00" rather than being left blank.

$("#tblListOfRecords").jqGrid({
    ...
    colModel: [
      { name: "SomeNumber", formatter: 'number', sorttype: "integer", formatoptions: { decimalPlaces: 2 } }
    ]
    ...

This wasn't quite what I wanted.

My solution was to write my own formatter:

$("#tblListOfRecords").jqGrid({
    ...
    colModel: [
      { name: "SomeNumber", formatter: formatNumber}
    ]
});

function formatNumber(cellValue, options, rowdata, action) {
    //  Convert a jqGrid number string (eg "1234567.89012") into a thousands-formatted string "1,234,567.89" with 2 decimal places
    if (cellValue == "")
        return "";
    if (cellValue == null || cellValue == 'null')
        return "";

    var number = parseFloat(cellValue).toFixed(2);          //  Give us our number to 2 decimal places
    return number.toLocaleString();                         //  "toLocaleString" adds commas for thousand-separators.
}
Souvenir answered 26/2, 2015 at 11:29 Comment(1)
cellValue will cause a problem, should be cellvalue...otherwise this works for me!Shafer

© 2022 - 2024 — McMap. All rights reserved.