CKeditor populate dialog select with Ajax

随声附和 提交于 2019-12-10 11:07:13

问题


I am trying to populate my CKeditor dialog selectbox with ajax. The following is from my plugin.js file:

...
{
    type : 'select',
    id : 'style',
    label : 'Style',
    setup : CKEDITOR.ajax.post(  '.../ckeditor/plugins/simpleLink/ajax.php', JSON.stringify( { foo: 'bar' } ), 'application/json', function( data ) { 
            console.log( data);
    }),
    items : [ ['--- Select something ---', 0] ],
    commit : function( data )
    {
        data.style = this.getValue();
    }
}
...

The ajax output looks like this:

["Basketball","basketball"],["Baseball","baseball"],["Hockey","hockey"]

I am really wondering how to get the output INTO the "items". From my point of view I tried everything. Can someone help me?


回答1:


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);
            },
        });
    },
},



回答2:


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();
    }
}


来源:https://stackoverflow.com/questions/24633504/ckeditor-populate-dialog-select-with-ajax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!