stevenyang1011
11/15/2017 - 8:59 PM

Example of a customized Grid column with: -- left join subquery table -- renderer -- filter

Example of a customized Grid column with: -- left join subquery table -- renderer -- filter

//    protected function _prepareCollection()
//  ........
       
        $jobProductQuery = 'select a.`version`, a.`product_id` from `straker_job_product` as a
                            left join `straker_job` as b on a.`job_id`=b.`id`
                            where b.`store_id` = '.$store->getId().' and a.`version` =1
                            GROUP BY a.`product_id`';

        //join straker job product table to get version for each product
        $collection->getSelect()->joinLeft(

          new Zend_Db_Expr('('.$jobProductQuery.')'),
          'e.entity_id = t.product_id',
          array('version')

        );
        
//        ......... 
// }
        
        
        
//    protected function _prepareColumns()      { 
//  ........ 

                $this->addColumn('version',
            array(
                'header'=> Mage::helper('catalog')->__('Translated'),
                'width' => '70px',
                'index' => 'version',
                'type'  => 'options',
                'options' => array(
                    'Translated'   => Mage::helper('catalog')->__('Translated'),
                    'Not Translated'   => Mage::helper('catalog')->__('Not Translated')
                ),
                'renderer'  => 'StrakerTranslations_EasyTranslationPlatform_Block_Adminhtml_Template_Grid_Renderer_Translated',
                'filter_condition_callback' => array($this, '_versionFilter'),
            ));
            
//        ......... 
// }    

            
protected function _versionFilter($collection, $column)
    {
        if (!$value = $column->getFilter()->getValue()) {
            return $this;
        }

        if ($value == 'Translated' ){
            $collection->getSelect()->where('t.version is not null');

        } elseif ($value == 'Not Translated'){
            $collection->getSelect()->where('t.version is null');
        }

        return $this;
    }
        
        
        
// renderer file        
<?php
class StrakerTranslations_EasyTranslationPlatform_Block_Adminhtml_Template_Grid_Renderer_Translated
    extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
    public function render(Varien_Object $row)
    {
        if($row->getVersion()){
            return 'Translated';
        }
        else{
            return 'Not Translated';
        }
    }
}