DataTables: Uncaught TypeError: Cannot read property 'defaults' of undefined

前端 未结 4 1532
逝去的感伤
逝去的感伤 2020-12-08 03:54

When using the Bootstrap integration for DataTables, I see the following error in the console:

Uncaught TypeError: Cannot read property \'defaults\' of undefined (         


        
相关标签:
4条回答
  • 2020-12-08 04:17

    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>
    
    0 讨论(0)
  • 2020-12-08 04:25

    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!

    0 讨论(0)
  • 2020-12-08 04:30
    var datatable_jquery_script = document.createElement("script");
    datatable_jquery_script.src = "vendor/datatables/jquery.dataTables.min.js";
    document.body.appendChild(datatable_jquery_script);
    setTimeout(function(){
        var datatable_bootstrap_script = document.createElement("script");
        datatable_bootstrap_script.src = "vendor/datatables/dataTables.bootstrap4.min.js";
        document.body.appendChild(datatable_bootstrap_script);
    },100);
    

    I used setTimeOut to make sure datatables.min.js loads first. I inspected the waterfall loading of each, bootstrap4.min.js always loads first.

    0 讨论(0)
  • 2020-12-08 04:30

    <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 (:

    0 讨论(0)
提交回复
热议问题