Can't use DataTables in Symfony4 (Yarn)

Can't use DataTables in Symfony4 (Yarn)

BrokhaelBrokhael Posts: 2Questions: 1Answers: 0
edited October 2018 in Free community support

I've followed the steps to install DataTables in my project, but when I use it it displays this error in the console:

Uncaught TypeError: Cannot set property '$' of undefined
  at DataTable (jquery.dataTables.js:132)
  at Object../assets/js/app.js (app.js:4)
  at __webpack_require__ (bootstrap b5a95e352a7377e994b5:19)
  at ./assets/css/app.css (bootstrap b5a95e352a7377e994b5:62)
  at bootstrap b5a95e352a7377e994b5:62

This is my app.js file:

const $ = require('jquery');
require('../css/app.css');
require('bootstrap');
require('datatables.net-dt')(window, $);

and this is how I use DataTable:

<table id="tabla" class="display">
        <thead>
        <tr>
            <th>Column 1</th>
            <th>Column 2</th>
        </tr>
        </thead>
        <tbody>
        <tr>
            <td>Row 1 Data 1</td>
            <td>Row 1 Data 2</td>
        </tr>
        <tr>
            <td>Row 2 Data 1</td>
            <td>Row 2 Data 2</td>
        </tr>
        </tbody>
    </table>
$('#tabla').DataTable();

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi @Brokhael ,

    Looking at this thread here, it looks like you also need datatables.net - datatables.net-dt is for the styling.

    Hope that does the trick,

    Cheers,

    Colin

  • BrokhaelBrokhael Posts: 2Questions: 1Answers: 0

    Nope, it's not working, smae error.

  • allanallan Posts: 63,464Questions: 1Answers: 10,466 Site admin

    I don't actually see what would cause that issue in your code I'm afraid. Line 132 is this.$ = function ( sSelector, oOpts ) - so the error suggests that this is undefined. I'm not at all sure what would cause that!

    Can you link to a page showing the issue so I can debug it directly, or create a repo with a test case in it please.

    Allan

This discussion has been closed.