phpjqgrid-formatterjqgrid-php

Custom text on jqgrid column that takes data from a table


I've been making a code on PHP using jqgrid for displaying some data from a database table on mariadb. I am not really that accustomed with jqgrid and the thing I am trying to achieve just bothers me too much. The code as is, is able to successfully read all the columns from a database table called tblactionlog. The table has the following columns:

ActionId Primary int(11),
id int(11),
date datetime,
unit varchar(255),
weight_transferred double,
w_proc varchar(255),
wi_material varchar(255),
wi_product varchar(255),
buyer text,
idconverted int(11), 
action int(11)

So all I want is this: I want the code to read each column value in each row and display a custom message made by me that utilizes the values of the columns the message it displays depends on the number in the 'action' column (which is an integer from 0 to 9).

for example in case the row has values let's say

unit = Josh , action = 0 , weight_transferred = 500 , 
buyer = peter ,wi_material = apple 

then on this row of the grid shall display only one column with the message:

"Josh gave 500kg of apples to peter"

The code I have so far is as follows:

    <?php
// (...) lines that call the database
$userUnit = $_SESSION['deployment'];

// SQL Query
$grid->SelectCommand = "SELECT ActionId, id, date, unit, weight_transferred, w_proc, wi_material, wi_product, buyer, idconverted, action
                        FROM loginglobal.tblactionlog
                        WHERE unit = '$userUnit'";

$grid->table = 'tblactionlog';
// set the output format to JSON
$grid->dataType = 'json';
// Let the grid create the model from SQL query
$grid->setColModel();
// Set the URL from where we obtain the data
$grid->setUrl('adv-logger_grid.php');
// Set alternate background using altRows property
$grid->setGridOptions(array(
    "caption" => "Logging History",
    "hoverrows" => true,
    "autowidth" => true,
    "shrinkToFit" => true,
    "rowNum" => 100,
    "sortorder" => "desc",
    //"sortname"=>"ActionId",
    "altRows" => true,
    "multiselect" => false,
    "rowList" => array(
        100,
        200,
        500,
        1000,
        5000,
        10000
    ) ,
    //"footerrow"=>true,
    "userDataOnFooter" => true,
    "gridview" => true
));

// Function to generate custom messages
function generateCustomMessage($action, $date, $id, $weight)
{
    $logMessage = '';
    // Define log messages for each action value
    switch ($action)
    {
        case 0:
            $logMessage = "At $date you deleted entry with id $id which has $weight kgs of weight.";
        break;
        case 1:
            $logMessage = "At $date you added entry with id $id which has $weight kgs of weight.";
        break;
        default:
            $logMessage = 'Unknown action';
        break;
    }
    return $logMessage;
}

$grid->setColProperty("description", array(
    "name" => "description",
    "index" => "description",
    "label" => "desc",
    "width" => 200,
    "align" => "center",
    "sortable" => false,
    "formatter" => "custom",
    "formatoptions" => array(
        "custom_func" => "generateCustomMessage"
    )
));
$grid->setColProperty("ActionId", array("hidden" => true));
$grid->setColProperty("date", array("hidden" => true));
$grid->setColProperty("id", array("hidden" => true));
$grid->setColProperty("weight_transferred", array("hidden" => true));
$grid->setColProperty("w_proc", array("hidden" => true));
$grid->setColProperty("wi_material", array("hidden" => true));
$grid->setColProperty("wi_product", array("hidden" => true));
$grid->setColProperty("buyer", array("hidden" => true));
$grid->setColProperty("idconverted", array("hidden" => true));
$grid->setColProperty("action", array("hidden" => true));
$grid->setColProperty("unit", array("hidden" => true));

// Other parts of your code (...)
$PDF_HEADER_LOGO_WIDTH = 0;
$PDF_HEADER_TITLE = $dataCompany['CompanyName'];

// PDF options
//(...)
//most of the parts I Deleted using (...)
$grid->setGridEvent('onSortCol', $sorcol);
$grid->setGridEvent('loadComplete', $loadcomplete);
$grid->renderGrid('#grid', '#pager', true, null, null, true, true);
$conn = null;

mysql_close($glogin_mysql);
mysql_close($mysql);
?>

Solution

  • There are two main problems in your script.

    1. The field description is not defined in your SQL. setColProperty apply only to already existing fields. You will need to define it.

    2. You mix php code with javascript code in a part of formatter.

    3. There is no formatter=>"custom". The custom proerty in formatter does not exists. This options are "number", "integer", "select"... - see docs.

    To solve you will need to modify your query to have description. One possible solution is like this

    $grid->SelectCommand = "SELECT '' as description, ActionId, id, date, 
                                    unit, weight_transferred, w_proc, 
                                    wi_material, wi_product, buyer, 
                                    idconverted, action 
                            FROM loginglobal.tblactionlog 
                            WHERE unit = '$userUnit'";
    

    and then for description field to add javascript function

    Javascript function can be added like this (this is just example and is not releated to your problem - you will need to write it your own)

    $grid->setColProperty("description", array(
        "label" => "desc",
        "width" => 200,
        "align" => "center",
        "sortable" => false,
        "formatter" => "js:myCustomFormat"
    ));
    
    
    // We can put JS from php
    $custom = <<<CUSTOM
    function myCustomFormat(cellValue, options, rowObject) {
    /// here your javascript code
    // use rowObject to get the needed fields
    var newvalue = "some value";
    return newvalue;
    }
    CUSTOM;
    // Let set the code which is executed at end
    $grid->setJSCode($custom);
    ...
    

    Please note the js: before the myCustomFormat.

    Good luck