sysprog
(Sysprog)
1
Hello all.
I have the ajax request on my page -
CHtml::link(‘Add division’, ‘#’, array(‘onclick’ => ‘$.ajax( { url: "’.Yii::app()->baseUrl.’/index.php/model/controller/view/", cache: false, type: “POST”, success: function (data) { $("#divisionEdit").html(data); } }); $("#mydialog").dialog(“open”); return false; '));
I want to register some script in the ajax response like above
Yii::app()->getClientScript()->registerScript("InitDivisionTree", $scriptBody, CClientScript::POS_LOAD);
Is there any way to fix this?
Regards
Oleg
joblo
(Joe)
2
‘registerScript’ will not work in an ajax response, because it’s only done when a full page is rendered.
Use echo CHtml::scriptFile($url) or
echo CHtml::script(…) instead.
You can use renderPartial in the ajax response too.
Then you can use ‘registerScript’ if ‘$processOutput=true’ when calling the renderPartial method.
sysprog
(Sysprog)
3
Thank you for your response
The problem was I could not register the client scripts of the TreeView
And the TreeView initialization was interrupted with the
In the TreeView initialization we have
public function init()
{
if(isset($this->htmlOptions['id']))
$id=$this->htmlOptions['id'];
else
$id=$this->htmlOptions['id']=$this->getId();
if($this->url!==null)
$this->url=CHtml::normalizeUrl($this->url);
$cs=Yii::app()->getClientScript();
$cs->registerCoreScript('treeview');
$options=$this->getClientOptions();
$options=$options===array()?'{}' : CJavaScript::encode($options);
$cs->registerScript('Yii.CTreeView#'.$id,"jQuery(\"#{$id}\").treeview($options);");
if($this->cssFile===null)
$cs->registerCssFile($cs->getCoreScriptUrl().'/treeview/jquery.treeview.css');
else if($this->cssFile!==false)
$cs->registerCssFile($this->cssFile);
echo CHtml::tag('ul',$this->htmlOptions,false,false)."\n";
echo self::saveDataAsHtml($this->data);
}
So I just have registered manually the scripts in the main view
Yii::app()->getClientScript()->registerCoreScript('treeview');
And code start work fine
What if to make such changes?
public function init()
{
if(isset($this->htmlOptions['id']))
$id=$this->htmlOptions['id'];
else
$id=$this->htmlOptions['id']=$this->getId();
if($this->url!==null)
$this->url=CHtml::normalizeUrl($this->url);
if(!Yii::getApp()->getRequest()->isAjaxRequest)
{
$cs=Yii::app()->getClientScript();
$cs->registerCoreScript('treeview');
}
else
{
echo "<script src='jquery.treeview.js'></script>";
}
$options=$this->getClientOptions();
$options=$options===array()?'{}' : CJavaScript::encode($options);
$cs->registerScript('Yii.CTreeView#'.$id,"jQuery(\"#{$id}\").treeview($options);");
if($this->cssFile===null)
$cs->registerCssFile($cs->getCoreScriptUrl().'/treeview/jquery.treeview.css');
else if($this->cssFile!==false)
$cs->registerCssFile($this->cssFile);
echo CHtml::tag('ul',$this->htmlOptions,false,false)."\n";
echo self::saveDataAsHtml($this->data);
}
sysprog
(Sysprog)
4