CKeditor populate dialog select with Ajax

时光怂恿深爱的人放手 提交于 2019-12-06 14:27:33

CKEditor select widgets have an 'add' function you can call to populate them. You can call it from an 'onLoad' function, if you add one:

{
    type: 'select',
    id: 'myselect',
    label: 'The select will be empty until it is populated',
    items: [ ],
    onLoad: function(api) {
        widget = this;
        $.ajax({
            type: 'GET',
            url: 'path/to/your/json',
            dataType: 'json',
            success: function(data, textStatus, jqXHR) {
                for (var i = 0; i < data.length; i++) {
                    widget.add(data[i]['label'], data[i]['value']);
                }
            },
            error: function(jqXHR, textStatus, errorThrown) {
                console.log('ajax error ' + textStatus + ' ' + errorThrown);
            },
        });
    },
},

Found a workaround. For anyone having the same problem - here is my way of solving it:

plugin.js:

This code before "CKEDITOR.plugins.add( 'PLUGINNAME', {..."

jQuery.extend({
getValues: function(url) {
    var result = null;
    $.ajax({
        url: url,
        type: 'get',
        dataType: 'json',
        async: false,
        success: function(data) {
            result = data;
        }
    });
   return result;
}
});
var results = $.getValues('.../ckeditor/plugins/PLUGINNAME/ajax.php');

This is the code for the select box

{
    type : 'select',
    id : 'style',
    label : 'Style',
    setup : '',
    items : results,
    commit : function( data )
    {
        data.style = this.getValue();
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!