DataTables.js未加载

时间:2015-05-12 10:06:02

标签: javascript jquery asp.net-mvc jquery-datatables

在我的网络MVC应用程序上使用DataTables.js。 这是我的链接:

<link href="@Url.Content("~/Content/datatables/jquery.dataTables.css")" rel="stylesheet" type="text/css" />
@Scripts.Render("~/bundles/jquery");
<script type=" text/javascript" src="@Url.Content("/Scripts/datatables/jquery.dataTables.min.js")"></script>
<script type="text/javascript" src="@Url.Content("/Scripts/datatables/tables/table.js")"></script>

我无法理解,但我的jQuery库加载两次(2次我捕获断点),jquery.dataTables.min.js - 没有加载。 然后我在浏览器中检查了源代码 - 一切正确 - js文件到位。

我在尝试显示网格时收到错误消息:

var dataSet = [
    ['Trident', 'Internet Explorer 4.0', 'Win 95+', '4', 'X'],
    ['Trident', 'Internet Explorer 5.0', 'Win 95+', '5', 'C'],
    ['Trident', 'Internet Explorer 5.5', 'Win 95+', '5.5', 'A'],
    ['Trident', 'Internet Explorer 6', 'Win 98+', '6', 'A'],
    ['Trident', 'Internet Explorer 7', 'Win XP SP2+', '7', 'A'],
    ['Trident', 'AOL browser (AOL desktop)', 'Win XP', '6', 'A']
];

$(document).ready(function () {
    $('#customTable').html('<table cellpadding="0" cellspacing="0" border="0" class="display" id="example"></table>');

    $('#example').dataTable({
        "data": dataSet,
        "columns": [
            { "title": "Engine" },
            { "title": "Browser" },
            { "title": "Platform" },
            { "title": "Version", "class": "center" },
            { "title": "Grade", "class": "center" }
        ]
    });
});

未捕获的TypeError:$(...)。DataTable不是函数

PS:我的边界脚本:

bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                        "~/Scripts/jquery-{version}.js"));

            bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(
                        "~/Scripts/jquery-ui-{version}.js"));

            bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                        "~/Scripts/jquery.unobtrusive*",
                        "~/Scripts/jquery.validate*"));

            // Use the development version of Modernizr to develop with and learn from. Then, when you're
            // ready for production, use the build tool at http://modernizr.com to pick only the tests you need.
            bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                        "~/Scripts/modernizr-*"));

            bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/site.css"));

            bundles.Add(new StyleBundle("~/Content/themes/base/css").Include(
                        "~/Content/themes/base/jquery.ui.core.css",
                        "~/Content/themes/base/jquery.ui.resizable.css",
                        "~/Content/themes/base/jquery.ui.selectable.css",
                        "~/Content/themes/base/jquery.ui.accordion.css",
                        "~/Content/themes/base/jquery.ui.autocomplete.css",
                        "~/Content/themes/base/jquery.ui.button.css",
                        "~/Content/themes/base/jquery.ui.dialog.css",
                        "~/Content/themes/base/jquery.ui.slider.css",
                        "~/Content/themes/base/jquery.ui.tabs.css",
                        "~/Content/themes/base/jquery.ui.datepicker.css",
                        "~/Content/themes/base/jquery.ui.progressbar.css",
                        "~/Content/themes/base/jquery.ui.theme.css"));

我的错误在哪里?

1 个答案:

答案 0 :(得分:1)

您的datatables js&amp; css文件应该在一个包中作为初学者:

bundles.Add(new ScriptBundle("~/bundles/datatables").Include(
             "~/Scripts/datatables/jquery.dataTables.min.js"));

datatables js包添加到您的页面中,如下所示:

@section Scripts {
    @Scripts.Render("~/bundles/datatables")
}

为什么要动态添加表格?最好只将它添加到View中,以便在js加载之前将其添加到DOM中:

<table cellpadding="0" cellspacing="0" border="0" class="display" id="example"></table>

以下是jsFiddle

上代码的工作示例