0

I'm new to Electron and currently playing with the electron-api-demos. I'm trying to use the plug-in Datatables.net (and by my searches, I'm not the first one struggling!), both packages has been installed as dependencies (I can see them in package.json)

I had some difficulties with jQuery but after a few searches, manage to make it work using this line on the page I want it:

<script>window.$ = window.jQuery = require('./node_modules/jquery/dist/jquery')</script>

Then I tried many ways to add the Datatables plugin but always ending up with errors. Currently, it's a line after the one with jQuery, then the custom script:

<script>require('./node_modules/datatables.net/js/jquery.dataTables')</script>
<script>require('./renderer-process/foo/bar')</script>

In the custom script, I'm trying to transform a table in datatable:

$(function () {
  $('#datatable-bar').DataTable();
});

I end up with one warning and one error:

jQuery.Deferred exception: $(...).DataTable is not a function TypeError: $(...).DataTable is not a function
Uncaught TypeError: $(...).DataTable is not a function

I checked questions here on Stack Overflow, on the forum of Datatables.net and other websites. Two basics answer are:

  • jQuery is loaded after the plug-in (not my case I reckon)
  • jQuery is loaded twice (how could I verify that possibility?)

Help and/or any other suggestions are welcomed!

Claire
  • 773
  • 1
  • 8
  • 19

1 Answers1

1

OK, shame on me, I should have just read the documentation more thoroughly. I had to post a question here (after half a day of research) to find the right answer! But it might help others as I stumbled on the same kind of problem on a lot of different forum.

So if you check the README file of the datatables.net package, you've got the answer:

<script>
  var $ = require( 'jquery' );
  require( 'datatables.net' )( window, $ );
</script>

Is the way of making the plug-in works without any more errors or lines to call them as I have see on some answers. It's loading jQuery and the Datatables script. It's working like a charm now!

I reckon that page can be useful too: https://datatables.net/download/npm

Claire
  • 773
  • 1
  • 8
  • 19
  • I did this, and am getting this error: Uncaught ReferenceError: require is not defined Any idea on what i can do? – Toni May 01 '19 at 11:04
  • I'm not sure but this thread might help: https://stackoverflow.com/questions/23603514/javascript-require-function-giving-referenceerror-require-is-not-defined Because your error is with the function require(). – Claire May 02 '19 at 15:47