【问题标题】:Is it possible to reinitialize a CKEditor Combobox/Drop Down Menu?是否可以重新初始化 CKEditor 组合框/下拉菜单?
【发布时间】:2011-10-14 03:43:21
【问题描述】:

如何动态更新下拉菜单中的项目?

我有一个用于 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');
            }
        });
    } 
});

【问题讨论】:

    标签: javascript plugins ckeditor


    【解决方案1】:

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

    像这样改变你的初始化:

    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 时,this 指的是richcombo 对象本身,我无法通过其他任何方式获得.

    希望这对我有帮助!

    埃尔切

    【讨论】:

    • 此解决方案适用于静态数据,但是当我尝试将上述解决方案与从服务器获取的数据一起使用时,它显示为空白。你能帮忙吗?
    • 从服务器获取您的项目,例如从 Ajax 调用并将它们放在“yourListOfItems”数组中
    • 这有点乱,但它让我走上了正轨。谢谢! p.s. richCombo 对象的结构很奇怪而且很糟糕,而且这个功能真的应该是richCombo 的一部分,而不是必须从外部破解它。
    【解决方案2】:

    我在richcombo 周围找不到任何有用的文档,我查看了源代码并了解了我需要的事件。

    @El Che 解决方案帮助我解决了这个问题,但我有另一种方法来解决这个问题,因为我有一个更复杂的组合框结构(搜索、组)

                var _this = this;
                    populateCombo.call(_this, data);
    
                    function populateCombo(data) {
                        /* I have a search workaround added here */
    
                        this.startGroup('Default'); /* create default group */
    
                        /* add items with your logic */
                        for (var i = 0; i < data.length; i++) {
                            var dataitem = data[i];
                            this.add(dataitem.name, dataitem.description, dataitem.name);
                        }
    
                        /* other groups .... */
                    }
    
                    var buildListHasRunOnce = 0;
                    /* triggered when combo is shown */
                    editor.on("panelShow", function(){
                        if (buildListHasRunOnce) {
                            // reset list
                            populateCombo.call(_this, data);
                        }
                        buildListHasRunOnce = 1;
                    });
    
                    /* triggered when combo is hidden */
                    editor.on("panelHide", function(){
                        $(_this._.list.element.$).empty();
                        _this._.items = {};
                        _this._.list._.items = {};
                    });
    

    注意 以上所有代码都在 addRichCombo 初始化回调中

    • 我删除了“panelHide”事件中的组合框内容
    • 我在“panelShow”事件中重新填充组合框

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-09
      • 1970-01-01
      相关资源
      最近更新 更多