Search code examples
phpjqgrid

How can i write condition in jqgrid colmodel


Can u help me. I have the following colmodel:

colNames: ["Name","Value", "upper limit", "lower limit", "stock"],
            colModel: [
                { name: 'Num', index:'num', align:"center", sortable:false,width: 40, resizable:false },
                { name: 'Value', index:'Value', align:"center", width: 70,
                    cellattr: function (rowId, cellValue, rawObject, cm, rdata) {
                        // {
                        //     if (rawObject[1] >= 300 || rawObject[1] <= 50) {
                        //         var colorText = 'style="font-weight: bold;font-size: 24px;+colorText"';
                        //     } else {
                        //         var colorText = 'style="font-weight: bold;font-size: 24px;color:red"';
                        //     };  return(colorText);}},
                        console.log (rawObject[1])
                    }},

                {name:'upper', index:'upper', align:"center", width: 25, resizable:false,editable:true,
                    cellattr: function(rowId, val, rawObject){return 'style="font-size: 15px;"';}},
                {name:'lower', index:'lower', align:"center", width: 25, resizable:false, editable:true,
                    cellattr: function(rowId, val, rawObject){return 'style="font-size: 15px;"';}},
                { name: 'stock', index:'stock', align:"center", sortable:false,width: 40, resizable:false }
                ],

Below is what I get from get-data.php.

$array = array(
array("1","$f1rst", "300","50"),//: Закладочный ствол
array("2","$second","350","60"),//: Клетьевой ствол
array("3","$three","120","20"),//: Портал
array("4","$four","200","20"),//: Скиповой ствол
array("5","$five","200","0"));//: ЦВС


//var_dump($array);
if (empty($array)) {
    echo '["Нет данных"]';
}
else {echo json_encode($array);}

index.php example

I need to compare values. For example: there is the first value in the first cell equal to 60. If 'value'> = 300 or 'value' <= 50, then paint the cell red. Next, the second cell. If value> = 350 or value <= 60. And so with all five meanings.

upd. In my example, the code doesn't work. It does a conditional check on the entire column. And I need for each cell

upd2. see how it works enter image description here


Solution

  • Tony Tomov gave the correct answer. Thank you very much, everything worked out and everything works

    Below is a link to an example

    example

     {label: 'Название', name: 'Num', index: 'num', align: "center", sortable: false, width: 40, resizable: false},
            {
              label: 'Значение', name: 'Value', index: 'Value', align: "center", width: 70,
              cellattr: function (rowId, cellValue, rawObject, cm, rdata) {
                if (rdata["Num"] === '1' && (rdata['Value'] >= 300 || rdata['Value'] <= 50)) {
                  return ' class="class1" ';
                } else if (rdata["Num"] === '2' && (rdata['Value'] >= 350 || rdata['Value'] <= 60)) {
                  return ' class="class1" ';
                } else if (rdata["Num"] === '3' && (rdata['Value'] >= 120 || rdata['Value'] <= 20)) {
                  return ' class="class1" ';
                } else if (rdata["Num"] === '4' && (rdata['Value'] >= 200 || rdata['Value'] <= 20 || isNaN(rdata['Value']))) {
                  return ' class="class1" ';
                } else if (rdata["Num"] === '5' && (rdata['Value'] >= 200 || rdata['Value'] <= 0)) {
                  return ' class="class1" ';
                }
              }
            },