最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Use CKeditor instance in Vue.js - Stack Overflow

programmeradmin5浏览0评论

I am trying to implement CKeditor in my Laravel-backoffice which build its views with Vue.js

In this form I want to replace the "textarea" with name="ckeditor1" with a texteditor

    <form method="POST" v-on="submit: onSubmitForm">
    <div class="col-md-4">

        <h1>Pagina: @{{ page.name }}</h1>

        <h2>Pagina algemeen</h2>

        <div class="form-group">
            <label for="name">
                Name
                <span class="error" v-if="! page.name">*</span>
            </label>
            <input type="text" name="name" id="name" class="form-control" v-model="page.name">
        </div>

        <ul class="nav nav-tabs">
            <li class="" v-repeat="page.translations" v-class="active: language == defaultLanguage"><a
                        data-toggle="tab" href="#@{{ language  }}">@{{ language  }}</a></li>
        </ul>
        <div class="tab-content">
            <div v-repeat="page.translations" id="@{{ language  }}" class="tab-pane fade in "
                 v-class="active: language == defaultLanguage">
                <h2>Pagina inhoud</h2>

                <div class="form-group">
                    <label for="name">
                        Titel
                    </label>
                    <input type="text" name="title_@{{ language  }}" id="title_@{{ language  }}"
                           class="form-control" v-model="title">
                </div>
                <div class="form-group">
                    <label for="content">
                        Inhoud
                    </label>
                    <textarea name="ckeditor1" id="content_@{{ language  }}"
                              class="form-control editor" v-model="content"></textarea>
                </div>

                <h2>Seo</h2>

                <div class="form-group">
                    <label for="meta_keywords">
                        Meta keywords
                    </label>
                    <input type="text" name="meta_keywords_@{{ language  }}"
                           id="meta_keywords_@{{ language  }}" class="form-control"
                           v-model="meta_keywords">
                </div>
                <div class="form-group">
                    <label for="meta_decription">
                        Meta description
                    </label>
                    <textarea name="meta_description_@{{ language  }}"
                              id="meta_description_@{{ language  }}" class="form-control"
                              v-model="meta_description"></textarea>
                </div>
                <input type="hidden" name="page_id_@{{ language  }}" id="page_id_@{{ language  }}"
                       class="form-control" v-model="page_id" value="@{{ pageId }}">
            </div>
        </div>

        <div class="form-group" v-if="! submitted">
            <button type="submit" class="btn btn-default">
                Opslaan
            </button>
        </div>
    </div>
</form>

The @{{ }} fields are loaded and filled with json call and vue.js but there is no problem cause all fields are filled perfectly as needed. The problem is just the initializing of my editor.

This is where I get my data:

Vue.http.headersmon['X-CSRF-TOKEN'] = document.querySelector('#token').getAttribute('value');

var pages = new Vue({
    el: '#page',
    data: {
        pageId: document.querySelector('#page-id').getAttribute('value'),
        pageTitle: 'Pagina',
        page: [],
        submitted: false,
        defaultLanguage: 'nl',
        errors: false
    },

    ready: function() {
        this.fetch();
    },

    methods: {
        fetch: function() {
            this.$http.get('/api/pages/' + this.pageId, function(response) {

                this.page = response;
            });
        },
        onSubmitForm: function(e) {
            e.preventDefault();
            this.submitted = true;
            this.errors = false;

            if(this.pageId == 0) {
                this.$http.post('/api/pages/', this.page, function (response) {
                    if (response.errors.length) {
                        this.errors = response.errors;
                        this.submitted = false;

                        return;
                    }//endif

                    this.submitted = false;
                    window.location.href = '/admin/pages';
                });
            }
            else
            {
                this.$http.put('/api/pages/' + this.pageId, this.page, function (response) {

                    if (response.errors.length) {
                        this.errors = response.errors;
                        this.submitted = false;

                        return;
                    }//endif

                    this.submitted = false;
                    window.location.href = '/admin/pages';
                });
            }
        }

    }
});

UPDATE -> SOLVED

By adding Vue.nextTick I can initialize an editor. I added a class 'editor' to every textarea I want it to be an editor and then find all id's from the textareas with class="editor".

fetch: function() {
    this.$http.get('/api/pages/' + this.pageId, function(response) {

        this.page = response;

        Vue.nextTick(function () {
            $('textarea.editor').each(function(){
                CKEDITOR.replace(this.id);
            });
        });
    });
},

I am trying to implement CKeditor in my Laravel-backoffice which build its views with Vue.js

In this form I want to replace the "textarea" with name="ckeditor1" with a texteditor

    <form method="POST" v-on="submit: onSubmitForm">
    <div class="col-md-4">

        <h1>Pagina: @{{ page.name }}</h1>

        <h2>Pagina algemeen</h2>

        <div class="form-group">
            <label for="name">
                Name
                <span class="error" v-if="! page.name">*</span>
            </label>
            <input type="text" name="name" id="name" class="form-control" v-model="page.name">
        </div>

        <ul class="nav nav-tabs">
            <li class="" v-repeat="page.translations" v-class="active: language == defaultLanguage"><a
                        data-toggle="tab" href="#@{{ language  }}">@{{ language  }}</a></li>
        </ul>
        <div class="tab-content">
            <div v-repeat="page.translations" id="@{{ language  }}" class="tab-pane fade in "
                 v-class="active: language == defaultLanguage">
                <h2>Pagina inhoud</h2>

                <div class="form-group">
                    <label for="name">
                        Titel
                    </label>
                    <input type="text" name="title_@{{ language  }}" id="title_@{{ language  }}"
                           class="form-control" v-model="title">
                </div>
                <div class="form-group">
                    <label for="content">
                        Inhoud
                    </label>
                    <textarea name="ckeditor1" id="content_@{{ language  }}"
                              class="form-control editor" v-model="content"></textarea>
                </div>

                <h2>Seo</h2>

                <div class="form-group">
                    <label for="meta_keywords">
                        Meta keywords
                    </label>
                    <input type="text" name="meta_keywords_@{{ language  }}"
                           id="meta_keywords_@{{ language  }}" class="form-control"
                           v-model="meta_keywords">
                </div>
                <div class="form-group">
                    <label for="meta_decription">
                        Meta description
                    </label>
                    <textarea name="meta_description_@{{ language  }}"
                              id="meta_description_@{{ language  }}" class="form-control"
                              v-model="meta_description"></textarea>
                </div>
                <input type="hidden" name="page_id_@{{ language  }}" id="page_id_@{{ language  }}"
                       class="form-control" v-model="page_id" value="@{{ pageId }}">
            </div>
        </div>

        <div class="form-group" v-if="! submitted">
            <button type="submit" class="btn btn-default">
                Opslaan
            </button>
        </div>
    </div>
</form>

The @{{ }} fields are loaded and filled with json call and vue.js but there is no problem cause all fields are filled perfectly as needed. The problem is just the initializing of my editor.

This is where I get my data:

Vue.http.headers.mon['X-CSRF-TOKEN'] = document.querySelector('#token').getAttribute('value');

var pages = new Vue({
    el: '#page',
    data: {
        pageId: document.querySelector('#page-id').getAttribute('value'),
        pageTitle: 'Pagina',
        page: [],
        submitted: false,
        defaultLanguage: 'nl',
        errors: false
    },

    ready: function() {
        this.fetch();
    },

    methods: {
        fetch: function() {
            this.$http.get('/api/pages/' + this.pageId, function(response) {

                this.page = response;
            });
        },
        onSubmitForm: function(e) {
            e.preventDefault();
            this.submitted = true;
            this.errors = false;

            if(this.pageId == 0) {
                this.$http.post('/api/pages/', this.page, function (response) {
                    if (response.errors.length) {
                        this.errors = response.errors;
                        this.submitted = false;

                        return;
                    }//endif

                    this.submitted = false;
                    window.location.href = '/admin/pages';
                });
            }
            else
            {
                this.$http.put('/api/pages/' + this.pageId, this.page, function (response) {

                    if (response.errors.length) {
                        this.errors = response.errors;
                        this.submitted = false;

                        return;
                    }//endif

                    this.submitted = false;
                    window.location.href = '/admin/pages';
                });
            }
        }

    }
});

UPDATE -> SOLVED

By adding Vue.nextTick I can initialize an editor. I added a class 'editor' to every textarea I want it to be an editor and then find all id's from the textareas with class="editor".

fetch: function() {
    this.$http.get('/api/pages/' + this.pageId, function(response) {

        this.page = response;

        Vue.nextTick(function () {
            $('textarea.editor').each(function(){
                CKEDITOR.replace(this.id);
            });
        });
    });
},
Share Improve this question edited Jun 30, 2015 at 12:22 bflydesign asked Jun 28, 2015 at 20:30 bflydesignbflydesign 4838 silver badges22 bronze badges 2
  • What is exactly is going wrong? Does it not start up the editor? Is there an error in the Javascript console? – David K. Hess Commented Jun 30, 2015 at 0:29
  • @DavidK.Hess I don't get any errors and the editor is not showing when my <textarea> is located in <div v-repeat="page.translations"> section. As soon as I place a <textarea> outside that section, then it's working perfectly. – bflydesign Commented Jun 30, 2015 at 6:13
Add a ment  | 

2 Answers 2

Reset to default 4

By adding Vue.nextTick I can initialize an editor. I added a class 'editor' to every textarea I want it to be an editor and then find all id's from the textareas with class="editor".

fetch: function() {
    this.$http.get('/api/pages/' + this.pageId, function(response) {

        this.page = response;

        Vue.nextTick(function () {
            $('textarea.editor').each(function(){
                CKEDITOR.replace(this.id);
            });
        });
    });
}

I am also using CKeditor with laravel-vue. You just need to set and get data with CKeditor for basic thing.

This is my main.html file in which i need CKeditor.

<div class="row">
    <div class="col-md-2">
         <label for="body" >Mail Body :</label>
    </div>

   <div class="col-md-10" >
       <textarea class="ckeditor" id="body" rows="5" cols="70" name="body" v-model="template.body" ></textarea>
   </div>
</div>

After that i initialize my CKeditor value in app.js file

var vm = this;
axios.post('/fetchEmailTemplate', {
                'template_id' : template_id
                }).then(function (response) {

                    vm.template = response.data.emailTemplate;

                CKEDITOR.instances['body'].setData(vm.template.body);

                }).catch(function (error) {
                    console.log(error);
                    setNotification(error, "danger");
                });

If I'm not mistaken ckeditor replaces the original textarea by a custom template. So what you see within ckeditor will not be put into your messageArea textarea automatically. That's why you don't see any changes to your model.
So, for making changes you need to replace updated text before submit in app.js file like below.

this.template.body = CKEDITOR.instances['body'].getData();
发布评论

评论列表(0)

  1. 暂无评论