JavaScript   发布时间:2022-04-16  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了javascript – 是否可以重新初始化CKEditor组合框/下拉菜单?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
如何动态更新下拉菜单中的项目?

我有一个CKEditor的自定义插件,它填充了一个下拉菜单,其中包含我可以注入我的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:

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函数的聪明之处在于我们在绑定它时提供“this”,因此每当触发rebuildList时,这都引用了我无法通过其他任何方式获得的richcombo对象.

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

埃尔切

大佬总结

以上是大佬教程为你收集整理的javascript – 是否可以重新初始化CKEditor组合框/下拉菜单?全部内容,希望文章能够帮你解决javascript – 是否可以重新初始化CKEditor组合框/下拉菜单?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。