javascript - 是否可以重新初始化 CKEditor 组合框/下拉菜单?

标签 javascript plugins ckeditor

如何动态更新下拉列表中的项目?

我有一个用于 CKEditor 的自定义插件,它使用一个项目列表填充一个下拉菜单,我可以将这些项目注入(inject)到我的 textarea 中。

这个项目列表来自一个名为 maptags 的 Javascript 数组,它会为每个页面动态更新。

var maptags = []

当您第一次通过 init: 函数点击它时,这个标签列表会被添加到下拉列表中。我的问题是,如果该数组中的项目随着客户端更改页面上的内容而发生变化,我该如何将该列表重新加载到更新后的数组?

这是我的 CKEditor 插件代码:

CKEDITOR.plugins.add('mapitems', {
    requires: ['richcombo'], //, 'styles' ],
    init: function (editor) {
        var config = editor.config,
        lang = editor.lang.format;       

        editor.ui.addRichCombo('mapitems',
        {
            label: "Map Items",
            title: "Map Items",
            voiceLabel: "Map Items",
            className: 'cke_format',
            multiSelect: false,

            panel:
            {
                css: [config.contentsCss, CKEDITOR.getUrl(editor.skinPath + 'editor.css')],
                voiceLabel: lang.panelVoiceLabel
            },

            init: function () {
                this.startGroup("Map Items");
                //this.add('value', 'drop_text', 'drop_label');
                for (var this_tag in maptags) {
                    this.add(maptags[this_tag][0], maptags[this_tag][1], maptags[this_tag][2]);
                }
            },

            onClick: function (value) {
                editor.focus();
                editor.fire('saveSnapshot');
                editor.insertHtml(value);
                editor.fire('saveSnapshot');
            }
        });
    } 
});

最佳答案

我想我刚刚解决了这个问题。

像这样改变你的初始化:

init: function () {
                var rebuildList = CKEDITOR.tools.bind(buildList, this);
                rebuildList();
                $(editor).bind('rebuildList', rebuildList);
            },

并在该范围之外定义 buildList 函数。

var buildListHasRunOnce = 0;
        var buildList = function () {
            if (buildListHasRunOnce) {
                // Remove the old unordered list from the dom.
                // This is just to cleanup the old list within the iframe
                $(this._.panel._.iframe.$).contents().find("ul").remove();
                // reset list
                this._.items = {};
                this._.list._.items = {};
            }
            for (var i in yourListOfItems) {
                var item = yourListOfItems[i];
                // do your add calls
                this.add(item.id, 'something here as html', item.text);
            }
            if (buildListHasRunOnce) {
                // Force CKEditor to commit the html it generates through this.add
                this._.committed = 0; // We have to set to false in order to trigger a complete commit()
                this.commit();
            }
            buildListHasRunOnce = 1;
        };

CKEDITOR.tools.bind 函数的巧妙之处在于我们在绑定(bind)它时提供“this”,因此每当触发 rebuildList 时,this 指的是 richcombo 对象本身,我无法通过任何其他方式获得它.

希望这对我有帮助,对我来说效果很好!

艾尔切

关于javascript - 是否可以重新初始化 CKEditor 组合框/下拉菜单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7762810/

相关文章:

javascript - d3 Selection.remove() 不删除

Javascript 按名称更改字段值

javascript - HTML 中插入了意外的空白元素

javascript - 开发一个简单的Firefox插件容易吗(JS注入(inject))

javascript - 段落上的 ckeditor 类未被识别

javascript - 传递参数查看

plugins - 如何发布具有多个程序集的 Dynamics CRM4 插件?

plugins - 在 Flutter 中添加蓝牙插件

vue.js - VueJS CKeditor5上传图片

javascript - CKEditor 占位符图标未显示在工具栏中