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

前端之家收集整理的这篇文章主要介绍了javascript – 是否可以重新初始化CKEditor组合框/下拉菜单?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何动态更新下拉菜单中的项目?

我有一个CKEditor的自定义插件,它填充了一个下拉菜单,其中包含我可以注入我的textarea的项目列表.

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

  1. var maptags = []

当您首次通过init:函数单击它时,此标记列表将添加到下拉列表中.我的问题是,如果客户端更改页面上的内容,该数组中的项目会发生变化,如何将该列表重新加载到更新的数组?

这是我的CKEditor插件代码

  1. CKEDITOR.plugins.add('mapitems',{
  2. requires: ['richcombo'],//,'styles' ],init: function (editor) {
  3. var config = editor.config,lang = editor.lang.format;
  4.  
  5. editor.ui.addRichCombo('mapitems',{
  6. label: "Map Items",title: "Map Items",voiceLabel: "Map Items",className: 'cke_format',multiSelect: false,panel:
  7. {
  8. css: [config.contentsCss,CKEDITOR.getUrl(editor.skinPath + 'editor.css')],voiceLabel: lang.panelVoiceLabel
  9. },init: function () {
  10. this.startGroup("Map Items");
  11. //this.add('value','drop_text','drop_label');
  12. for (var this_tag in maptags) {
  13. this.add(maptags[this_tag][0],maptags[this_tag][1],maptags[this_tag][2]);
  14. }
  15. },onClick: function (value) {
  16. editor.focus();
  17. editor.fire('saveSnapshot');
  18. editor.insertHtml(value);
  19. editor.fire('saveSnapshot');
  20. }
  21. });
  22. }
  23. });

解决方法

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

像这样更改你的init:

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

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

  1. var buildListHasRunOnce = 0;
  2. var buildList = function () {
  3. if (buildListHasRunOnce) {
  4. // Remove the old unordered list from the dom.
  5. // This is just to cleanup the old list within the iframe
  6. $(this._.panel._.iframe.$).contents().find("ul").remove();
  7. // reset list
  8. this._.items = {};
  9. this._.list._.items = {};
  10. }
  11. for (var i in yourListOfItems) {
  12. var item = yourListOfItems[i];
  13. // do your add calls
  14. this.add(item.id,'something here as html',item.text);
  15. }
  16. if (buildListHasRunOnce) {
  17. // Force CKEditor to commit the html it generates through this.add
  18. this._.committed = 0; // We have to set to false in order to trigger a complete commit()
  19. this.commit();
  20. }
  21. buildListHasRunOnce = 1;
  22. };

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

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

埃尔切

猜你在找的JavaScript相关文章