Jhtmlarea wysiwyg editor - Adding a table

I did an extensive search, looking for information on adding a custom button on the toolbar that would bring the table into the editor.

That's all I could find (which, by the way, doesn't answer the question): Adding tables to jHtmlArea

Does anyone have thoughts on how to do or have seen something like this that I could use as a reference?

thanks joe

0
source share
1 answer

Sorry to revive the old theme, but I also wanted to be able to add tables using jhtmlarea. This is what I came up with

$("#editor").htmlarea({
    toolbar: [
        ["html"],
        ["bold","italic","underline","strikeThrough","|","subscript","superscript"],
        ["orderedList","unorderedList"],
        ["indent","outdent"],
        ["link","unlink","|","horizontalrule"],
        ["cut","copy","paste"],
        [{
            css: 'jhtml-table',
            text: 'Insert Table',
            action: function(btn) {
                var t = this, li = btn.closest('li');
                var trtd = Array(11).join('<tr>'+Array(11).join('<td></td>')+'</tr>');
                var tbl = $('<table tabindex="0"><tbody>' + trtd + '</tbody><tfoot><tr><td colspan="10" style="color:#888;text-align:center;">&nbsp;</td></tr></tfoot></table>')
                    .css({position:'absolute', background:'#fff', border:'#bbb 1px solid', outline:'0 none'})
                    .appendTo(li)
                    .focus().blur(function(e){ tbl.remove(); });
                tbl.find('tbody td').css({border:'#bbb 1px solid',height:'12px',width:'12px'})
                    .hover(function(e){
                        var td = $(this),
                            x = td.index()+1,
                            y = td.closest('tr').index()+1;
                        tbl.find('tfoot td').html(x + 'x' + y);
                        tbl.find('tbody tr').slice(0,y).each(function(i){
                            $(this).find('td').slice(0,x).css({background:'#ddd'});
                            $(this).find('td').slice(x).css({background:'none'});
                        });
                        tbl.find('tbody tr').slice(y).each(function(i){
                            $(this).find('td').css({background:'none'});
                        })
                    })
                    .click(function(e){
                        var td = $(this),
                            x = td.index()+2,
                            y = td.closest('tr').index()+2;
                        t.pasteHTML('<table class="display-table"><tbody>' + Array(y).join('<tr>'+Array(x).join('<td>&nbsp;</td>')+'</tr>') + '</tbody></table>');
                        tbl.remove();
                    });

            }
        }]
    ],
    css: "css/jHtmlArea.editor.css"
});

- ( Chrome), , html.

+2

Source: https://habr.com/ru/post/1729148/


All Articles