简体   繁体   English

在数据表 JQuery 和 Ajax 中,DataTable(...) 不是 function

[英]In Datatables JQuery and Ajax, DataTable(…) is not a function

I get the following error in my ASP.NET MVC project.我在 ASP.NET MVC 项目中收到以下错误。 There is a reference in the page to to the *.js file.页面中有对 *.js 文件的引用。 I tried almost every solution on the internet for this problem, I could find.我尝试了互联网上几乎所有解决方案来解决这个问题,我能找到。

From the source code of the page when it is loaded in the browser, it seems to be in correct order and loaded once:从页面在浏览器中加载时的源代码来看,似乎顺序正确,加载了一次:

<script src="/Scripts/old/Scripts/jquery-1.10.2.js"></script> <!-- I tried new and old jquery Version -->
<script src="/Scripts/bootstrap.js"></script>
<script src="/scripts/bootbox.js"></script>
<script src="/scripts/datatables/jquery.datatables.js"></script>
<script src="/scripts/datatables/datatables.bootstrap.js"></script>

The Code (index.cshtml):代码(index.cshtml):

$(document).ready(function() {
  $('#customers').DataTable()({
    ajax: {
      url: '/api/customers',
      dataSrc: ''
    },
    columns: [{
      data: "name",
      render: function(data, type, customer) {
        return "<a href='/customers/edit/" + customer.id + "'>" + customer.name + "</a>";
      }
    }, {
      data: "name"
    }, {
      data: "id",
      render: function(data) {
        return "<button class='btn-link js-delete' data-customer-id=" + data + ">Delete</button>";
      }
    }]
  });

  // More Code
});

The error, from the developer console of Chrome:来自 Chrome 的开发者控制台的错误:

Customers:79 Uncaught TypeError: $(...).DataTable(...) is not a function客户:79 未捕获类型错误:$(...).DataTable(...) 不是 function
at HTMLDocument.在 HTML 文档。 (Customers:79) (客户:79)
at fire (jquery-1.10.2.js:3062)着火(jquery-1.10.2.js:3062)
at Object.fireWith [as resolveWith] (jquery-1.10.2.js:3174)在 Object.fireWith [as resolveWith] (jquery-1.10.2.js:3174)
at Function.ready (jquery-1.10.2.js:447)在 Function.ready (jquery-1.10.2.js:447)
at HTMLDocument.completed (jquery-1.10.2.js:118)在 HTMLDocument.completed (jquery-1.10.2.js:118)

Any suggestions?有什么建议么?

OK, solved .好的,解决了 First comment was right.第一条评论是对的。

$('#customers').DataTable({ instead of $('#customers').DataTable()({ $('#customers').DataTable({而不是$('#customers').DataTable()({

You need to import those scripts before the Rendering scripts section.您需要在渲染脚本部分之前导入这些脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM