Social Icons

Pages

Friday, March 29, 2013

tbtabs yii ajax

In view

<?php $this->widget('bootstrap.widgets.TbTabs', array(
        'id' => 'mytabs',
        'type' => 'tabs',
        'tabs' => array(
           
                array('id' => 'tab2', 'label' => 'Tab 2', 'content' => 'loading ....'),
                array('id' => 'tab3', 'label' => 'Tab 3', 'content' => 'loading ....'),


        ),
        'events'=>array('shown'=>'js:loadContent')
    )
);?>



<script type="text/javascript">

function loadContent(e){

    var tabId = e.target.getAttribute("href");

    var ctUrl = '';

    if(tabId == '#tab2') {
        ctUrl = 'controller/action';


//eg: ctUrl = '<?php echo $this->createUrl('emailQueues/test/id/'.$id)?>';




    } else if(tabId == '#tab3') {
        ctUrl = 'url to get tab 3 content';
    }

    if(ctUrl != '') {

        $.ajax({


            url      : ctUrl,
            type     : 'POST',
            dataType : 'html',
            cache    : false,
            success  : function(html)
            {
                jQuery(tabId).html(html);
            },
            error:function(){
                    alert('Request failed');
            }
        });
    }

    preventDefault();
    return false;
}

</script>



Before Save Function in Model

beforeSave() -- model is a nice function in yii framework

When we update record in table, Some fields are changed by default like when record created, when record modified etc. Here I wrote code for this action. In Format 1, I configured separately for created date, modified date. $this->isNewRecord is return true, if it is new record. Now I affected date only If $this->isNewRecord is return false, I will affect modified date only. In Format 2, I configured both created date, modified date in beforeSave() function. In this code, created date will affected only on new record. modified date will affected on when recored created or When record modified This function reduce our workflow. Just make this function once It was very helpful.




class Mail extends CActiveRecord
{
    // Format 1
    public function beforeSave() 
    {
        if($this->isNewRecord)
        {           
            $this->date=new CDbExpression('NOW()');
            $this->status=0; //option by default
        }else{
             $this->modified = new CDbExpression('NOW()');
        }
        return parent::beforeSave();
    }

    // Format 2

    public function beforeSave() 
    {
        if($this->isNewRecord)
        {           
            $this->date=new CDbExpression('NOW()');
            $this->status=0; //option by default
        }
        $this->modified = new CDbExpression('NOW()');
        return parent::beforeSave();
    }
}










Thursday, March 28, 2013

gridview view in modal popup Yii


In view

$this->widget('bootstrap.widgets.TbExtendedGridView', array(
    'type'=>'bordered',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    'template'=>"{items}",
    'columns'=>array(
        'id',
        'firstName',
        'lastName',
        'language',
        'hours',
        array(
            'header'=>'Options',
            'class'=>'bootstrap.widgets.TbButtonColumn',
            'buttons'=>array(
                'view'=>
                    array(
                        'url'=>'Yii::app()->createUrl("person/view", array("id"=>$data->id))',
                        'options'=>array(
                            'ajax'=>array(
                                'type'=>'POST',
                                'url'=>"js:$(this).attr('href')",
                                'success'=>'function(data) { $("#viewModal .modal-body p").html(data); $("#viewModal").modal(); }'
                            ),
                        ),
                    ),
            ),
        )
    )));
?>

<!-- View Popup  -->
<?php $this->beginWidget('bootstrap.widgets.TbModal', array('id'=>'viewModal')); ?>
<!-- Popup Header -->
<div class="modal-header">
<h4>View Employee Details</h4>
</div>
<!-- Popup Content -->
<div class="modal-body">
<p>Employee Details</p>
</div>
<!-- Popup Footer -->
<div class="modal-footer">

<!-- close button -->
<?php $this->widget('bootstrap.widgets.TbButton', array(
    'label'=>'Close',
    'url'=>'#',
    'htmlOptions'=>array('data-dismiss'=>'modal'),
)); ?>
<!-- close button ends-->
</div>
<?php $this->endWidget(); ?>
<!-- View Popup ends -->



And in Controller 


public function actionView($id)
{
    if( Yii::app()->request->isAjaxRequest )
        {
        $this->renderPartial('view',array(
            'model'=>$this->loadModel($id),
        ), false, true);
    }
    else
    {
        $this->render('view',array(
            'model'=>$this->loadModel($id),
        ));
    }
}





Monday, March 25, 2013

CGridView and show detail in CJuiDialog Yii


in view file, add the following to CButtonClass section of the array:


<?php $this->widget('zii.widgets.grid.CGridView', array(
    .....
    'columns' => array(
        ....
        array(
            'class' => 'CButtonColumn',
            'buttons' => array(
                'view' => array(
                    'url' => 'url' => 'Yii::app()->createUrl("/path/to/controller/view")',
                    'options' => array(
                        'ajax' => array(
                            'type' => 'POST',
                            'url' => "js:$(this).attr('href')",
                            'update' => '#detail-section',
                        ),
                    ),

                ),    // view button
            ),
        ),
    )
)) ?>


<?php $this->beginWidget('zii.widgets.jui.CJuiDialog', array(
        'id' => 'dlg-detail',
        'options' => array(
            'title' => 'Dialog Box Title',
            'closeOnEscape' => true,
            'autoOpen' => false,
            'model' => false,
            'width' => 550,
            'height' => 450,
        ),
)) ?>
<div id="detail-section"></div>
<?php $this->endWidget() ?>




change the view action in controller file:



public function actionView($id) {
    if (Yii::app()->request->isAjaxRequest) {
        $this->renderPartial('_view', array(
            'model' => $this->loadModel($id),
        ), false, true);

        Yii::app()->end();
    }
}












Ajax button with confirm and before send texts


In View

<?php
echo "<div id='resPass'>";
echo CHtml::ajaxButton(
'Reset Password',          // the link body (it will NOT be HTML-encoded.)
array('resetPassword','id'=>$model->user_id), // the URL for the AJAX request. If empty, it is assumed to be the current URL.
array(
'update'=>'#req_res_loading',
'beforeSend' => 'function() {   
     
 $("#resPass").html("please wait......");

     }',

     'complete' => 'function() {
      $("#resPass").hide();
     }', 
),

      array(
   'class'=>'btn btn-info',
   'confirm'=>Yii::t('admin','Are you want to Reset Password ?'),
      )


);
echo "</div>"
?>
<div id="req_res_loading"></div>


In controller 


       public function actionResetPassword($id)
{
        if(Yii::app()->request->isAjaxRequest)
                {
       .................
              Yii::app()->end();
        }
   
}



Sunday, March 24, 2013

Add button append text box with delete


   <div id="content">
      <div>
 
        <input type="text"/>
        <a class="add" href="#">Add</a>
    </div>








$("#content").delegate(".add", "click", function() {
    var content = '<div>' +
                   
                         '<input type="text"/>'+
                         '<a class="del" href="#">Delete</a>&nbsp;' +
                         '<a class="add" href="#">Add</a>'+
                         '</div>';
    $("#content").append(content);
    return false;
});

$("#content").delegate(".del", "click", function() {
    $(this).parent().remove();
    return false;
});







Friday, March 22, 2013

CgridView Two column value concate Yii


$this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'scenarios-grid',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    'columns'=>array(    
   
        array(
            'name'=>'fname',
            'header'=>'Client Name',
            'value'=>'$data->first_name." ".$data->last_name',
        ),
              array(
            'class'=>'CButtonColumn',
                        'template'=>'{view}{delete}'  
        ),
    ),
));