Skip to content
This repository was archived by the owner on Feb 2, 2025. It is now read-only.

Getting it working with webpack #978

Closed
marnickmenting opened this issue Feb 23, 2017 · 25 comments
Closed

Getting it working with webpack #978

marnickmenting opened this issue Feb 23, 2017 · 25 comments
Labels
Angular stale Used to close issues for inactivity (by bot)

Comments

@marnickmenting
Copy link

marnickmenting commented Feb 23, 2017

I am trying to get DataTables working with webpack using angular 2.4.

What versions you are using?

  • angular version: 2.4
  • jquery version: 3.1.1
  • datatables version: 1.10.13
  • angular-datatables version: 2.1.0
  • angular-MM version: ?

What's the problem?

I can't get my table interactive. I don't know where to put the dependencies.

Can you share your code?

This is what I have so far. I added this to install dependencies in package.json.

npm install jquery --save
npm install datatables.net --save
npm install datatables.net-dt --save
npm install angular-datatables --save

In app.module.ts I added at the top:

import { DataTablesModule } from 'angular-datatables';

and at the 'imports' key under @NgModule:

    DataTablesModule,

Then in table.template.html I added the HTML from the "Zero configuration" Getting Started manual.

<table datatable class="row-border hover">
...
</table>

But now I don't know how to make the table a datatable. Should I add an import somewhere? I am not using Angular-CLI so the manual does not help me here.

@marnickmenting
Copy link
Author

I got the CSS loaded at least, by adding this to table.component.ts:

@Component({
	templateUrl: './table.template.html',
	styleUrls:['../../../node_modules/datatables.net-dt/css/jquery.dataTables.css'],
	encapsulation: ViewEncapsulation.None
})

But I got this error:

ERROR in ./~/to-string-loader/src/to-string.js!./~/css-loader!./~/datatables.net-dt/css/jquery.dataTables.css
Module build failed: /Users/marnick/dev/starter-angular/node_modules/datatables.net-dt/images/sort_both.png:1
(function (exports, require, module, __filename, __dirname) { �PNG
                                                              ^
SyntaxError: Invalid or unexpected token
    at Object.exports.runInThisContext (vm.js:73:16)

Apparently this is related to the loader used in webpack. I solved this by installing

npm install css-to-string-loader --save-dev

And changing the following in webpack.common.js:

{
  test: /\.css$/,
  use: ['to-string-loader', 'css-loader']
},

to

{
  test: /\.css$/,
  use: ['css-to-string-loader', 'css-loader']
},

Now my datatable is styled, but still lacking interactivity.

@marnickmenting
Copy link
Author

marnickmenting commented Feb 23, 2017

When trying the AJAX-example I get the error:

Error: Uncaught (in promise): Error: Template parse errors:
Can't bind to 'dtOptions' since it isn't a known property of 'datatable'. ("

edit: I found out that in the dist folder of version 2.1.0 the @input() and selector:'[datatable]' lines are missing in angular-datatables.directive.ts. But they are present in the src folder of this repository. When I replace them, it works!

@l-lin
Copy link
Owner

l-lin commented Feb 23, 2017

Mmmh weird... Looking at the dist file, Input is defined in https://github.com/l-lin/angular-datatables/blob/v2.1.0/dist/angular-datatables.directive.js#L19

Can you show me the file that fixed your issue?

@marnickmenting
Copy link
Author

@l-lin
Copy link
Owner

l-lin commented Feb 23, 2017

@marnickmenting marnickmenting changed the title Getting it working with webpack.js Getting it working with webpack Feb 24, 2017
@marnickmenting
Copy link
Author

marnickmenting commented Mar 1, 2017

What I did for now is add the line

import { DataTablesModule } from '../../../node_modules/angular-datatables/dist/angular-datatables';

to my custom Module. This works, so I guess Angular is importing the .js files instead of the .ts files. Previously I was directly importing the .ts files from /dist/ and that's where I got the errors.

@Newan
Copy link

Newan commented Mar 8, 2017

same here, with ng2-admin.

Can't bind to 'dtOptions' since it isn't a known property of 'datatable'. ("

@marnickmenting
Copy link
Author

marnickmenting commented Apr 18, 2017

With version 2.3.0 the above problem has gone away. So now I can just add

import { DataTablesModule } from 'angular-datatables';

I followed the steps from the docs, but now I get the following error:

ERROR TypeError: $(...).DataTable is not a function

Searching for it on the internet tells me it might be about loading jQuery twice, but I can't find where I would be doing this in webpack. I'm not experienced with that. Anyone has a clue?

@Pokerkoffer
Copy link

I am using angular-datatables together with ng2-datepicker. datatables requires jQuery to be added in the script tag section and datepicker requires jQuery to be added to the systemjs map. jQuery is loaded twice and thats why the error $(...).DataTable is not a function occurs. Skipping one jQuery inclusion results in an error, both ways. Does anyone have an idea how to fix this?

@marnickmenting
Copy link
Author

marnickmenting commented May 29, 2017

No, I did not get this working. I keep getting the ERROR TypeError: $(...).DataTable is not a function message. My workaround is now to use version 2.1.0.

@burakkilic
Copy link

I'm getting the same error.

@joemoceri
Copy link

Same

@rahultokase
Copy link

Any solution to above ERROR TypeError: $(...).DataTable is not a function even i am getting the same error

@l-lin l-lin added the Angular label Nov 25, 2017
@DoanVanThuong
Copy link

I'm getting the same error.

@cesar-dick
Copy link

I was getting the same error. After a long winter, I find out that I was missing the import of the scripts in the angular-cli.json file:

"styles": [
"../node_modules/datatables.net-dt/css/jquery.dataTables.css",
"styles.scss"
],
"scripts": [
"../node_modules/datatables.net/js/jquery.dataTables.js",
],

@deneshgautam
Copy link

m getting an error
Uncaught ReferenceError: angular is not defined
at Object../node_modules/angular-datatables/dist/angular-datatables.js (angular-datatables.js:1352)
at webpack_require (bootstrap:81)
at Object../node_modules/angular-datatables/index.js (index.js:1)
at webpack_require (bootstrap:81)
at Object../src/app/app.module.ts (app.component.ts:10)
at webpack_require (bootstrap:81)
at Object../src/main.ts (environment.ts:8)
at webpack_require (bootstrap:81)
at Object.0 (storage.swaps.ts:57)
at webpack_require (bootstrap:81)

@pup548
Copy link

pup548 commented Sep 3, 2018

getting same problem as deneshgautam...any solutions?

@jiwlee0625
Copy link

Look at this: #1295

Summary: just run npm install [email protected]

@renangi
Copy link

renangi commented Sep 10, 2018

thanks jiwlee0625

@Benjith
Copy link

Benjith commented Sep 29, 2018

app.module.ts:64 Uncaught ReferenceError: DataTablesModule is not defined

@TomYule
Copy link

TomYule commented Nov 10, 2018

I'm getting the same error.

@ser789654
Copy link

getting error:

ERROR ReferenceError: $ is not defined
at angular-datatables.directive.js:42

@gladkiy
Copy link

gladkiy commented Nov 21, 2019

Try to install these packages:

npm install @types/jquery --save-dev
npm install @types/datatables.net --save-dev

@stale
Copy link

stale bot commented Dec 6, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale Used to close issues for inactivity (by bot) label Dec 6, 2020
@stale
Copy link

stale bot commented Dec 13, 2020

This issue has been closed due to inactivity. Please feel free to re-open the issue to add new inputs.

@stale stale bot closed this as completed Dec 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Angular stale Used to close issues for inactivity (by bot)
Projects
None yet
Development

No branches or pull requests