Skip to main content

YII cgridview Tricks

Yii cgridview make custom drop down filters, Yii cgridview set row color based on a condition, Yii cgridview add time picker in filter, Yii cgridview add drop down in colum values

Here is an example how you can make custom drop down filters in YII cgridview:




$this->widget('zii.widgets.grid.CGridView', array(
    'id' => 'card-applications-grid',
    'dataProvider' => $model->search(),
    'filter' => $model,
    'afterAjaxUpdate' => 'reinstallDatePicker',
    
    'rowCssClassExpression'=>'($data->is_updated==1)?"application_updated":"application_not_updated"',
    
    'columns' => array(
        array(
            'name'=>'applicant_id',
            'value'=>'$data->applicant_id',
            'filter'=>  CHtml::listData($model->findAll(), 'applicant_id', 'applicant_id')
            ),
        
        
        array(
            'name' => 'applicant_date',
            'filter' => $this->widget('zii.widgets.jui.CJuiDatePicker', array(
                'model'=>$model, 
                'attribute'=>'applicant_date', 
                 'language' => 'ja',
                'i18nScriptFile' => 'jquery.ui.datepicker-ja.js', // (#2)
                
                'htmlOptions' => array(
                    'id' => 'datepicker_for_applicant_date',
                    'size' => '30',
                ),
                'options' => array(
                'showAnim' => 'fold',
                'dateFormat'=>'yy-mm-dd',
                'changeMonth'=>true,
                'changeYear'=>true,
                'yearRange'=>'1950:2013',
                'minDate' => '1950-01-01',      // minimum date
                //'maxDate' => '2013-01-'
                ),
                'defaultOptions' => array(
                'showAnim' => 'fold',
                'dateFormat'=>'yy-mm-dd',
                'changeMonth'=>true,
                'changeYear'=>true,
                'yearRange'=>'1950:2013',
                'minDate' => '1950-01-01',      // minimum date
                //'maxDate' => '2013-01-'
                ),
            ), 
            true), // (#4)
        ),
        
        'applicant_time',
        
        
        
        array(
            'name'=>'appCreator_id',
            'value'=>'$data->appCreator->user_name',
            'filter' => CHtml::listData(Users::model()->findAll('user_type="appCreator"'), 'user_id', 'user_name' )
            ),
        
        
        array(
            'name'=>'apllicant_payment_status',
            'value'=>'CardApplications::getPaidUnpaid($data->apllicant_payment_status)',
            'filter'=>CHtml::listData(CardApplications::getPaidUnpaids(), 'id', 'title'),
        ),
        
        array(
            'name'=>'applicant_activation_status',
            'value'=>'CardApplications::getActivationStatus($data->applicant_activation_status)',
            'filter'=>CHtml::listData(CardApplications::getActivationStatuss(), 'id', 'title'),
        ),        
        array(
            'name'=>'applicant_status',
            'value'=>'CardApplications::getApplicationStatus($data->applicant_status)',
            'filter'=>CHtml::listData(CardApplications::getApplicationStatuss(), 'id', 'title'),
        ),        
        array(
            'name'=>'application_payment_method',
            'value'=>'CardApplications::getApplicationPaymentMethod($data->application_payment_method)',
            'filter'=>CHtml::listData(CardApplications::getApplicationPaymentMethods(), 'id', 'title'),
        ),        
        
        array(
            'name'=>'application_delivery_method',
            'value'=>'CardApplications::getApplicationDeliveryMethod($data->application_delivery_method)',
            'filter'=>CHtml::listData(CardApplications::getApplicationDeliveryMethods(), 'id', 'title'),
        ),        
        
        array(
            'name'=>'applicant_courrier_status',
            'value'=>'CardApplications::getApplicationCourrierStatus($data->applicant_courrier_status)',
            'filter'=>CHtml::listData(CardApplications::getApplicationCourrierStatuss(), 'id', 'title'),
        ),        
        
        
        array(
            //'name'=>'renew', 
            'header'=>'renew',
            'value'=>'CHtml::checkBox("renew", false)',
            'type'=>'raw',
            ),
        array(
            'name'=>'applicant_service_id',
            'value'=>  'ServiceTypes::model()->findByPk($data->applicant_service_id)->service_name'
            
        ),                
        
        'applicant_sex',
        'applicant_profession',
        'applicant_name',
        'applicant_sur_name',
        'applicant_fathers_name',
        'applicant_dob',
        
        'applicant_identifier',
        'applicant_social_security',
        'applicant_job',
        'applicant_afm',        
        array(
            'name'=>'applicant_doy',
            'value'=>  '$data->applicantDoy->doy_name'
        ),                
        
        array(
            'name'=>'applicant_country',
            'value'=>  '$data->applicantCountry->country_name'
        ),                
        
        array(
            'name'=>'applicant_region',
            'value'=>  '$data->applicantRegion->region_name'
        ),                
        
        array(
            'name'=>'applicant_county',
            'value'=>  '$data->applicantCounty->county_name'
        ),                
        
        
        array(
            //'name'=>'applicant_dependants',
            'header'=>'Dependants',
            'value'=>  "CHtml::dropDownList('','', CHtml::listData(CardApplications::getApplicationCourrierStatuss(), 'id', 'title'))",
             'type' => 'raw',
        ),                
        
        'applicant_city',
        'applicant_street_number',
        'applicant_zip_code',
        'applicant_landline',
        'applicant_mobile_phone',
        'applicant_email',
        'applicant_user_name',
        'applicant_password',
        'applicant_way_of_receiving',
        'applicant_desired_date_of_receipt',
        'applicant_delivery_time_from',
        'applicant_delivery_time_till',
        'applicant_comments',
        //'applicant_status',
        //'applicant_activation_status',
        //'apllicant_payment_status',
        //'application_payment_method',
        array(
            'class' => 'CButtonColumn',
            
        ),
    ),
));

Popular posts from this blog

Yii, return to previous url after login or logout

If you want to return to your previous url after login or logout try this : <?php $this -> redirect (Yii :: app () -> request -> urlReferrer ); ?> To set the return url to be the url that was before the login page or registeration page was called you can put following code in views/layouts/main.php file : <?php //this checks id the controller action is not 'login' then it keeps the current url in returnUrl if (CController :: getAction () -> id != 'login' ) { Yii :: app () -> user -> setReturnUrl (Yii :: app () -> request -> getUrl ()); } ?>

Yii2: Using csrf token

Yii2: Using csrf token First, if you do not understand what is the CSRF token? and why should we use it, please refer to the following link : https://www.owasp.org/index.php/Cross-Site_Request_Forgery_(CSRF) One of the new features of Yii2 is CSRF validation enabled by default. If you use ajax or basic form as follows : <form action='#' method='POST'> ........... </form> You will get an error exception : Bad Request (#400): Unable to verify your data submission That is because you do not submit csrf token. The easiest way if you dont care about csrf just disable it in main config : 'components' => [ 'request' => [ .... 'enableCsrfValidation'=>false, ], ..... ], Or in Controller : public function beforeAction($action) { $this->enableCsrfValidation = false; return parent::beforeAction($action); } So how to use Csrf Validation for your strong security website: * Wi