Is it possible to reinitialize a CKEditor Combobox/Drop Down Menu?

前端 未结 2 947
孤城傲影
孤城傲影 2021-02-06 05:42

How do I dynamically update the items in a drop down?

I have a custom plugin for CKEditor that populates a drop down menu with a list of items which I can inject into my

2条回答
  •  再見小時候
    2021-02-06 06:11

    I could not find any helpful documenatation around richcombo, i took a look to the source code and got an idea of the events i needed.

    @El Che solution helped me to get through this issue but i had another approach to the problem because i had a more complex combobox structure (search,groups)

                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 = {};
                    });
    

    NOTE All above code is inside addRichCombo init callback

    • I remove combobox content on "panelHide" event
    • I repopulate combobox on "panelShow" event

    Hope this helps

提交回复
热议问题