在网上找了很多相关的, 但都不太详细, 参考了下,自己终于写出来了.
首先说说我的版本
yii : 1.1.15
ueditor : 1.4.3 utf8
事不宜迟, 马上开始.
首先在官网下载ueditor, 解压后把文件夹移动到yii项目的根目录喇
然后打开ueditor.config.js 修改
var URL = window.UEDITOR_HOME_URL || '/ueditor/';//getUEBasePath();原来的
增加 ActiveForm.php, 用来给yii 生成widget
ActiveForm.php 代码
class ActiveForm extends CActiveForm {
public function ueditor($model, $attribute, $htmlOptions=array()) {
//得到这个插件的name
CHtml::resolveNameID($model, $attribute, $htmlOptions);
$attr = 'name="'.$htmlOptions['name'].'" style="'.$htmlOptions['style'].'"';
$content = $model->content?$model->content:'';
$str = '<script type="text/javascript" src="/ueditor/ueditor.config.js"></script>';
$str .='<script type="text/javascript" src="/ueditor/ueditor.all.min.js"></script>';
$str .= '<script id="editor" type="text/plain" '.$attr.'>'.html_entity_decode($content).'</script>';
$str .= '<script type="text/javascript">var ue = UE.getEditor(\'editor\');</script>';
return $str;
}
}
最后在我们需要使用ueditor的地方去加载这个widget
$form = $this->beginWidget('ActiveForm', array(
'id' => 'user-form',
// Please note: When you enable ajax validation, make sure the corresponding
// controller action is handling ajax validation correctly.
// There is a call to performAjaxValidation() commented in generated controller code.
// See class documentation of CActiveForm for details on this.
'enableAjaxValidation' => false,
));
?>
<fieldset>
<p>
<?php echo $form->labelEx($model, 'title'); ?>
<?php echo $form->textField($model, 'title', array('size' => 30, 'maxlength' => 30,'class' => 'text-input small-input')); ?>
<?php echo $form->error($model,'title'); ?>
</p>
<p>
<?php echo $form->labelEx($model, 'description'); ?>
<?php echo $form->textField($model, 'description', array('size' => 30, 'maxlength' => 30,'class' => 'text-input small-input')); ?>
</p>
<p>
<?php echo $form->labelEx($tmodel, 'content'); ?>
<?php echo $form->ueditor($tmodel,'content',array('style'=>'width:850px;height:400px;'));//style是限定文本框的大小?>
</p>
<p>
<?php echo CHtml::submitButton($model->isNewRecord ? '新建' : '保存',array('class'=>'button')); ?>
</p>
</fieldset>
<div class="clear"></div>
<?php $this->endWidget(); ?>
我刚学YII, 这里只是个demo 做得比较简单, 可能实现的方法比较笨拙, 就在这里抛砖下吧, 大家有更好的方法也可以告诉我. 谢谢~