javascriptjqueryangulartypescriptslickgrid

how to use SlickGrid (jQuery legacy library) in Angular 4


I'm trying to get a legacy jQuery library called SlickGrid working in Angular 4 and I cannot seem to figure out the best way to do that (I'm also new to Angular 4 so that doesn't help). So far, I found an npm package for @types/slickgrid and installed it. I then try to import it in my component with this

import { Component, OnInit } from '@angular/core';
import * as $ from 'jquery';
import {Grid} from 'slickgrid';

However this throws me some error in the console

app.component.ts(5,24): error TS2306: File 'C:/demo/node_modules/@types/slickgrid/index.d.ts' is not a module.

I also tried with this import 'slickgrid' but I get the same error

Here is my full component.ts file

import { Component, OnInit } from '@angular/core';
import * as $ from 'jquery';
import {Grid} from 'slickgrid';

@Component({
    selector: 'my-app',
    template: `<td valign='top' width='50%'>
        <div id='myGrid' style='width:600px;height:500px;'></div>
    </td>`
})
export class AppComponent implements OnInit {

    ngOnInit(): void {
        let grid;
        let columns = [
        {id: 'title', name: 'Title', field: 'title'},
        {id: 'duration', name: 'Duration', field: 'duration'},
        {id: '%', name: '% Complete', field: 'percentComplete'},
        {id: 'start', name: 'Start', field: 'start'},
        {id: 'finish', name: 'Finish', field: 'finish'},
        {id: 'effort-driven', name: 'Effort Driven', field: 'effortDriven'}
        ];
        let options = {
        enableCellNavigation: true,
        enableColumnReorder: false
        };
        let data = [];
        for (let i = 0; i < 500; i++) {
        data[i] = {
            title: 'Task ' + i,
            duration: '5 days',
            percentComplete: Math.round(Math.random() * 100),
            start: '01/01/2009',
            finish: '01/05/2009',
            effortDriven: (i % 5 === 0)
        };
        }
        $(() => {
        grid = new Grid('#myGrid', data, columns, options);
        });
    }
}

And my index.html where I import the libraries (I could go with WebPack eventually, if I can get this going)

    <html>
    <head>
    <link rel="stylesheet" type="text/css" href="styles.css"/>
    <link rel="stylesheet" type="text/css" href="/node_modules/slickgrid-6pac/slick.grid.css"/>
    <link rel="stylesheet" type="text/css" href="/components/css/SlickGrid.css"/>
    </head>
    <body>
        <my-app> Loading... </my-app>
    </body>
    <script src="/node_modules/slickgrid-6pac/lib/jquery-1.11.2.js"></script>
    <script src="/node_modules/slickgrid-6pac/lib/jquery.event.drag-2.2.js"></script>
    <script src="/node_modules/underscore/underscore-min.js"></script>
    <script src="/node_modules/slickgrid-6pac/slick.core.js"></script>
    <script src="/node_modules/slickgrid-6pac/slick.grid.js"></script>
    <!-- Polyfill(s) for older browsers -->
    <script src="/node_modules/core-js/client/shim.min.js"></script>
    <script src="/node_modules/zone.js/dist/zone.min.js"></script>
    <script src="/node_modules/reflect-metadata/Reflect.js"></script>
    <script src="/node_modules/systemjs/dist/system.src.js"></script>
    <script src="/dist/basic_slickgrid_ts/systemjs.config.js"></script>
    <script>
    System.import('app').catch(function(err){ console.error(err); });
    </script>
</html>

I really need to get this working and I don't know what else to try.


Solution

  • Thanks a lot to @Rahul Singh who posted a comment that answers my question. With the instructions shown below, I was able to create a new Open Source lib Angular-Slickgrid which everyone can now enjoy.

    Install Angular-CLI and modify the angular-cli.json file

    {
      "apps": [
      {
        "styles": [
          "../node_modules/slickgrid/slick.grid.css",
          "styles.css"
        ],
        "scripts": [
          "../node_modules/slickgrid/lib/jquery-1.8.3.js",
          "../node_modules/slickgrid/lib/jquery.event.drag-2.2.js",
          "../node_modules/slickgrid/slick.core.js",
          "../node_modules/slickgrid/slick.grid.js"
        ],    
    }
    

    Do an npm install of the legacy library

    npm install slickgrid
    

    add <div> for the grid in the component.html file

    <div id='myGrid' style='width:600px;height:500px;'></div>
    

    and finally the component.ts

    import { Component, OnInit } from '@angular/core';
    import $ from 'jquery/dist/jquery';
    import jQuery from 'jquery/dist/jquery';
    
    // using external js modules in Angular
    declare var Slick: any;
    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css']
    })
    export class AppComponent implements OnInit {
      title = 'app';
    
      ngOnInit(): void {
          let grid;
          const columns = [
            {id: 'title', name: 'Title', field: 'title'},
            {id: 'duration', name: 'Duration', field: 'duration'},
            {id: '%', name: '% Complete', field: 'percentComplete'},
            {id: 'start', name: 'Start', field: 'start'},
            {id: 'finish', name: 'Finish', field: 'finish'},
            {id: 'effort-driven', name: 'Effort Driven', field: 'effortDriven'}
          ];
          const options = {
            enableCellNavigation: true,
            enableColumnReorder: false
          };
          let data = [];
          for (let i = 0; i < 500; i++) {
            data[i] = {
              title: 'Task ' + i,
              duration: '5 days',
              percentComplete: Math.round(Math.random() * 100),
              start: '01/01/2009',
              finish: '01/05/2009',
              effortDriven: (i % 5 === 0)
            };
          }
          $(() => {
            grid = new Slick.Grid('#myGrid', data, columns, options);
          });
      }
    }
    

    and it works, again thanks @Rahul