Yii2 百度编辑器小部件Ueditor.php
<?php
namespace common\components;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\widgets\InputWidget;
/*
* 创建百度编辑器插件
* 2015年11月22日12:41:25
* 滴水湖 宜浩佳园 111弄 55号
*/
class Ueditor extends InputWidget
{
public $attributes;
public function init()
{
parent::init();
}
public function run()
{
$view = $this->getView();
$this->attributes['id']=$this->options['id'];
if($this->hasModel()){
$input=Html::activeTextarea($this->model, $this->attribute,$this->attributes);
}else{
$input=Html::textarea($this->name,'',$this->attributes);
}
echo $input;
UeditorAsset::register($view);//将Ueditor用到的脚本资源输出到视图
$js='var ue = UE.getEditor("'.$this->options['id'].'",'.$this->getOptions().');';//Ueditor初始化脚本
$view->registerJs($js, $view::POS_END);//将Ueditor初始化脚本也响应到视图中
}
public function getOptions()
{
unset($this->options['id']);//Ueditor识别不了id属性,故而删之
return Json::encode($this->options);
}
}
?>
UeditorAsset.php
<?php
namespace common\components;
use yii\web\AssetBundle;
/*
* 创建百度编辑器插件
* 2015年11月22日12:41:25
* 滴水湖 宜浩佳园 111弄 55号
*/
class UeditorAsset extends AssetBundle
{
public $js = [
'ueditor.config.js',
'ueditor.all.js',
];
public $css = [
];
public function init()
{
$this->sourcePath =$_SERVER['DOCUMENT_ROOT'].\Yii::getAlias('@web').'/staic/ue'; //设置资源所处的目录
}
}
引用 部分 主要命名空间的我这里是
use common\components\Ueditor;
<?= $form->field($model, 'content')->widget(Ueditor::className(),[
'options'=>[
'focus'=>true,
'toolbars'=> [
[
'source', //源代码
'anchor', //锚点
'undo', //撤销
'redo', //重做
'bold', //加粗
//.......................省略
]
],
],
'attributes'=>[
'style'=>'height:80px'
]
]) ?>
0