How can I get cell value from jqGrid column to do conditional formatting for backcolor

I am using jqGrid treegrid and want to format the back color of the columns based on the data value in the cell (integer):

Here is an example where I set the column:

             {
                 name: 'missingBooks',
                 cellattr: function (rowId, tv, rawObject, cm, rdata) {

                 //conditional formatting
                     if (rawObject[11] > 0) {
                         return 'style="background-color:#FFCCCC"';
                     }
                 },
                 width: 75,
                 unformat: originalValueUnFormatter,
                 formatter: missingBooksFormatter,
                 align: "right",
                 index: 'missingBooks',
                 hidden: false,
                 sorttype: 'int',
                 sortable: true
             },

this works fine, but my problem is with the cellAttr callback. In this conditional format string:

      if (rawObject[11] > 0) {
                         return 'style="background-color:#FFCCCC"';
                     }

I would like to reuse this logic, so I don't want to be indexed in rawObject and figure out which column I'm using. I was hoping there was a way to do something like this:

       if (rawObject.missingBooks > 0) {
                         return 'style="background-color:#FFCCCC"';
                     }

but it looks like undefined. That way, if I add a new column, I don't have to reindex all this conditional formatting code.

+3
source share
1

. jqGrid. , rd, addCell rd . addCell formatCol, formatCol cellattr rd, , , .

, - jqGrid. , rawObject .

, beforeRequest beforeProcessing , .

var colMap = {};
$("#tree").jqGrid({
    ...
    colModel: [
        {name: 'missingBooks',
            cellattr: function (rowId, tv, rawObject, cm, rdata) {
                //conditional formatting
                 if (Number(rawObject[colMap.missingBooks]) > 0) {
                     return ' style="background-color:#FFCCCC"';
                 } else {
                     return '';
                 }
            }
            ...
    ],
    beforeRequest: function () {
        if ($.isEmptyObject(colMap)) {
            var i, cmi,
                cm = $(this).jqGrid('getGridParam', 'colModel'),
                l = cm.length;
            for (i = 0; i < l; i++) {
                cmi = cm[i];
                colMap[cmi.name] = i;
            }
        }
    }
});

, , rawObject[11], 11 .

.

+2

Source: https://habr.com/ru/post/1788287/


All Articles