Table in Angular Document editor component

27 Apr 202414 minutes to read

Tables are an efficient way to present information. Document Editor can display and edit the tables. You can select and edit tables through keyboard, mouse, or touch interactions. Document Editor exposes a rich set of APIs to perform these operations programmatically.

Create a table

You can create and insert a table at cursor position by specifying the required number of rows and columns.

Refer to the following sample code.

 this.documentEditor.editor.insertTable(3,3);

The maximum size of row and column is limited to 32767 and 63 respectively.

Insert rows

You can add a row (or several rows) above or below the row at cursor position by using the insertRow method. This method accepts the following parameters:

Parameter Type Description
above(optional) boolean This is optional and if omitted, it takes the value as false and inserts below the row at cursor position.
count(optional) number This is optional and if omitted, it takes the value as 1.

Refer to the following sample code.

//Inserts a row below the row at cursor position
this.documentEditor.editor.insertRow();
//Inserts a row above the row at cursor position
this.documentEditor.editor.insertRow(false);
//Inserts three rows below the row at cursor position
this.documentEditor.editor.insertRow(true, 3)

Insert columns

You can add a column (or several columns) to the left or right of the column at cursor position by using the insertColumn method. This method accepts the following parameters:

Parameter Type Description
left(optional) boolean This is optional and if omitted, it takes the value as false and inserts to the right of column at cursor position.
count(optional) number This is optional and if omitted, it takes the value as 1.

Refer to the following sample code.

//Insert a column to the right of the column at cursor position.
this.documentEditor.editor.insertColumn();
//Insert a column to the left of the column at cursor position.
this.documentEditor.editor.insertColumn(false);
//Insert two columns to the left of the column at cursor position.
this.documentEditor.editor.insertColumn(false, 2);

Select an entire table

If the cursor position is inside a table, you can select the entire table by using the following sample code.

this.documentEditor.selection.selectTable();

Select row

You can select the entire row at cursor position by using the following sample code.

this.documentEditor.selection.selectRow();

If current selection spans across cells of different rows, all these rows will be selected.

Select column

You can select the entire column at cursor position by using the following sample code.

this.documentEditor.selection.selectColumn();

If current selection spans across cells of different columns, all these columns will be selected.

Select cell

You can select the cell at cursor position by using the following sample code.

this.documentEditor.selection.selectCell();

Delete table

Document Editor allows you to delete the entire table. You can use the deleteTable() method of editor instance, if selection is in table. Refer to the following sample code.

this.documentEditor.editor.deleteTable();

Delete row

Document Editor allows you to delete the selected number of rows. You can use the deleteRow() method of editor instance to delete the selected number of rows, if selection is in table. Refer to the following sample code.

this.documentEditor.editor.deleteRow();

Delete column

Document Editor allows you to delete the selected number of columns. You can use the deleteColumn() method of editor instance to delete the selected number of columns, if selection is in table. Refer to the following sample code.

this.documentEditor.editor.deleteColumn();

Merge cells

You can merge cells vertically, horizontally, or combination of both to a single cell. To vertically merge the cells, the columns within selection should be even in left and right directions. To horizontally merge the cells, the rows within selection should be even in top and bottom direction.
Refer to the following sample code.

this.documentEditor.editor.mergeCells()

Positioning the table

Document Editor preserves the position properties of the table and displays the table based on position properties. It does not support modifying the position properties. Whereas the table will be automatically moved along with text edited if it is positioned relative to the paragraph.

How to work with tables

The following sample demonstrates how to delete the table row or columns, merge cells and how to bind the API with button.

import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { ButtonModule } from '@syncfusion/ej2-angular-buttons'
import { DocumentEditorAllModule } from '@syncfusion/ej2-angular-documenteditor'
import { ToolbarModule } from '@syncfusion/ej2-angular-navigations'
import { ComboBoxModule } from '@syncfusion/ej2-angular-dropdowns'
import {ColorPickerModule } from '@syncfusion/ej2-angular-inputs'



import { Component, ViewEncapsulation, ViewChild } from '@angular/core';
import {
    DocumentEditorComponent, EditorService, SelectionService, SfdtExportService, EditorHistoryService, TableDialogService, ContextMenuService
} from '@syncfusion/ej2-angular-documenteditor';

@Component({
imports: [
        
        ButtonModule,
        ToolbarModule,
        DocumentEditorAllModule,
        ComboBoxModule,
        ColorPickerModule
    ],


standalone: true,
        selector: 'app-container',
        styleUrls: ['./style.css'],
        template: `<div>
        <div>
            <ejs-toolbar (clicked)='toolbarButtonClick($event)'>
                <e-items>
                    <e-item prefixIcon="e-de-ctnr-table e-icons" tooltipText="Insert Table" id="table"></e-item>
                    <e-item type="Separator"></e-item>
                    <e-item prefixIcon="e-de-ctnr-insertabove e-icons" tooltipText="Insert new row above" id="insert_above"></e-item>
                    <e-item prefixIcon="e-de-ctnr-insertbelow e-icons" tooltipText="Insert new row below" id="insert_below"></e-item>
                    <e-item type="Separator"></e-item>
                    <e-item prefixIcon="e-de-ctnr-insertleft e-icons" tooltipText="Insert new column to the left" id="insert_left"></e-item>
                    <e-item prefixIcon="e-de-ctnr-insertright e-icons" tooltipText="Insert new column to the right" id="insert_right"></e-item>
                    <e-item type="Separator"></e-item>
                    <e-item prefixIcon="e-de-delete-table e-icons" tooltipText="Delete Entire table" id="delete_table"></e-item>
                    <e-item prefixIcon="e-de-ctnr-deleterows e-icons" tooltipText="Delete the selected row" id="delete_row"></e-item>
                    <e-item prefixIcon="e-de-ctnr-deletecolumns e-icons" tooltipText="Delete the selected column" id="delete_column"></e-item>
                    <e-item type="Separator"></e-item>
                    <e-item prefixIcon="e-de-ctnr-mergecell e-icons" tooltipText="Merge the selected cells" id="merge_cell"></e-item>
                    <e-item type="Separator"></e-item>
                    <e-item text="Dialog" tooltipText="Open insert table dialog" id="table_dialog"></e-item>
                </e-items>
            </ejs-toolbar>
        </div>
    <ejs-documenteditor #document_editor [isReadOnly]=false  [enableSelection]=true  [enableEditorHistory]=true  [enableEditor]=true [enableTableDialog]=true [enableContextMenu]=true [enableSfdtExport]=true height="330px" style="display:block" (created)="onCreated()"></ejs-documenteditor>
        </div>`,
        encapsulation: ViewEncapsulation.None,
        providers: [EditorService, SelectionService, SfdtExportService, EditorHistoryService, TableDialogService, ContextMenuService]
})

export class AppComponent {
    @ViewChild('document_editor')
    public documentEditor?: DocumentEditorComponent;

    onCreated(): void {
        (this.documentEditor as DocumentEditorComponent).editor.insertTable(2, 2);
    }

    public toolbarButtonClick(arg: any) {
        switch (arg.item.id) {
            case 'table':
                //Insert table API to add table
                (this.documentEditor as DocumentEditorComponent).editor.insertTable(3, 2);
                break;
            case 'insert_above':
                //Insert the specified number of rows to the table above to the row at cursor position
                (this.documentEditor as DocumentEditorComponent).editor.insertRow(true, 2);
                break;
            case 'insert_below':
                //Insert the specified number of rows to the table below to the row at cursor position
                (this.documentEditor as DocumentEditorComponent).editor.insertRow();
                break;
            case 'insert_left':
                //Insert the specified number of columns to the table left to the column at cursor position
                (this.documentEditor as DocumentEditorComponent).editor.insertColumn(true, 2);
                break;
            case 'insert_right':
                //Insert the specified number of columns to the table right to the column at cursor position
                (this.documentEditor as DocumentEditorComponent).editor.insertColumn();
                break;
            case 'delete_table':
                //Delete the entire table
                (this.documentEditor as DocumentEditorComponent).editor.deleteTable();
                break;
            case 'delete_row':
                //Delete the selected number of rows
                (this.documentEditor as DocumentEditorComponent).editor.deleteRow();
                break;
            case 'delete_column':
                //Delete the selected number of columns
                (this.documentEditor as DocumentEditorComponent).editor.deleteColumn();
                break;
            case 'merge_cell':
                //Merge the selected cells into one (both vertically and horizontally)
                (this.documentEditor as DocumentEditorComponent).editor.mergeCells();
                break;
            case 'table_dialog':
                //Opens insert table dialog
                (this.documentEditor as DocumentEditorComponent).showDialog('Table');
                break;
        }
    }
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));

See Also