1

I'm new to Angular and trying to set up tables in quill editor. Whenever I try to register the quill-better-table module. I face major issues. Look at my code below.

import { Component , ViewChild, OnInit} from '@angular/core';
import QuillBetterTable from 'quill-better-table';
import Quill from 'quill';
import { QuillEditorComponent } from 'ngx-quill';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit{
  @ViewChild(QuillEditorComponent, { static: true }) editor: QuillEditorComponent
  content = 'Hello World!'
  modules = {};
  ngOnInit(){
    Quill.register({
        'modules/better-table': QuillBetterTable
    });
  }
  constructor()
  {
    this.modules = {
      table: false,  // disable table module
      'better-table': {
        operationMenu: {
          items: {
            unmergeCells: {
              text: 'Another unmerge cells name'
            }
          },
          color: {
            colors: ['green', 'red', 'yellow', 'blue', 'white'],
            text: 'Background Colors:'
          }
        }
      },
      keyboard: {
        bindings: QuillBetterTable.keyboardBindings
      }
    }
  }
}

I receive these errors -

quill Cannot import modules/table. Are you sure it was registered?
debug @ quill.js:2037

quill.js:2037 quill Cannot load table module. Are you sure you registered it?
zgue
  • 3,793
  • 9
  • 34
  • 39
Alec Aldrine Lakra
  • 355
  • 1
  • 4
  • 14

1 Answers1

4

I am assuming you already have angular project configured. If not, you can use this command to create a new angular project npx @angular/cli@next new editor

  1. Use yarn/npm to add quill.js packages to angular project.
> npm install quill@dev quill-better-table ngx-quill

After installation, your project's package.json should have following dependencies

  "dependencies": {
    "ngx-quill": "^7.3.9",
    "quill": "^2.0.0-dev.3 ",
    "quill-better-table": "^1.2.4",
   }
  1. Import quill.js snow theme(or any other theme). In file src/styles.scss, add this snippet
@import "~quill/dist/quill.snow.css";
  1. Import and configure Quill Module (File: src/app/app.module.ts)
import { NgModule } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";
import { QuillConfig, QuillModule } from "ngx-quill";
import * as Quill from "quill";
import QuillBetterTable from "quill-better-table";
import { AppComponent } from "./app.component";

Quill.register(
  {
    "modules/better-table": QuillBetterTable
  },
  true
);

const quillConfig: QuillConfig = {
  modules: {
    table: false, // disable table module
    "better-table": {
      operationMenu: {
        items: {
          unmergeCells: {
            text: "Another unmerge cells name"
          }
        },
        color: {
          colors: ["#fff", "red", "rgb(0, 0, 0)"], // colors in operationMenu
          text: "Background Colors" // subtitle
        }
      }
    },
    keyboard: {
      bindings: QuillBetterTable.keyboardBindings
    }
  }
};
@NgModule({
  declarations: [AppComponent],
  imports: [BrowserModule, QuillModule.forRoot(quillConfig)],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule {}
  1. Add HTML tag. (File: src/app/app.component.html)
<quill-editor (onEditorCreated)="editorCreated($event)"></quill-editor>
  1. Insert table in editor (File: src/app/app.component.ts)
import { ChangeDetectionStrategy, Component } from "@angular/core";

interface Quill {
  getModule(moduleName: string);
}

interface BetterTableModule {
  insertTable(rows: number, columns: number): void;
}

@Component({
  selector: "app-root",
  templateUrl: "./app.component.html",
  styleUrls: ["./app.component.scss"],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class AppComponent {
  public quill: Quill;

  private get tableModule(): BetterTableModule {
    return this.quill.getModule("better-table");
  }

  public editorCreated(event: Quill): void {
    this.quill = event;
    // Example on how to add new table to editor
    this.addNewtable();
  }

  private addNewtable(): void {
    this.tableModule.insertTable(3, 3);
  }
}

This is how the output looks like at the end: enter image description here

Prabh
  • 989
  • 5
  • 10
  • 1
    Everytime I add that line **tables : false** , I still get the error saying -- `Cannot import modules/table. Are you sure it was registered?` and when I remove that line , I still get an everlasting `ERROR TypeError: Cannot read property 'pop' of undefined at new quill_better_table_BetterTable` – Alec Aldrine Lakra Nov 05 '19 at 11:56
  • Can you create a separate angular app using `npx @angular/cli@next new editor` command and exactly follow all of the above steps? – Prabh Nov 05 '19 at 14:28
  • Yeah it worked fine with your code, but I feel this module is at beginning stage and cannot be used for production use , there are few bugs , you can delete all the cells and still be stuck with one, that's awful. Anyways thanks , your idea was helpful – Alec Aldrine Lakra Nov 06 '19 at 17:54
  • 1
    Hi I implemented it it works when I open a form and past a table. however, once the data is inserted in database and when I open my form to edit, it does not display data in the box. someone else also face the same issue here: https://stackoverflow.com/questions/60102086/quill-table-with-angular-not-working-on-edit-mode can you check – MJ X Feb 07 '20 at 19:23
  • Does this require an Angular Framework? Can it be setup without Angular? I'm struggling to do that. – user2449525 Jun 12 '20 at 12:48
  • Hi, i tried this & i get the error `Uncaught TypeError: Cannot read property 'register' of undefined`. i useangular 5 & quill versions are: "ngx-quill": "^7.3.9", "quill": "^2.0.0-dev.3", "quill-better-table": "^1.2.10", – fayas Nov 03 '20 at 12:11
  • Can you create a stackblitz demo so i can understand your problem better? – Prabh Nov 04 '20 at 14:48
  • Two-way binding is not working using quill@2.0.0-dev.4. How binding data – Harleen Kaur Arora Jun 25 '21 at 06:11
  • @AlecAldrineLakra, did you get an answer to this ever? I'm having the same issue. – S.Slusky May 28 '22 at 16:36