美文网首页
Yii2中使用KindEditor

Yii2中使用KindEditor

作者: ahcj_11 | 来源:发表于2017-08-17 16:07 被阅读296次

    标签(空格分隔): Yii2


    1 安装

    $ [ahcj@localhost basic9] composer require pjkui/kindeditor "dev-master" -vvv
    

    2 使用方法

    1.1 修改控制器,增加上传方法

    新建 backend/web/upload,然后修改 backend\controllers\NewsController,增加以下代码.

    public function actions(){
        return [
            'Kupload' => [
                'class' => 'pjkui\kindeditor\KindEditorAction',
            ]
        ];
    }
    

    1.2 修改视图

     <?php
    
    use yii\helpers\Html;
    use yii\widgets\ActiveForm;
    use pjkui\kindeditor\KindEditor;
    
    /* @var $this yii\web\View */
    /* @var $model common\models\News */
    /* @var $form yii\widgets\ActiveForm */
    ?>
    
    <div class="news-form">
    
        <?php $form = ActiveForm::begin(); ?>
    
        <?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?>
     
        
        <?= $form->field($model, 'content')->widget( KindEditor::className(),['clientOptions'=>['allowFileManager'=>'true','allowUpload'=>'true']]) ?>
    <!--
        <?= $form->field($model, 'created_at')->textInput() ?>
    
        <?= $form->field($model, 'updated_at')->textInput() ?>
    -->
        <div class="form-group">
            <?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
        </div>
    
        <?php ActiveForm::end(); ?>
    
    </div>
    
    
    

    3 配置

    编辑器相关配置,请在view
    中配置,参数为clientOptions,比如定制菜单,编辑器大小等等,具体参数请查看KindEditor官网文档。

    3.1 配置为富文本编辑器,默认配置

    <?= $form->field($model, 'content')->widget('pjkui\kindeditor\Kindeditor',['clientOptions'=>['allowFileManager'=>'true','allowUpload'=>'true']]) ?>
    

    3.2 配置kindeditor为上传文件按钮,可以自动上传文件到服务器

    <?= $form->field($model, 'article_pic')->widget('pjkui\kindeditor\Kindeditor',['clientOptions'=>['allowFileManager'=>'true','allowUpload'=>'true'],'editorType'=>'uploadButton]) ?>
    

    3.3 配置kindeditor为取色器

    <?= $form->field($model, 'content')->widget('pjkui\kindeditor\Kindeditor','editorType'=>'colorpicker']) ?>
    

    3.4 配置kindeditor为文件管理器,可以查看和选着其上传的文件。

    <?= $form->field($model, 'article_pic')->widget('pjkui\kindeditor\Kindeditor',['clientOptions'=>['allowFileManager'=>'true','allowUpload'=>'true'],'editorType'=>'file-manager']) ?>
    

    3.5 配置kindeditor为图片上传对话框。

    usage:

    <?= $form->field($model, 'article_pic')->widget('pjkui\kindeditor\Kindeditor',['clientOptions'=>['allowFileManager'=>'true','allowUpload'=>'true'],'editorType'=>'image-dialog']) ?>
    

    3.6 配置kindeditor为文件上传对话框。

    usage:

    <?= $form->field($model, 'article_pic')->widget('pjkui\kindeditor\Kindeditor',['clientOptions'=>['allowFileManager'=>'true','allowUpload'=>'true'],'editorType'=>'file-dialog']) ?>
    

    3.7 Demo

    use \pjkui\kindeditor\KindEditor;
    echo KindEditor::widget([
        'clientOptions' => [
            'height' => '500',
            //定制菜单
            'items' => [
            'source', '|', 'undo', 'redo', '|', 'preview', 'print', 'template', 'code', 'cut', 'copy', 'paste',
            'plainpaste', 'wordpaste', '|', 'justifyleft', 'justifycenter', 'justifyright',
            'justifyfull', 'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent', 'subscript',
            'superscript', 'clearhtml', 'quickformat', 'selectall', '|', 'fullscreen', '/',
            'formatblock', 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold',
            'italic', 'underline', 'strikethrough', 'lineheight', 'removeformat', '|', 'image', 'multiimage',
            'flash', 'media', 'insertfile', 'table', 'hr', 'emoticons', 'baidumap', 'pagebreak',
            'anchor', 'link', 'unlink', '|', 'about'
            ],
    ]);
    

    相关文章

      网友评论

          本文标题:Yii2中使用KindEditor

          本文链接:https://www.haomeiwen.com/subject/ylhnrxtx.html