Gagan
Gagan

Reputation: 5656

trumbowyg editor with vuejs

I am using the trumbowyg editor control in my vuejs spa. From the documentation I know that I can use the following code to set the contents of the editor.

$('#editor').trumbowyg('html','<p>Your content here</p>'); 
$('#editor').trigger('tbwchange');

However, it is not working for me in my VueJs App. I have an object that has a description key defined. I can console.log the description , but when I try to assign it to the editor control as mentioned above, it fails . I can see no error in the console but the text just won't show up in the editor.

Here is what I am going at the moment.

<template>
    <div class="modal fade"  data-backdrop="static" data-keyboard="false">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title">
                        <span v-if="anouncement">Edit Anouncement</span>
                        <span v-else>New Anouncement</span>
                    </h4>
                </div>
                <div class="modal-body">
                    <div class="form-group">
                        <input type="text" placeholder="enter anouncement summary here" class="form-control" v-model="anouncementObj.summary">
                    </div>
                    <div class="form-group">
                        <input type="text" placeholder="enter location here" class="form-control" v-model="anouncementObj.location">
                    </div>
                    <textarea class="note-view__body" id="anonDescription" v-model="description" placeholder="enter event description"></textarea>
                </div>
                <div class="modal-footer">
                    <button type="button" v-on:click="clear()" class="btn btn-link" data-dismiss="modal">Close</button>
                    <button type="button" v-on:click="performSave()" class="btn btn-link">Save</button>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    export default {
        props : {
            anouncement : Object
        },
        data() {
            return {
                anouncementObj :{}
            }
        },
        mounted () {
            this.makeTextBoxReady();
            this.anouncementObj = Object.assign({},this.anouncementObj, this.anouncement || {});
                $('#anonDescription').trumbowyg('html',this.anouncement.description); 
                $('#anonDescription').trigger('tbwchange');
                console.log(this.anouncement.description);
        },
        methods : {
            makeTextBoxReady: function() {
                $(document).ready(function() {
                    if (!$('html').is('.ie9')) {
                        if ($('.note-view__body')[0]) {
                            $('.note-view__body').trumbowyg({
                                autogrow: true,
                                btns: [
                                    'btnGrp-semantic', ['formatting'],
                                    'btnGrp-justify',
                                    'btnGrp-lists', ['removeformat']
                                ]
                            });
                        }
                    }
                });
            },
            performSave : function() {
                let description = $('#anonDescription').trumbowyg('html');

                let formData = new FormData();
                for (name in this.anouncementObj) {
                    formData.append(name, this.anouncementObj[name]);
                }

                if( !this.anouncementObj.id) {
                    this.anouncementObj.id = 0;
                }

                formData.append('description',description);

                this.$http.post('/admin/anouncement/createOrUpdate', formData).then(response => {
                    // console.log(response);
                    if(response.data.status==200) {
                        alert(response.data.message);
                        this.$emit('getAnouncements');
                    }
                })
            },
            clear: function() {
                this.anouncementObj= {};
            }

        }
    }
</script>

Can you please let me know what I am doing wrong here? I have also tried the nexttick approach but even that is not working.

Upvotes: 0

Views: 1139

Answers (1)

Gagan
Gagan

Reputation: 5656

I got it working. I was not using the correct bootstrap modal id. Please see this related question for more information.

This is the correct code.

if(this.anouncementObj && this.anouncementObj.description && this.anouncementObj.id) {
   $('#'+this.anouncementObj.id+' #anonDescription').trumbowyg('html',this.anouncementObj.description); 
   $('#'+this.anouncementObj.id+' #anonDescription').trigger('tbwchange');
}

Upvotes: 1

Related Questions