Get KoolPHP UI with 30% OFF!

Link a class instance to a row

Jayme
Hello,
I have a KoolGrid which is showing a list of alarms of the system.
I would like to link each row to an instance of my Alarm class.
How can I do that ? Does KoolGrid Row have a holder to carry the class on ?
Best regards.
Posted Feb 7, 2017 , edited Feb 7, 2017 Kool
Jayme
As a matter of fact I would like to attach an object with properties and methods to the KoolGrid Row.
Posted Feb 7, 2017 Kool
Anthony Amolochitis
Use the grid event handler. On row prerender, create the object there.
I have a sample to create the handler on the following link.
https://www.koolphp.net/forum/threads/82.1/koolgrid-with-an-event-handler-class--basic-crud-ops-included-in-the-class.html
You can add the row prerender event to this class.
Then create your object on the row dataitem array in the pre render of each row.
// stuff before if needed
$row->DataItem["AlarmClassObject"] = new AlarmClass();
// stuff afterwards if needed
Posted Feb 7, 2017 Kool
Jayme
Hello Anthony.
Thank very much for answering me and sharing your code sample.
I am very sorry, but I did not find the property you have mentioned $row->DataItem[""] on the KoolGrid documentation.
I am using the KoolGrid as is. I did not make any change on its code.
Best regards.
Posted Feb 7, 2017 Kool
Anthony Amolochitis
The DataItem array is your row array.
I discovered it in my debugger.
It's there.
Posted Feb 7, 2017 Kool
Jayme
So, DataItem is the columns array of the row,.
Have you created an extra column to use as the holder of the class ?
Best regards.
Posted Feb 7, 2017 Kool
Anthony Amolochitis
Yes, many times over. Notice the field names with the word "custom" in them. Those don't exist in the database.
They are created in row pre render.
Example with Custom Column in conjunction with row pre render event and adding custom fields to my row.
    protected function TemplateColumn()
    {           
        $fieldsetStyle = ' style="padding:4px; border:1px solid lightgrey; border-radius:2px; min-height:115px; background-color:white;" ' ;
        $legendStyle   = ' style="padding:4px; border:1px solid lightgrey; border-radius:2px; background-color:white;font-weight:bold;" ' ;
        
        $customerInfoSection = '<fieldset '.$fieldsetStyle.'>'
                . '<legend '.$legendStyle.' >{poSetupInfoHeader}</legend>'
                . '{customerInfoBlock}'
                . '</fieldset>';
        
        $poInfoSection = '<fieldset '.$fieldsetStyle.'>'
                . '<legend '.$legendStyle.' > PO Information - PO ID# {po_id} </legend>'
                . '<table width="100%">'
                . '<tr>'
                    . '<td>Product</td>'
                    . '<td>{product_name}</td>'
                . '</tr>'
                . '<tr>'
                    . '<td>Po Number</td>'
                    . '<td>{po_number}</td>'
                . '</tr>'
                . '<tr>'
                    . '<td>Order Quantity</td>'
                    . '<td>{order_quantity}</td>'
                . '</tr>'
                . '<tr>'
                    . '<td>Printer</td>'
                    . '<td>{partner}</td>'
                . '</tr>'
                . '<tr>'
                    . '<td>Due Date</td>'
                    . '<td>{due_date_custom}</td>'
                . '</tr>'
                . '</table>'
                . '</fieldset>';
        
        $statusHeaderRow = ' style="color:black; font-weight:bold; border-bottom:1px dashed lightgrey;" width="14%" align="center" ' ;
        $statusDataRow   = ' style="color:rgb(100,100,100);" align="center" ' ;
                
        $statusInfoSection = '<fieldset style="padding:4px; border:1px solid lightgrey; border-radius:2px; min-height:50px; background-color:white;">'
                . '<legend '.$legendStyle.' >{customCompletedStatus}</legend>'
                . '<table width="100%">'
                . '<tr>'
                    . '<td '.$statusHeaderRow.'>Paid</td>'
                    . '<td '.$statusHeaderRow.'>List</td>'
                    . '<td '.$statusHeaderRow.'>Art</td>'
                    . '<td '.$statusHeaderRow.'>Shell</td>'
                    . '<td '.$statusHeaderRow.'>Variable</td>'
                    . '<td '.$statusHeaderRow.'>Co-op</td>'
                    . '<td '.$statusHeaderRow.'>Compliance</td>'
                . '</tr>'
                . '<tr>'
                    . '<td'.$statusDataRow.'>{paid_status_custom}</td>'
                    . '<td'.$statusDataRow.'>{list_status_custom}</td>'
                    . '<td'.$statusDataRow.'>{art_status_custom}</td>'
                    . '<td'.$statusDataRow.'>{shell_proof_custom}</td>'
                    . '<td'.$statusDataRow.'>{var_proof_custom}</td>'
                    . '<td'.$statusDataRow.'>{need_coop_custom}</td>'
                    . '<td'.$statusDataRow.'>{need_compliance_custom}</td>'
                . '</tr>'
                . '</table>'
                . '</fieldset>';
        
        $template = '
            <table width="100%">
            <tbody>
            <tr>
                <td width="50%" valign="top">'. $customerInfoSection .'</td>
                <td width="50%" valign="top">'. $poInfoSection.'</td>
            </tr>
            <tr>
                <td colspan="2">'.$statusInfoSection.'</td>
            </tr>
            </tbody>
            </table>
            ' ;
        
        $column = new GridCustomColumn()    ;
        $column->DataField  = 'po_id'       ;
        $column->Width      = '600px'       ;
        $column->HeaderText = ''            ;
        $column->Valign     = 'top'         ;
        $column->CssClass   = ''            ;
        $column->AllowHtmlRender = true     ;
        $column->ItemTemplate = $template   ;
        $this->KoolGrid->MasterTable->addcolumn($column) ;
    }

Example of row pre render event :
    /**
     * @param GridRow $row
     * @param array $args
     */
    function OnRowPreRender($row,$args)
    {   
        $resellerName = $this->PoSetupGrid->PoDataCollection->Po_settingsData->default_companyname ;
        if( empty( $resellerName) )
        {
            $resellerName = $this->PoSetupGrid->resellerData->ThirdpartyresellerData->name ;
        }
        
        
        $row->DataItem['poSetupInfoHeader'] = 'Purchase order for '. $resellerName ;
        $salesRepName = PoSetupGridMethod::GetSelectedSalesmanName($this->PoSetupGrid->dbConnect , $this->PoSetupGrid->PoDataCollection) ; // None Selected 
        // load both properties from the data collection object
        $Co_op_DealershipCodeProp = $this->PoSetupGrid->custData->GetPropertyArray( CustomerPropertyList::PO_Prop_CoopDealerCode ) ;
        $DealershipBankProp       = $this->PoSetupGrid->custData->GetPropertyArray( CustomerPropertyList::PO_Prop_BankApprove    ) ;
        $coopData = Custinfo_propertiesData::GetData( $Co_op_DealershipCodeProp );
        $bankData = Custinfo_propertiesData::GetData( $DealershipBankProp );
        
        $row->DataItem['customerInfoBlock'] =  '<b>' 
            . $this->PoSetupGrid->custData->CustinfoData->coname    . '</b><br/>'
            . $this->PoSetupGrid->custData->CustinfoData->addr      . ', '
            . $this->PoSetupGrid->custData->CustinfoData->city      . ' ' 
            . $this->PoSetupGrid->custData->CustinfoData->state     . ' '
            . $this->PoSetupGrid->custData->CustinfoData->zip       . '<br/>'
            . '<label style="color:grey;">Sales Rep:</label> ' . $salesRepName . '<br/><br/>';
        
        if( !empty( $coopData->propertyValue) )
        {
            $row->DataItem['customerInfoBlock'] .= '<label style="color:grey;">Co-op Code : </label>' . $coopData->propertyValue . '<br/>' ;
        }
        
        if( !empty( $bankData->propertyValue) && $bankData->propertyValue != 'NA' )
        {
            $row->DataItem['customerInfoBlock'] .= '<label style="color:grey;">Bank Approval : </label>' . $bankData->propertyValue . '<br/>' ;
        }
        
        $row->DataItem['paid_status_custom']    = $this->GetStatusBackground( $row->DataItem['paid_status'] ) ;
        $row->DataItem['list_status_custom']    = $this->GetStatusBackground( $row->DataItem['list_status'] ) ;
        $row->DataItem['art_status_custom']     = $this->GetStatusBackground( $row->DataItem['art_status']  ) ;
        $row->DataItem['shell_proof_custom']    = $this->GetStatusBackground( $row->DataItem['shell_proof'] ) ;
        $row->DataItem['var_proof_custom']      = $this->GetStatusBackground( $row->DataItem['var_proof']   ) ;
        $row->DataItem['need_coop_custom']      = $this->GetStatusBackground( $row->DataItem['need_coop']   ) ;
        $row->DataItem['need_compliance_custom']= $this->GetStatusBackground( $row->DataItem['need_compliance'] ) ;
        
        $row->DataItem['due_date_custom'] = date( StandardFormatDate::HumanReadableNoTime , strtotime( $row->DataItem['due_date'] )) ;
        
        $row->DataItem['customCompletedStatus'] = 'Purchase Order Status Section';
        if( intval( $row->DataItem['completed'] ) == 1 )
        {
            $row->DataItem['customCompletedStatus'] = '<div style="background-color:#81ff78; padding:2px;'
                . 'border:1px solid green; border-radius:4px;">'
                . 'Purchase Order Status Section - Purchase Order has been marked complete </div>';
        }                
    }

Here is an image of what is made.
Posted Feb 7, 2017 , edited Feb 7, 2017 Kool
Jayme
Thank you again for sharing your code.
I think I should change the approach because I need to get access to the properties of the Alarm Class at client side, I think I should create the objects using JavaScript and put them in an array .
Posted Feb 7, 2017 Kool
Anthony Amolochitis
On pre render you can assign a custom call to the methods in your client side script as well.
I do this to click to send email notifications and other random things.
Posted Feb 7, 2017 Kool