javascript - 在 Datatables JQuery 和 Ajax 中,DataTable(...) 不是函数
问题描述
我在我的 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(...) 不是
HTMLDocument 中的函数。(客户:79)
在
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()({
解决方案
您需要在渲染脚本部分之前导入这些脚本。
推荐阅读
- c# - SignalR 2 StartAsync 永远不会返回
- wpf - application.xaml 中的 WPF ControlTemplate Datatrigger
- sql - 计算每天 Ms-Sql 总行中的最大连续行
- laravel - Laravel 资源控制器在 amchor 点击时没有删除路由
- javascript - 根据 Javascript 中的模式在数组中查找值
- c - 为什么这两个函数不能将十进制转换为二进制(uint8_t)?
- node.js - 如何在新的包名字对象规则之后检查 npm 包名称的可用性?
- string - 在 Groovy 中将字符串转换为 InputStream
- android - KeyguardManager - api 25 及更低版本的 requestDismissKeyguard() 方法替代 [Android]
- angular - angular 6(无法确定 @angular/compiler-cli 和 typescript 的版本)