django StackedInline tinyMCE 问题

发布于 2024-11-30 17:20:34 字数 3250 浏览 1 评论 0原文

当我单击在内联块中添加另一个{{ verbose_name }}时,显示tinymce-textatea,但所有控件和编辑都被禁用(控制台中没有错误)。在3中,第一个textarea的tinymce工作正常。 恕我直言,这是初始化问题。
代码:

#templates\admin\base.html
<script type="text/javascript" src="{% admin_media_prefix %}tinymce/jscripts/tiny_mce/tiny_mce.js"></script>
<script type="text/javascript">
    window.__admin_media_prefix__ = "{% filter escapejs %}{% admin_media_prefix %}{% endfilter %}";

    var tMCE = tinyMCE.init({
        //General otptions
        mode : 'textareas',
        theme: 'advanced',
        readonly : false,
        plugins : 'autolink,lists,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template',

        // Theme options
        theme_advanced_buttons1 : 'save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,formatselect,fontsizeselect,|,forecolor,backcolor',
        theme_advanced_buttons2 : 'cut,copy,paste,pastetext,pasteword,|,search,replace,|,undo,redo,|,link,unlink,anchor,|,image,media,|,fullscreen,code',
        theme_advanced_buttons3 : '',
        theme_advanced_toolbar_location : 'top',
        theme_advanced_toolbar_align : 'left',
        theme_advanced_statusbar_location : 'bottom',
        theme_advanced_resizing : true,

        file_browser_callback : function(field_name, url, type, win) {
            var w = window.open('/elfinder', null, 'width=1000,height=800');
            w.tinymceFileField = field_name;
            w.tinymceFileWin = win;
        }

    });
</script>

#admin.py
class PostInline( admin.TabularInline ):
    model = Post
    readonly_fields = ('created', 'updated')

class ItemAdmin( admin.ModelAdmin ):
    inlines = [
        PostInline,
    ]
    readonly_fields = ('created', 'updated')

更新:

#base.html

$(document).ready(function(){     
    tinyMCE.settings = tinyMCE_opt;
    $( 'textarea:not(.inline-group textarea)' ).each( function() {
        tinyMCE.execCommand( 'mceAddControl', false, this.id );
    } );
});
#stacked.html
$(rows).formset({
            prefix: "{{ inline_admin_formset.formset.prefix }}",
            addText: "{% blocktrans with inline_admin_formset.opts.verbose_name|title as verbose_name %}Add another {{ verbose_name }}{% endblocktrans %}",
            formCssClass: "dynamic-{{ inline_admin_formset.formset.prefix }}",
            deleteCssClass: "inline-deletelink",
            deleteText: "{% trans "Remove" %}",
            emptyCssClass: "empty-form",
            removed: updateInlineLabel,
            added: (function(row) {
                initPrepopulatedFields(row);
                reinitDateTimeShortCuts();
                updateSelectFilter();
                updateInlineLabel(row);

                $( row ).find( 'textarea' ).each( function() {
                    tinyMCE.execCommand( 'mceAddControl', false, this.id );
                } );

            })
        });

但是出现另一个问题:
如果发生错误,方法formset将不再工作,内联文本区域是一个简单的文本区域

When I click Add another {{ verbose_name }} in inline block, displays tinymce-textatea, but all controls and edit are disabled ( no errors in console ). In 3 first textarea's tinymce works properly.
Imho it is init problem.
Code:

#templates\admin\base.html
<script type="text/javascript" src="{% admin_media_prefix %}tinymce/jscripts/tiny_mce/tiny_mce.js"></script>
<script type="text/javascript">
    window.__admin_media_prefix__ = "{% filter escapejs %}{% admin_media_prefix %}{% endfilter %}";

    var tMCE = tinyMCE.init({
        //General otptions
        mode : 'textareas',
        theme: 'advanced',
        readonly : false,
        plugins : 'autolink,lists,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template',

        // Theme options
        theme_advanced_buttons1 : 'save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,formatselect,fontsizeselect,|,forecolor,backcolor',
        theme_advanced_buttons2 : 'cut,copy,paste,pastetext,pasteword,|,search,replace,|,undo,redo,|,link,unlink,anchor,|,image,media,|,fullscreen,code',
        theme_advanced_buttons3 : '',
        theme_advanced_toolbar_location : 'top',
        theme_advanced_toolbar_align : 'left',
        theme_advanced_statusbar_location : 'bottom',
        theme_advanced_resizing : true,

        file_browser_callback : function(field_name, url, type, win) {
            var w = window.open('/elfinder', null, 'width=1000,height=800');
            w.tinymceFileField = field_name;
            w.tinymceFileWin = win;
        }

    });
</script>

#admin.py
class PostInline( admin.TabularInline ):
    model = Post
    readonly_fields = ('created', 'updated')

class ItemAdmin( admin.ModelAdmin ):
    inlines = [
        PostInline,
    ]
    readonly_fields = ('created', 'updated')

UPDATE:

#base.html

$(document).ready(function(){     
    tinyMCE.settings = tinyMCE_opt;
    $( 'textarea:not(.inline-group textarea)' ).each( function() {
        tinyMCE.execCommand( 'mceAddControl', false, this.id );
    } );
});
#stacked.html
$(rows).formset({
            prefix: "{{ inline_admin_formset.formset.prefix }}",
            addText: "{% blocktrans with inline_admin_formset.opts.verbose_name|title as verbose_name %}Add another {{ verbose_name }}{% endblocktrans %}",
            formCssClass: "dynamic-{{ inline_admin_formset.formset.prefix }}",
            deleteCssClass: "inline-deletelink",
            deleteText: "{% trans "Remove" %}",
            emptyCssClass: "empty-form",
            removed: updateInlineLabel,
            added: (function(row) {
                initPrepopulatedFields(row);
                reinitDateTimeShortCuts();
                updateSelectFilter();
                updateInlineLabel(row);

                $( row ).find( 'textarea' ).each( function() {
                    tinyMCE.execCommand( 'mceAddControl', false, this.id );
                } );

            })
        });

But appears another problem:
If an error occurred method formset does not work anymore and inlines textareas is a simple textareas

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蝶舞 2024-12-07 17:20:34

解决方案(感谢托马斯):

#base.html
$(document).ready(function(){    
    tinyMCE.settings = tinyMCE_opt;
    $( 'textarea:not(.inline-group textarea)' ).each( function() {
        tinyMCE.execCommand( 'mceAddControl', false, this.id );
    } );        
});

#stacked.html

$(rows).formset({
    prefix: "{{ inline_admin_formset.formset.prefix }}",
    addText: "{% blocktrans with inline_admin_formset.opts.verbose_name|title as verbose_name %}Add another {{ verbose_name }}{% endblocktrans %}",
    formCssClass: "dynamic-{{ inline_admin_formset.formset.prefix }}",
    deleteCssClass: "inline-deletelink",
    deleteText: "{% trans "
    Remove " %}",
    emptyCssClass: "empty-form",
    removed: updateInlineLabel,
    added: (function (row) {
        initPrepopulatedFields(row);
        reinitDateTimeShortCuts();
        updateSelectFilter();
        updateInlineLabel(row);
        $(row).find('textarea').each(function () {
            tinyMCE.execCommand('mceAddControl', false, this.id);
        });


    })
});

$('.dynamic-{{ inline_admin_formset.formset.prefix }} textarea').each(function () {
    tinyMCE.execCommand('mceAddControl', false, this.id);
});

solution ( thanks Thomas):

#base.html
$(document).ready(function(){    
    tinyMCE.settings = tinyMCE_opt;
    $( 'textarea:not(.inline-group textarea)' ).each( function() {
        tinyMCE.execCommand( 'mceAddControl', false, this.id );
    } );        
});

#stacked.html

$(rows).formset({
    prefix: "{{ inline_admin_formset.formset.prefix }}",
    addText: "{% blocktrans with inline_admin_formset.opts.verbose_name|title as verbose_name %}Add another {{ verbose_name }}{% endblocktrans %}",
    formCssClass: "dynamic-{{ inline_admin_formset.formset.prefix }}",
    deleteCssClass: "inline-deletelink",
    deleteText: "{% trans "
    Remove " %}",
    emptyCssClass: "empty-form",
    removed: updateInlineLabel,
    added: (function (row) {
        initPrepopulatedFields(row);
        reinitDateTimeShortCuts();
        updateSelectFilter();
        updateInlineLabel(row);
        $(row).find('textarea').each(function () {
            tinyMCE.execCommand('mceAddControl', false, this.id);
        });


    })
});

$('.dynamic-{{ inline_admin_formset.formset.prefix }} textarea').each(function () {
    tinyMCE.execCommand('mceAddControl', false, this.id);
});
野味少女 2024-12-07 17:20:34

不幸的是,您需要捕获“新内联”按钮按下并使用它自己将 mce 附加到新创建的字段。或者您可以重新初始化整个页面,但这只是浪费。

编辑:

你需要使用

django.jquery("#id-of-button").click(function () {
     tinyMCE.execCommand('mceAddControl',false,'id-of-new-textarea');
}

Unfortunately you need to capture that "new inline" button press and use it to attach mce to the newly created fields yourself. or you could re-init the whole page but that would just be wasteful.

EDIT:

you need to use

django.jquery("#id-of-button").click(function () {
     tinyMCE.execCommand('mceAddControl',false,'id-of-new-textarea');
}
一紙繁鸢 2024-12-07 17:20:34

这是一个无需修改模板即可工作的 hack(至少在 TinyMCE 3.x 上)。

django.jQuery(function ($) {  // django's copy of jQuery is crucial for old_add.click()
    setTimeout(function () {
        $('.add-row a').each(function () {
            var inline_group = $(this).parents('.inline-group');
            var new_add = $(this).clone();
            var old_add = $(this);
            old_add.hide().before(new_add);
            new_add.click(function () {
                old_add.click();
                if (inline_group.find('.mceEditor').length) {
                    var widget = inline_group.find('.form-row:not(.empty-form) .mceEditor');
                    var textarea = widget.prev('textarea').show();
                    widget.remove();
                    tinyMCE.execCommand('mceAddControl', true, textarea.attr('id'));
                }
            });
        });
    }, 0);
});

Here's a hack that works (at least on TinyMCE 3.x) without needing to modify the template.

django.jQuery(function ($) {  // django's copy of jQuery is crucial for old_add.click()
    setTimeout(function () {
        $('.add-row a').each(function () {
            var inline_group = $(this).parents('.inline-group');
            var new_add = $(this).clone();
            var old_add = $(this);
            old_add.hide().before(new_add);
            new_add.click(function () {
                old_add.click();
                if (inline_group.find('.mceEditor').length) {
                    var widget = inline_group.find('.form-row:not(.empty-form) .mceEditor');
                    var textarea = widget.prev('textarea').show();
                    widget.remove();
                    tinyMCE.execCommand('mceAddControl', true, textarea.attr('id'));
                }
            });
        });
    }, 0);
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文