Wednesday, February 22, 2012

Make checkbox using ajax load in yiiframework

Sometimes you have to implement function for loading ajax content from another link to your page using checkbox. In yii there is also CHtml::activeCheckbox functions that can be used to fulfill this. But if you want to enable this checkbox to allow you loading ajax content you have to use a custom code because if you use ajax property in htmlOptions the checkbox can not be selected because in the end of generated code it has return false; 


So to custumize this I have one way for solving this problem. The way is just to create your own script to make this conditions just see this example below..


<?php echo CHtml::checkBox("load_ajax",false,array('id'=>'load_risk_all'));echo "Load All Risk Child";
$url=CController::createUrl('project/vieweach',array('project_id'=>$model->project_id));
Yii::app()->clientScript->registerScript("check",
           '$("#load_risk_all").change(function(){
             if($(this).is(":checked")){
              $("#load_risk").load("'.$url.'");
              $("#load_risk").fadeIn();
             }else{
              $("#load_risk").html("");
             }
             })
          ',
           CClientScript::POS_READY);

?>
<div id="load_risk">
</div>

And then you have to make sure for using partialRender for the content in your controller. You can also to customize your code of load() function in jQuery for loading the ajax content using $.ajax functions with the data you want.

$this->renderPartial('/risk/view_each',array('provider'=>$provider),false,true);


You can also use onChange event in checkbox htmlOption for supporting your own javascript model like this


<?php echo CHtml::checkbox("load risk",false,array("onChange"=>"js:yourcustomfunction()")); ?> //don't forget to register your own function in script tag
Then nice use this..

No comments:

Post a Comment