Social Icons

Pages

Featured Posts

Monday, July 8, 2013

filter by concatenation of model fields in CGridView

I have user model with first_name and last_name . It will show as full name (first_name+last_name). Now i want to search in Cgridview.


class User extends CActiveRecord
{
        public $fullName;


in search

$criteria->addSearchCondition('concat(first_name, " ", last_name)', $this->fullName); 


in rules()
array('..., fullName', 'safe', 'on' => 'search'),

in model  created a function
 public function getFullName()
        {
                return $this->first_name . ' ' . $this->last_name;
        }

And last I've put it in my view:

<?php $this->widget('zii.widgets.grid.CGridView', array(
      
        .......
          'columns' => array(
                array(
                        'name' => 'full_name',
                        'value' => '$data->getFullName()',
                ),
               
                ...


Tuesday, July 2, 2013

Yii ajax request and json response

This  will help you to understand the details of yii ajax request and response from controller.

In view.php


<?php
Yii::app()->clientScript->registerScript('productform','
$("#category").live("change",function(){
 $.ajax({
   url:'".Yii::app()->createAbsoluteUrl("site/productlist")."',
   type:"POST",        
   data:"catid="+$("#Cat_id option:selected").val(),
   dataType:"json",
   "success":function(data){            
         if(data==null){
              $("#product_type").empty();
         }else{
              var obj = eval(data);
              $("#product_type").empty();
              $.each(obj, function(key, value) {
                 $("#product_type").append("<option value="+key+">"+value+"</option>");
              });
                 
         }
       }
      });
    });
');
?>


<div class="row">
<?php echo $form->labelEx($model,'category'); ?>
<?php echo $form->dropDownList($model,'category',$categorylist); ?>
<?php echo $form->error($model,'category'); ?>
 </div>

<div class="row">
<?php echo $form->labelEx($model,'product_type'); ?>
<?php echo $form->dropDownList($model,'product_type',
                                array(''=>'Select Product')); ?>
<?php echo $form->error($model,'product_type'); ?>
 </div>
?>






controller.php


<php
class SiteController extends Controller
{
public function actionProductlist()
 {
  if(Yii::app()->request->isPostRequest)
  {
    if(isset($_POST['catid']) && $_POST['catid']!=''){
       $Productmodel=Product::model()->
                         findAll(array('condition'=>'isactive=1 and 
                         catid='.$_POST['catid'],'order'=>'name'));
       if($Productmodel){
          $data=CHtml::listData($Productmodel,'productid','name');
          print_r(json_encode($data));
       }
    }
  }
  else
   throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
 }
}
?>



Thursday, June 6, 2013

Yii URL Management

Yii allows to customize your url
Eg: http://localhost/liveYii/index.php?r=User/admin
change to
http://localhost/liveYii/User/admin

You have to do edit the files

1) protected/config/main.php

'components'=>array(
..........
'urlManager' => array(
'urlFormat' => 'path',
'showScriptName' => false,
'rules' => array(
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
),
....................................
);

2) .htaccess

RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(images|js)
# otherwise forward it to index.php
RewriteRule . index.php

Monday, May 27, 2013

Ajax SubmitButton Yii

  1. <?php echo CHtml::ajaxSubmitButton( 'Send',
  2.                                         CHtml::normalizeUrl(array('site/ajaxIndexSubmit')),
  3.                                         array(
  4.                                         'error'=>'js:function(){
  5.                                             alert('error');
  6.                                         }',
  7.                                         'beforeSend'=>'js:function(){
  8.                                             alert('beforeSend');
  9.                                         }',
  10.                                         'success'=>'js:function(data){
  11.                                             alert('success, data from server: '+data);
  12.                                         }',
  13.                                         'complete'=>'js:function(){
  14.                                             alert('complete');
  15.                                         }',
  16.                                         //'update'=>'#where_to_put_the_response',
  17.                                         )
  18.                                     );
  19.     ?>

Thursday, May 23, 2013

cgridview value from functions

 In CGridView,  a column where  want to modify the value with a getter function from a model I have loaded. 
view Cgridview

'columns'=>array(
                .............
                array(
                     'name'=>'subject',
                     'type'=>'raw',
                        'value'=>array($model,'subjectFormated'), 
                ),
            .........................
),

in model function subjectFormated()

public static function subjectFormated($model){
       ////will get model->subject 
.....your code...................
}

Wednesday, May 22, 2013

Create custom button with AJAX function in CGridView


First we need to create the required action in our controller, we will call it simply ‘status’.  To begin we add it in our controller rules ‘actions’ array:
array('allow'
      'actions'=>array('view''update', 'delete', 'status')),
      'users'=>array('admin'),
),

Next we need to create an action for setting our record status:


public function actionStatus($id)

{
    $model = MyModel::model()->findByPk($id);  // use whatever the correct class name is
    $model->status = ($model->status ==1 ? 1 : 0);
    $model->save();
    return true;




Now we are ready to create our custom button.  If you look at a standard Yii generated cGridview, toward the bottom you will see the following:
array(
     'class'=>'CButtonColumn',
),
This we need to extend to include both our standard buttons plus our new custom button, making use of ‘ajax’ in the ‘options’ array:
array(
     'class'=>'CButtonColumn',
     'template' => '{view}{update}{status}{delete//include the standard buttons plus the new status button
     'buttons'=>array
     (
         'status' => array
         (
             'label'=>'status',

             'imageUrl'=>'images/icn/status.png'// make sure you have an image
             'url'=>'Yii::app()->createUrl("mycontroller/status", array("id"=>$data->id))',
             'options' => array(


 'confirm'=>'Are you want to change status?',
'ajax' => array('type' => 'get', 'url'=>'js:$(this).attr("href")', 'success' => 'js:function(data) { $.fn.yiiGridView.update("my-grid")}')
         ),
     ),
),



Important things to note above:
  1. You need an image for your new button, I have used ‘images/icn/status.png’
  2. For the yiiGridView update() function, make sure that you enter the correct ID for your grid



further Reference

http://www.yiiframework.com/wiki/410/create-custom-button-button-with-ajax-function-in-cgridview/

http://www.mattiressler.com/customising-cgridview-custom-cbuttoncolumn-ajax-buttons/

http://www.yiiframework.com/wiki/106/using-cbuttoncolumn-to-customize-buttons-in-cgridview/