Yii2 百度编辑器小部件

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值