Get KoolPHP UI with 30% OFF!

Using the insert and edit template

Anthony Amolochitis
1. Create your base class to do your form building
class Grid_InsertEditTemplateFields
{    
    
    /**
     * @param Grid $Grid
     * @param GridRow $row
     * @return string
     */
    public static function GetDescription( &$Grid , &$row , $other )
    {
        $value = ( isset( $row->DataItem['Description'] ) ) ? ( $row->DataItem['Description'] ) : '' ;
        return '<input type="text" name="Description" value="'.$value.'" '.$other.'/>';        
    }
    
    
    /**
     * @param Grid $Grid
     * @param GridRow $row
     * @return string
     */
    public static function GetName( &$Grid , &$row , $other )
    {
        $value = ( isset( $row->DataItem['name'] ) ) ? ( $row->DataItem['name'] ) : '' ;
        return '<input type="text" name="name" value="'.$value.'" '.$other.'/>';        
    }
    
    /**
     * Build an insert/edit form 
     * @param Grid $Grid
     * @param GridRow $row
     * @return string
     */
    public static function GetInputForm( &$Grid , &$row )
    {        
        /**
         * Make sure to add in the hidden fields as well if needed
         */
        $style = ' style="width:400px;" class="form_TextInput" ' ;
        
        $table = '
            <table align="center" width="850px" style="border:none;" class="ui-widget ui-widget-content">
            <tbody>
            <tr>
            <td style="width:250px;">Description</td>
            <td style="width:625px;">'. self::GetDescription($Grid, $row, $style ) . '</td>
            </tr>
            <tr>
            <td style="width:250px;">Name</td>
            <td style="width:625px;">'. self::GetName($Grid, $row, $style ) . '</td>
            </tr>
            </tbody>
            </table>
            ' ;
        
        return $table ;
    }
    
    /**
     * Return an array with the data for the row data item object to function correctly
     * @param Grid $Grid
     * @param GridRow $row
     * @return string
     */
    public static function GetData( &$Grid , &$row )
    {
        $returnArray = array();
                
        //
        // loop through field names and add them to an array
        // if they exist
        // They will be placed in the $row->DataItem array
        // NOTE: $Grid is not a koolgrid, but a class with the koolgrid as a variable in it
        // This way I can attach field names to it
        // 
        foreach( $Grid->field as $fld )
        {
            if( isset( $_POST[$fld] ) )
            {
                $returnArray[$fld] = $_POST[$fld] ;
                                
                if( isset( $row->DataItem[$fld] ) )
                {
                    $row->DataItem[$fld] = $_POST[$fld] ;
                }
            }
        }
        
        return $returnArray ;
    }
    
}

2. Build your grid insert and edit templates to customize your input forms. Utilize the base class.
class Grid_InsertTemplate implements gridtemplate
{
    /**
     * @var Grid 
     */
    var $Grid ;
        
    /**
     * Access to the Grid Container object that holds my grid
     * This is not mandatory, but is very useful when trying to pass additional
     * data along with your grid to the template class
     * @param Grid $Grid
     */
    function __construct(&$Grid) 
    {
        $this->Grid = $Grid ;
    }
    
    /**
     * Produces buttons, but uses jquery
     * Can modify to your liking
     * @return string
     */
    protected function GetButtons()
    {
        $jqueryButton = ' <script> '
                . '$(function(){ '
                . ' $("#BackButton").button(); '
                . ' $("#SaveButton").button(); '
                . '}); </script> ';
        
        $confirmButton = '
            <input type="button" id="SaveButton" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text" 
            style="color:green; font-size:12px;border-color:green;" aria-disabled="false" onclick="grid_confirm_insert(this)" value="Save" />';
        
        $cancelButton = '
            <input type="button" id="BackButton" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text" 
            style="color:red;font-size:12px;border-color:#F78181;" aria-disabled="false" onclick="grid_cancel_insert(this)" value="Back" />';
        
        return '<table>
                <tr>
                <td style="padding:5px;">'.$cancelButton.'</td> <td>'.$confirmButton.'</td>
                </tr>
                </table>' . $jqueryButton ;
    }
    
    /**
     * Build the edit form here
     * @param GridRow $_row
     * @return string
     */
    function Render($_row)
    {
        $inputForm = Grid_InsertEditTemplateFields::GetInputForm($this->Grid, $_row);
        $buttons   = $this->GetButtons() ;
        
        return '<table class="ui-widget-content ui-corner-all" style="width:800px;">
                <thead>
                    <tr>
                    <th align="center" colspan="2">
                        <div class="ui-widget-header ui-corner-all" style="font-size: 14px;padding:5px; width="800px">
                            Input Form 
                        </div>
                    </th>
                    </tr>
                </thead> 
                <tbody>
                <tr>
                <td>'. $inputForm .'</td>
                </tr>
                <tr>
                <td align="right">'.$buttons.'</td>
                </tr>
                </tbody>
                </table>
                ';
    }
            
    /**
     * Same action occurs for insert and edit
     * @param GridRow $_row
     * @return array
     */
    function GetData($_row)
    {
        return Grid_InsertEditTemplateFields::GetData($this->Grid, $_row);
    }
    
}
class Grid_EditTemplate implements gridtemplate
{
    /**
     * @var Grid 
     */
    var $Grid ;
        
    /**
     * Access to the Grid Container object that holds my grid
     * This is not mandatory, but is very useful when trying to pass additional
     * data along with your grid to the template class
     * @param Grid $Grid
     */
    function __construct(&$Grid) 
    {
        $this->Grid = $Grid ;
    }
    
    /**
     * Produces buttons using jquery buttons
     * Not mandatory
     * @return string
     */
    protected function GetButtons()
    {   
        $jqueryButton = ' <script> '
                . '$(function(){ '
                . ' $("#BackButton").button(); '
                . ' $("#SaveButton").button(); '
                . '}); </script> ';
        
        $confirmButton = '
            <input type="button" id="SaveButton" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text" 
            style="color:green; font-size:12px;border-color:green;" aria-disabled="false" onclick="ConfirmEditWhiteLabelTemplate(this)" value="Save" />';
        
        $cancelButton = '
            <input type="button" id="BackButton" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text" 
            style="color:red;font-size:12px;border-color:#F78181;" aria-disabled="false" onclick="ConfirmCancelEditWhiteLabelTemplate(this)" value="Back" />';
        
        return '<table>
                <tr>
                <td style="padding:5px;">'.$cancelButton.'</td> <td>'.$confirmButton.'</td>
                </tr>
                </table> ' . $jqueryButton ;
        
    }
    
    /**
     * Build the edit form here
     * @param GridRow $_row
     * @return string
     */
    function Render($_row)
    {   
        $inputForm = Grid_InsertEditTemplateFields::GetInputForm($this->Grid, $_row);
        $buttons   = $this->GetButtons() ;
        
        return '<table class="ui-widget-content ui-corner-all" style="width:800px;">
                <thead>
                    <tr>
                    <th align="center" colspan="2">
                        <div class="ui-widget-header ui-corner-all" style="font-size: 14px;padding:5px; width="800px">
                            Edit Form
                        </div>
                    </th>
                    </tr>
                </thead> 
                <tbody>
                <tr>
                <td>'. $inputForm .'</td>
                </tr>
                <tr>
                <td align="right">'.$buttons.'</td>
                </tr>
                </tbody>
                </table>
                ';
    }
            
    /**
     * Same action occurs for insert and edit
     * @param GridRow $_row
     * @return array
     */
    function GetData($_row)
    {
        return Grid_InsertEditTemplateFields::GetData($this->Grid, $_row);
    }
    
}
Posted Dec 10, 2015 , edited Dec 10, 2015 Kool -