Insert Table in ASP.NET CORE Markdown Editor Control

17 Mar 202520 minutes to read

To enable the table insertion feature, add the CreateTable option to the toolbar items. Once added, users can click the Insert Table icon in the toolbar to insert a table into the editor.

By default, when a table is inserted, it consists of:

  • 2 rows and 2 columns
  • A table header row

This ensures that users can start formatting and adding content immediately.

<ejs-richtexteditor id="markdown" value="@ViewBag.value" editorMode="Markdown" created="created">
    <e-richtexteditor-toolbarsettings items="@ViewBag.items"></e-richtexteditor-toolbarsettings>
</ejs-richtexteditor>

<script type="text/javascript">
    var rteObj, textArea, mdsource;
    function created() {
        rteObj = this;
        rteObj.formatter = new ej.richtexteditor.MarkdownFormatter({ listTags: { 'OL': '1., 2., 3.' } });
        rteObj.dataBind();
        textArea = rteObj.contentModule.getEditPanel();
        textArea.addEventListener('keyup', function (e) {
            markdownConversion();
        });
        mdsource = document.getElementById('preview-code');
        mdsource.addEventListener('click', function (e) {
            fullPreview();
            if (e.currentTarget.classList.contains('e-active')) {
                rteObj.disableToolbarItem(['CreateTable']);
            }
            else {
                rteObj.enableToolbarItem(['CreateTable']);
            }
        });
    }
    loadExternalFile();
    function loadExternalFile() {
        var script = document.createElement('script');
        script.src = 'https://cdnjs.cloudflare.com/ajax/libs/marked/0.3.19/marked.js';
        document.getElementsByTagName('head')[0].appendChild(script);
    }
    function markdownConversion() {
        if (mdsource.classList.contains('e-active')) {
            var id = rteObj.getID() + 'html-preview';
            var htmlPreview = rteObj.element.querySelector('#' + id);
            htmlPreview.innerHTML = marked(rteObj.contentModule.getEditPanel().value);
        }
    }
    function fullPreview() {
        var id = rteObj.getID() + 'html-preview';
        var htmlPreview = rteObj.element.querySelector('#' + id);
        var previewTextArea = rteObj.element.querySelector('.e-rte-content');
        if (mdsource.classList.contains('e-active')) {
            mdsource.classList.remove('e-active');
            mdsource.parentElement.title = 'Preview';
            textArea.style.display = 'block';
            htmlPreview.style.display = 'none';
            previewTextArea.style.overflow = 'hidden';
        }
        else {
            mdsource.classList.add('e-active');
            if (!htmlPreview) {
                htmlPreview = ej.base.createElement('div', { className: 'e-content e-pre-source' });
                htmlPreview.id = id;
                textArea.parentNode.appendChild(htmlPreview);
                previewTextArea.style.overflow = 'auto';
            }
            if (previewTextArea.style.overflow === 'hidden') {
                previewTextArea.style.overflow = 'auto';
            }
            textArea.style.display = 'none';
            htmlPreview.style.display = 'block';
            htmlPreview.innerHTML = marked(rteObj.contentModule.getEditPanel().value);
            mdsource.parentElement.title = 'Code View';
        }
    }
</script>

<style>
    .e-richtexteditor textarea.e-content {
        float: left;
    }

    .e-richtexteditor .e-rte-content {
        overflow: hidden;
    }

    .e-icon-btn.e-active .e-md-preview::before {
        content: '\e350';
    }

    .e-icon-btn .e-md-preview::before {
        content: '\e345';
    }

    .bootstrap4 .e-icon-btn.e-active .e-md-preview::before {
        content: '\e790';
    }

    .bootstrap4 .e-icon-btn .e-md-preview::before {
        content: '\e787';
    }

    .fluent .e-icon-btn.e-active .e-md-preview::before,
    .fluent-dark .e-icon-btn.e-active .e-md-preview::before,
    .fluent2 .e-icon-btn.e-active .e-md-preview::before,
    .fluent2-dark .e-icon-btn.e-active .e-md-preview::before,
    .fluent2-highcontrast .e-icon-btn.e-active .e-md-preview::before,
    .tailwind .e-icon-btn.e-active .e-md-preview::before,
    .tailwind-dark .e-icon-btn.e-active .e-md-preview::before,
    .tailwind3 .e-icon-btn.e-active .e-md-preview::before,
    .tailwind3-dark .e-icon-btn.e-active .e-md-preview::before,
    .bootstrap5 .e-icon-btn.e-active .e-md-preview::before,
    .bootstrap5-dark .e-icon-btn.e-active .e-md-preview::before,
    .bootstrap5\.3 .e-icon-btn.e-active .e-md-preview::before,
    .bootstrap5\.3-dark .e-icon-btn.e-active .e-md-preview::before,
    .material3 .e-icon-btn.e-active .e-md-preview::before,
    .material3-dark .e-icon-btn.e-active .e-md-preview::before {
        content: '\e80e';
    }

    .tailwind .e-icon-btn .e-md-preview::before,
    .tailwind-dark .e-icon-btn .e-md-preview::before,
    .tailwind3 .e-icon-btn .e-md-preview::before,
    .tailwind3-dark .e-icon-btn .e-md-preview::before,
    .bootstrap5 .e-icon-btn .e-md-preview::before,
    .bootstrap5-dark .e-icon-btn .e-md-preview::before,
    .bootstrap5\.3 .e-icon-btn .e-md-preview::before,
    .bootstrap5\.3-dark .e-icon-btn .e-md-preview::before,
    .fluent .e-icon-btn .e-md-preview::before,
    .fluent-dark .e-icon-btn .e-md-preview::before,
    .fluent2 .e-icon-btn .e-md-preview::before,
    .fluent2-dark .e-icon-btn .e-md-preview::before,
    .fluent2-highcontrast .e-icon-btn .e-md-preview::before,
    .material3 .e-icon-btn .e-md-preview::before,
    .material3-dark .e-icon-btn .e-md-preview::before {
        content: '\e7de';
    }
</style>
public class HomeController : Controller
{

    public ActionResult Index()
    {
        object tools1 = new
        {
            tooltipText = "Preview",
            template = "<button id='preview-code' class='e-tbar-btn e-control e-btn e-icon-btn'>" +
"<span class='e-btn-icon e-md-preview e-icons'></span></button>"
        };
        ViewBag.items = new[] { "CreateTable", tools1 };

        ViewBag.value = @"In Rich Text Editor , you click the toolbar buttons to format the words and the changes are visible immediately. 
Markdown is not like that. When you format the word in Markdown format, you need to add Markdown syntax to the word to indicate which words 
and phrases should look different from each other.

RichTextEditor supports markdown editing when the editorMode set as **markdown** and using both *keyboard interaction* and *toolbar action*, you can apply the formatting to text.

We can add our own custom formation syntax for the Markdown formation.

The third-party library <b>Marked</b> is used in this sample to convert markdown into HTML content";
        return View();
    }
}

Changing default content

By default, when you insert a table, it comes with predefined column headers and structure. However, you can customize the table’s default content, including the heading and column names, to match your requirements.

The following example demonstrates how to customize the table content in the Markdown Editor:

<ejs-richtexteditor id="markdown" value="@ViewBag.value" editorMode="Markdown" created="created">
    <e-richtexteditor-toolbarsettings items="@ViewBag.items"></e-richtexteditor-toolbarsettings>
</ejs-richtexteditor>

<script type="text/javascript">

    window.onload = function () {
        ej.base.L10n.load({
            'en-US': {
                'richtexteditor': {
                    'TableHeadingText': 'Header',
                    'TableColText': 'Cell'
                }
            }
        });
    }
    var rteObj, textArea, mdsource;
    function created() {
        rteObj = this;
        rteObj.formatter = new ej.richtexteditor.MarkdownFormatter({ listTags: { 'OL': '1., 2., 3.' } });
        rteObj.dataBind();
        textArea = rteObj.contentModule.getEditPanel();
        textArea.addEventListener('keyup', function (e) {
            markdownConversion();
        });
        mdsource = document.getElementById('preview-code');
        mdsource.addEventListener('click', function (e) {
            fullPreview();
            if (e.currentTarget.classList.contains('e-active')) {
                rteObj.disableToolbarItem(['CreateTable']);
            }
            else {
                rteObj.enableToolbarItem(['CreateTable']);
            }
        });
    }
    loadExternalFile();
    function loadExternalFile() {
        var script = document.createElement('script');
        script.src = 'https://cdnjs.cloudflare.com/ajax/libs/marked/0.3.19/marked.js';
        document.getElementsByTagName('head')[0].appendChild(script);
    }
    function markdownConversion() {
        if (mdsource.classList.contains('e-active')) {
            var id = rteObj.getID() + 'html-preview';
            var htmlPreview = rteObj.element.querySelector('#' + id);
            htmlPreview.innerHTML = marked(rteObj.contentModule.getEditPanel().value);
        }
    }
    function fullPreview() {
        var id = rteObj.getID() + 'html-preview';
        var htmlPreview = rteObj.element.querySelector('#' + id);
        var previewTextArea = rteObj.element.querySelector('.e-rte-content');
        if (mdsource.classList.contains('e-active')) {
            mdsource.classList.remove('e-active');
            mdsource.parentElement.title = 'Preview';
            textArea.style.display = 'block';
            htmlPreview.style.display = 'none';
            previewTextArea.style.overflow = 'hidden';
        }
        else {
            mdsource.classList.add('e-active');
            if (!htmlPreview) {
                htmlPreview = ej.base.createElement('div', { className: 'e-content e-pre-source' });
                htmlPreview.id = id;
                textArea.parentNode.appendChild(htmlPreview);
                previewTextArea.style.overflow = 'auto';
            }
            if (previewTextArea.style.overflow === 'hidden') {
                previewTextArea.style.overflow = 'auto';
            }
            textArea.style.display = 'none';
            htmlPreview.style.display = 'block';
            htmlPreview.innerHTML = marked(rteObj.contentModule.getEditPanel().value);
            mdsource.parentElement.title = 'Code View';
        }
    }
</script>

<style>
    .e-richtexteditor textarea.e-content {
        float: left;
    }

    .e-richtexteditor .e-rte-content {
        overflow: hidden;
    }

    .e-icon-btn.e-active .e-md-preview::before {
        content: '\e350';
    }

    .e-icon-btn .e-md-preview::before {
        content: '\e345';
    }

    .bootstrap4 .e-icon-btn.e-active .e-md-preview::before {
        content: '\e790';
    }

    .bootstrap4 .e-icon-btn .e-md-preview::before {
        content: '\e787';
    }

    .fluent .e-icon-btn.e-active .e-md-preview::before,
    .fluent-dark .e-icon-btn.e-active .e-md-preview::before,
    .fluent2 .e-icon-btn.e-active .e-md-preview::before,
    .fluent2-dark .e-icon-btn.e-active .e-md-preview::before,
    .fluent2-highcontrast .e-icon-btn.e-active .e-md-preview::before,
    .tailwind .e-icon-btn.e-active .e-md-preview::before,
    .tailwind-dark .e-icon-btn.e-active .e-md-preview::before,
    .tailwind3 .e-icon-btn.e-active .e-md-preview::before,
    .tailwind3-dark .e-icon-btn.e-active .e-md-preview::before,
    .bootstrap5 .e-icon-btn.e-active .e-md-preview::before,
    .bootstrap5-dark .e-icon-btn.e-active .e-md-preview::before,
    .bootstrap5\.3 .e-icon-btn.e-active .e-md-preview::before,
    .bootstrap5\.3-dark .e-icon-btn.e-active .e-md-preview::before,
    .material3 .e-icon-btn.e-active .e-md-preview::before,
    .material3-dark .e-icon-btn.e-active .e-md-preview::before {
        content: '\e80e';
    }

    .tailwind .e-icon-btn .e-md-preview::before,
    .tailwind-dark .e-icon-btn .e-md-preview::before,
    .tailwind3 .e-icon-btn .e-md-preview::before,
    .tailwind3-dark .e-icon-btn .e-md-preview::before,
    .bootstrap5 .e-icon-btn .e-md-preview::before,
    .bootstrap5-dark .e-icon-btn .e-md-preview::before,
    .bootstrap5\.3 .e-icon-btn .e-md-preview::before,
    .bootstrap5\.3-dark .e-icon-btn .e-md-preview::before,
    .fluent .e-icon-btn .e-md-preview::before,
    .fluent-dark .e-icon-btn .e-md-preview::before,
    .fluent2 .e-icon-btn .e-md-preview::before,
    .fluent2-dark .e-icon-btn .e-md-preview::before,
    .fluent2-highcontrast .e-icon-btn .e-md-preview::before,
    .material3 .e-icon-btn .e-md-preview::before,
    .material3-dark .e-icon-btn .e-md-preview::before {
        content: '\e7de';
    }
</style>
public class HomeController : Controller
{

    public ActionResult Index()
    {
        object tools1 = new
        {
            tooltipText = "Preview",
            template = "<button id='preview-code' class='e-tbar-btn e-control e-btn e-icon-btn'>" +
"<span class='e-btn-icon e-md-preview e-icons'></span></button>"
        };
        ViewBag.items = new[] { "CreateTable", tools1 };

        ViewBag.value = @"In Rich Text Editor , you click the toolbar buttons to format the words and the changes are visible immediately. 
Markdown is not like that. When you format the word in Markdown format, you need to add Markdown syntax to the word to indicate which words 
and phrases should look different from each other.

RichTextEditor supports markdown editing when the editorMode set as **markdown** and using both *keyboard interaction* and *toolbar action*, you can apply the formatting to text.

We can add our own custom formation syntax for the Markdown formation.

The third-party library <b>Marked</b> is used in this sample to convert markdown into HTML content";
        return View();
    }
}