繁体   English   中英

自定义按钮数量tinymce

[英]Custom number of buttons tinymce

如何使用JQuery创建自定义按钮tinymce按钮? 我需要n个“菜单项”按钮。 在打开tinymce编辑器之前,将根据所选数据定义“ n”。

我的按钮:

editor.addButton('addButtons', {
        type: 'menubutton',
        text: 'My button',
        icon: false,
        menu: [
            {
                text: 'Menu item 1',
                onclick: function() {
                    editor.insertContent('&nbsp;<strong>item1</strong>&nbsp;');
                }
            }, {
                text: 'Menu item 2',
                onclick: function() {
                    editor.insertContent('&nbsp;<strong>item2</strong>&nbsp;');
                }
            }, {
                text: 'Menu item 3',
                onclick: function() {
                    editor.insertContent('&nbsp;<strong>item3</strong>&nbsp;');
                }
            }
        ] 
    });

我可以使用JQuery $("#totalButtons").val()从隐藏的输入类型中获取“ n”值。 如果totalButtons为4,则需要4个项目按钮。 是否有意义? 有可能吗?

谢谢

更新的代码:

var n = $('#total').val();
var menuItems = [];

  tinymce.init({
    selector: '#mytextareaTenant',
    content_css: 'https://fonts.googleapis.com/css?family=Aguafina+Script|Alex+Brush|Bilbo|Condiment|Great+Vibes|Herr+Von+Muellerhoff|Kristi|Meddon|Monsieur+La+Doulaise|Norican|Nothing+You+Could+Do|Parisienne|Permanent+Marker|Sacramento|Yellowtail',
    theme: 'modern',
    menubar: false,
    plugins: [
        "print"
    ],           

    setup: function (editor) { 
        editor.on('init', function (e) {
            renderEditorTenant();

            for (var i=1; i<=n; i++){
                var msg = '&nbsp;<strong>#item' + i + '#</strong>&nbsp;';
                var obj = {
                    text: 'Menu item ' + i,
                    onclick: function() {
                        editor.insertContent(msg);
                    }
                }
                menuItems.push(obj);
            }
        });

假设您有一个隐藏的输入,例如:

<input type="hidden" id="foo" name="zyx" value="3" />

您可以获取输入的值并生成包含n元素的数组:

var n = $('#foo').val();
var menuItems = [];

for (var i=0; i<n; i++){
  var msg = '&nbsp;<strong>item' + i + '</strong>&nbsp;';
  var obj = {
    text: 'Menu item ' + i,
    onclick: function() {
        editor.insertContent(msg);
    }
}
  menuItems.push(obj);
}

现在,只需将此数组传递给用于生成编辑器的函数即可:

editor.addButton('addButtons', {
        type: 'menubutton',
        text: 'My button',
        icon: false,
        menu: menuItems
    });

暂无
暂无

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

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