简体   繁体   English

如何使用Ajax在CKEDITOR中动态加载模板?

[英]How to load a template dynamically in CKEDITOR by using ajax?

I have a database table for my saved template. 我有一个保存的模板的数据库表。 All are in HTML format and I want to get a specific template using an AJAX. 全部都是HTML格式,我想使用AJAX获得特定的模板。

Here's my code: 这是我的代码:

HTML part HTML部分

<div class="form-group">
    <label>Select Template: <span class="text-info">(*Optional)</span></label>
    <select class="form-control" name="load_template" id="load-template">
        <?php if(count($templates) > 0) { ?>
        <option value="">-- load template --</option>
        <?php foreach($templates as $temp) { ?>
            <option value="<?php echo $temp['id']; ?>"><?php echo $temp['template_name']; ?></option>
        <?php } ?>
        <?php } else { ?>
            <option value="">There are no saved templates</option>
        <?php } ?>
    </select>
</div>

JS part JS部分

$('#editor').ckeditor();

$('#load-template').on('change', function(){
    var id = $(this).val();
    //do some ajax to get content...
    console.log(id);
    $.ajax({
        url: "<?php echo site_url('users/user/getLoadTemplate'); ?>",
        data: {template_id: id},
        dataType: 'html',
        type: 'post',
        beforeSend: function() {},
        success: function(template) {
            $('#editor').html(template);
            console.log(template); //ok it shows the HTML but how can I load it in my ckeditor?
        },
        error: function() {
            alert('Unable to load template. Error in AJAX');
        }
    });
});

PHP side PHP方面

public function getLoadTemplate() {
    $type = $this->input->post('template_id');
    $get_template = $this->Users_model->getLoadTemplate($type);
    echo $get_template['template'];
}

How can I load my ajax response HTML in the CKEDITOR? 如何在CKEDITOR中加载我的Ajax响应HTML?

Instead of this 代替这个

$('#editor').html(template);

Try this 尝试这个

CKEDITOR.instances['editor'].insertHtml(template) 
                   or
CKEDITOR.instances.editor.insertHtml(template)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM