Search code examples
jqueryjqgridfree-jqgrid

jqGrid 4.8.0 - How to get cell type or cell value using jsonmap


I'm trying to get a cell value from onSelectRow event.

I can achieve that using:

celValue = $('#jqGrid').jqGrid ('getCell', rowid, 'id');

Two question:

  1. Is there a way to get it by using 'jsonmap', and not 'name'?
  2. Is there a way to get the type of the cell? (date, number, integer etc')

Thanks,

Tal.


Solution

  • You can first find the index of the column from which you want to get the data. You can use the code like

    var $grid = $("#jqGrid"), colModel = $grid.jqGrid("getGridParam", "colModel"), cm, i,
        iCol = -1, l = colModel.length, propName = "jsonmap", propVal = "start_time", val;
    
    for (i = 0; i < l; i++) {
        cm = colModel[i];
        if (cm[propName] === propVal) {
            iCol = i;
            break;
        }
    }
    if (iCol >= 0) {
        val = $grid.jqGrid("getCell", rowid, iCol);
    }
    

    The above code search the index iCol of the column which has propVal ("start_time") in the property propName ("jsonmap"). The first column with the property will be used as option of getCell.

    In the same way you can search for the column which has specific formatter. I would recommend you to use column templates in colModel. If you defines cmTemplate

    $.extend(true, $.jgrid, {
        cmTemplate: {
            dateType: {
                sorttype: "date", edittype: "date", formatter: "date",
                formatoptions: { srcformat: "U/1000", newformat: "m/d/Y H:i:s" },
                searchoptions: { sopt: [ "eq", "ne", "lt", "le", "gt", "ge" ] },
                editoptions: { sopt: [ "eq", "ne", "lt", "le", "gt", "ge" ] },
                searchrules: { date: true }
            }
        }
    });
    

    after that you can use template: "dateType" property in colModel. In the way you can define many different types which all use the same formatter. Moreover you will have really readable colModel which can be very easy maintained. You can include properties like width which you can overwrite in colModel. In the way you can specify more better default values for some properties, but you can hold still the same flexibility like before. If I examine the code of the demo which you posted I think that column templates would be very good for you.