【问题标题】:How to set data on an already created editor?如何在已创建的编辑器上设置数据?
【发布时间】:2019-10-23 09:51:18
【问题描述】:

我想将数据设置为 3 个。已经创建的编辑器,我无法做到。

我试过了。不。 2 设置正在新创建的编辑器上发生的数据,而不是现有的。

  1. 下面是我创建 3 个编辑器的位置:
create_editor: function () {
            window.editors = {};
            document.querySelectorAll('.editor').forEach((node, index) => {
                ClassicEditor
                    .create(node, {
                        //removePlugins: [ 'Heading', 'Link' ],
                        toolbar: ['bold', 'italic', 'bulletedList', 'numberedList', 'blockQuote', 'Heading'],
                        //placeholder: 'Type the content here!'
                    })
                    .then(editor => {
                        //console.log(editor);
                        window.editors[index] = editor
                    })
                    .catch(error => {
                        //console.error(error);
                    });
            });
        }
  1. 下面是我尝试将我的数据设置为所有 3 个现有编辑器的位置:
set_editor: function (elem, data) {
            ClassicEditor
                .create(document.querySelector('#' + elem))
                .then(editor => {
                    editor.setData(data);
                })
        }

数据存储在本地存储中,我想检索它们并将它们设置为我已经创建的 3 个当前没有发生的编辑器。

【问题讨论】:

    标签: javascript jquery ajax ckeditor ckeditor5


    【解决方案1】:

    您的问题是您需要获取现有 ClassicEditor 的实例才能对其进行编辑。

    正如answer 中提供的那样,您可以将实例存储在全局变量中(就像您已经在做的那样,但不是索引存储 element.id 作为键)

    同样来自您的 cmets,很明显您正面临编辑器创建和编辑器设置之间的竞争条件,因此不要将编辑器保存在 window.editors 保存承诺,这样您可以等待编辑器创建完成后再设置价值。

    create_editor: function () {
        window.editors = {};
        document.querySelectorAll('.editor').forEach(node => {
            window.editors[node.id] =
            ClassicEditor
                .create(node, {
                    //removePlugins: [ 'Heading', 'Link' ],
                    toolbar: ['bold', 'italic', 'bulletedList', 'numberedList', 'blockQuote', 'Heading'],
                    //placeholder: 'Type the content here!'
                })
                .then(editor => {
                    //console.log(editor);
                })
                .catch(error => {
                    //console.error(error);
                });
        });
    }
    
    set_editor: function (elem, data) {
        window.editors[elem].then(editor => editor.setData(data));
    }
    

    【讨论】:

    • 我会尝试并告诉你。
    • 嗨 Avcs,我试过了,你的代码和我的一样。您的代码创建了另一个编辑器,数据显示在那里。我想向我现有的编辑器显示数据。这怎么可能?
    • stackoverflow.com/questions/48575534/… 请关注这个答案,它提供了一种基于元素 id 存储实例和获取的方法
    • 嘿 Avcs,感谢您的链接。我现在可以设置数据,但我可以为最后一个编辑器设置数据,而不是之前的所有其他 2 个编辑器。你能帮我吗?另外,我尝试了您编辑的代码,但我得到“window.editors[elem] 未定义”,尽管 console.log(window.editors) 返回了 3 个编辑器的正确对象。
    • 也许您正面临竞争条件?创建方法是异步的
    猜你喜欢
    • 2017-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-05
    • 2016-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多