Insert Table in Angular Markdown Editor Component
29 Aug 20259 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.
import { enableRipple, createElement } from '@syncfusion/ej2-base';
import { Component, ViewChild } from '@angular/core';
import { RichTextEditorModule, ToolbarSettingsModel, ContentRender, RichTextEditorComponent, ToolbarService, LinkService, ImageService, MarkdownEditorService, TableService } from '@syncfusion/ej2-angular-richtexteditor';
import { marked } from 'marked';
enableRipple(true);
@Component({
imports: [RichTextEditorModule],
standalone: true,
selector: 'app-root',
template: `<ejs-richtexteditor id='mdCustom' #mdCustom [toolbarSettings]='tools' [editorMode]='mode' (created)='onCreate()' [value]='value'>
</ejs-richtexteditor>`,
providers: [ToolbarService, LinkService, ImageService, MarkdownEditorService, TableService]
})
export class AppComponent {
@ViewChild('mdCustom')
public editorObj?: RichTextEditorComponent;
public textArea?: HTMLTextAreaElement;
public mdsource?: HTMLElement;
public tools: ToolbarSettingsModel = {
items: ['CreateTable',
{
tooltipText: 'Preview',
template: '<button id="preview-code" class="e-tbar-btn e-control e-btn e-icon-btn">' +
'<span class="e-btn-icon e-icons e-md-preview"></span></button>'
}]
};
public mode: string = 'Markdown';
public value: string = "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. Rich Text Editor supports markdown editing when the editorMode set as **markdown** and using both *keyboard interaction* and *toolbar action*, you can apply the formatting to text. You can add our own custom formation syntax for the Markdown formation, [sample link](https://ej2.syncfusion.com/home/). The third-party library <b>Marked</b> is used in this sample to convert markdown into HTML content.";
public onCreate(): void {
this.textArea = (this.editorObj!.contentModule as ContentRender).getEditPanel() as HTMLTextAreaElement;
this.textArea.addEventListener('keyup', () => {
this.markDownConversion();
});
this.mdsource = document.getElementById('preview-code') as HTMLElement;
this.mdsource ?.addEventListener('click', (e: MouseEvent) => {
this.fullPreview();
});
}
public async markDownConversion(): Promise<void> {
if (this.mdsource ?.classList.contains('e-active')) {
let id: string = this.editorObj ?.getID() + 'html-view';
let htmlPreview: Element = this.editorObj!.element.querySelector('#' + id) as Element;
htmlPreview.innerHTML = await marked(((this.editorObj!.contentModule as ContentRender).getEditPanel() as HTMLTextAreaElement).value);
}
}
public async fullPreview(): Promise<void> {
let id: string = this.editorObj!.getID() + 'html-preview';
let htmlPreview: HTMLElement = this.editorObj!.element.querySelector('#' + id) as HTMLElement;
if (this.mdsource!.classList.contains('e-active')) {
this.mdsource!.classList.remove('e-active');
this.textArea!.style.display = 'block';
htmlPreview.style.display = 'none';
} else {
this.mdsource!.classList.add('e-active');
if (!htmlPreview) {
htmlPreview = createElement('div', { className: 'e-content e-pre-source' });
htmlPreview.id = id;
this.textArea!.parentNode!.appendChild(htmlPreview);
}
this.textArea!.style.display = 'none';
htmlPreview.style.display = 'block';
htmlPreview.innerHTML = await marked(((this.editorObj!.contentModule as ContentRender).getEditPanel() as HTMLTextAreaElement).value);
this.mdsource!.parentElement!.title = 'Code View';
}
}
}import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));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:
import { Component } from '@angular/core';
import { L10n } from '@syncfusion/ej2-base';
import { RichTextEditorModule, ToolbarService, MarkdownEditorService , HtmlEditorService, ImageService, LinkService, TableService, ToolbarSettingsModel } from '@syncfusion/ej2-angular-richtexteditor';
L10n.load({
'en-US': {
richtexteditor: { TableHeadingText: 'Header', TableColText: 'Cell' },
},
});
@Component({
imports: [
RichTextEditorModule
],
standalone: true,
selector: 'app-root',
template: `<ejs-richtexteditor id='markdown-editor' [editorMode]='mode' [toolbarSettings]='tools' [value]="value"></ejs-richtexteditor>`,
providers: [ToolbarService, HtmlEditorService, MarkdownEditorService, ImageService, LinkService, TableService]
})
export class AppComponent {
public mode: string = 'Markdown';
public value: string =
'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. Rich Text Editor supports markdown editing when the editorMode set as **markdown** and using both *keyboard interaction* and *toolbar action*, you can apply the formatting to text. You can add our own custom formation syntax for the Markdown formation, [sample link](https://ej2.syncfusion.com/home/). The third-party library <b>Marked</b> is used in this sample to convert markdown into HTML content.';
public tools: ToolbarSettingsModel = {
items: [
'CreateTable',
],
};
}import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));