DataTables: Uncaught TypeError: Cannot read property 'defaults' of undefined
When using the Bootstrap integration for DataTables, I see the following error in the console:
Uncaught TypeError: Cannot read property 'defaults' of undefined (dataTables.bootstrap.js:20)
This causes the pagination controls to not have styles on them.
I can see that in the factory initialization, the following code needs to run:
factory( jQuery, jQuery.fn.dataTable );
However, jQuery.fn.dataTable
is returning undefined
.
The problem is that dataTable
is not defined at the point you are calling this method.
Ensure that you are loading the .js
files in the correct order:
<script src="/Scripts/jquery.dataTables.js"></script>
<script src="/Scripts/dataTables.bootstrap.js"></script>
I got the same error, I'm using laravel 5.4 with webpack, here package.json before:
{
...
...
"devDependencies": {
"jquery": "^1.12.4",
...
...
},
"dependencies": {
"datatables.net": "^2.1.1",
...
...
}
}
I had to move jquery
and datatables.net
npm packages under one of these "dependencies": {}
or "devDependencies": {}
in package.json
and the error disappeared, after:
{
...
...
"devDependencies": {
"jquery": "^1.12.4",
"datatables.net": "^2.1.1",
...
...
}
}
I hope that helps!
<script src="https://cdn.datatables.net/1.10.22/js/jquery.dataTables.min.js"defer</script>
Add Defer to the end of your Script tag, it worked for me (;
Everything needs to be loaded in the correct order (: