簡體   English   中英

在數據表 JQuery 和 Ajax 中,DataTable(...) 不是 function

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

我在 ASP.NET MVC 項目中收到以下錯誤。 頁面中有對 *.js 文件的引用。 我嘗試了互聯網上幾乎所有解決方案來解決這個問題,我能找到。

從頁面在瀏覽器中加載時的源代碼來看,似乎順序正確,加載了一次:

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

代碼(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
});

來自 Chrome 的開發者控制台的錯誤:

客戶:79 未捕獲類型錯誤:$(...).DataTable(...) 不是 function
在 HTML 文檔。 (客戶:79)
着火(jquery-1.10.2.js:3062)
在 Object.fireWith [as resolveWith] (jquery-1.10.2.js:3174)
在 Function.ready (jquery-1.10.2.js:447)
在 HTMLDocument.completed (jquery-1.10.2.js:118)

有什么建議么?

好的,解決了 第一條評論是對的。

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

您需要在渲染腳本部分之前導入這些腳本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM