Get KoolPHP UI with 30% OFF!

Lookup and set displayed cell text text based on database field

Chris Baron
I have a grid linked to a MySQL table. Where I want to display just the field contents, KoolGrid is working great but I have a couple of columns where I need to "lookup / calculate" the text to be displayed based on the associated DataField.
For instance I have a DataField called "status" which returns a single character "A / L / O" which needs to be displayed as a human readable status "Active / Listed / Sold". I'm guessing I need to somehow intercept the render for the cell to call some PHP or Javascript to do this "lookup" (and I can write the code to do the lookup) but I don't know how to intercept the render to change the text that will be displayed in the cell.
Similarly I have another Datafield called "sku" which I need to use to lookup the name of the product image and display it. I have a PHP routine (in Magento library) that looks up and returns the name, but again I don't know how to incercept the render to then return the appropriate html to display the image.
Any advice would be appreciated.
Thanks.
Posted Sep 21, 2015 Kool -
David
Hi Chris,
It looks like you could set a value map for your datasource like in this example:
Reverse a column's content
Please open the file GridIValueMap.php in KoolGrid folder and see the standard IdentifyMap and ReversveMap classes that implement the GridIValueMap interface. You could modify the ReverseMap class for your data field "Status" (says StatusMap) and then set the value map like this:
function changeString($status) {
//...
}
function inverseChangeString($status) {
//...
}
class StatusMap implements GridIValueMap
{
    function mapValue($value, $column)
    {
        if ($column == "status")
            $value = changeString($value);
        return $value;
    }
    
    function inverseMapValue($value, $column)
    {
        if ($column == "status")
            $value = inverseChangeString($value);
        return $value;
    }    
}
$ds->setValueMap(new StatusMap());

The function inverseMapValue is only used when editing and saving the column's content. So if the column is readonly you can just return $value without inversing it. Hope this helps!
Rgds,
Posted Sep 22, 2015 , edited Sep 22, 2015 Kool -
Chris Baron
Hi David
Thanks, that makes sense and looks reasonably easy. Sadly I guess this must be for the latest version as on my version 60 there's only on file, koolgrid.php in that folder and it's not a meaningfully readable file as it's obviously been compressed or put through something to stop reverse engineering.
Any ideas for something that will work with version 60
Thanks
Chris
Posted Sep 22, 2015 Kool -
Peter
KoolGrid has changed so much since version 60. Please consider to renew the license. Renewing the license with only $199, you will own the source-code as well.
Posted Sep 22, 2015 Kool
David
Hi Chris,
The value mapping feature is only available recently so I guess it could not be applied to version 60. Another way you can do is changing your select command to convert the field's content by sql and pass it to the grid's column.
Rgds,
Posted Sep 23, 2015 Kool
Chris Baron
Hi David
Thanks, that's actually what I did. It meant creating another unnecessary table in the database (which I'd hoped to avoid) and doing a join but it does the job. Thanks for your suggestions.
Kind regards
Chris
Posted Sep 23, 2015 Kool