【问题标题】:DataTable javascript does not work数据表 javascript 不起作用
【发布时间】:2017-10-24 20:06:10
【问题描述】:

我正在使用数据表对表进行排序,但是当我执行程序时,只执行存储在表中的数据;并且无法对表格进行排序。我已经在 html 中尝试过它并且它可以工作,但是当涉及到剃刀视图时无法排序。我已将此添加到 bundleconfig.cs

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

查看

    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css">
<script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>

<script type="text/javascript">

    $(document).ready(function () {
        $('#data-table').DataTable();
    });

</script>

<table id="data-table">
<thead>
    <tr>
        <th class="col-lg-3 tablehead">Item Number</th>
        <th class="col-lg-3 tablehead">Description</th>
        <th class="col-lg-3 tablehead">Quantity</th>
    </tr>
</thead>
<tbody>
    @foreach (var item in Model)
    {
        <tr>
            <td class="col-lg-3 tabledata">@item.itemNumber</td>
            <td class="col-lg-3 tabledata">@item.description</td>
            <td class="col-lg-3 tabledata">@item.quantity</td>
        </tr>
    }
</tbody>

编辑 调试后发现我在这里遇到错误 $('#data').DataTable();。错误:对象不支持属性或方法“DataTable”。

【问题讨论】:

    标签: javascript c# jquery datatable


    【解决方案1】:

    将您的代码更改为:

      <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css">
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
    
    
    <table id="data-table">
    <thead>
        <tr>
            <th class="col-lg-3 tablehead">Item Number</th>
            <th class="col-lg-3 tablehead">Description</th>
            <th class="col-lg-3 tablehead">Quantity</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td class="col-lg-3 tabledata">@item.itemNumber</td>
                <td class="col-lg-3 tabledata">@item.description</td>
                <td class="col-lg-3 tabledata">@item.quantity</td>
            </tr>
        }
    </tbody>
    </table>
    <script type="text/javascript">
    
        $(document).ready(function () {
            $('#data-table').DataTable();
        });
    
    </script>
    

    您需要在 DataTables 库之前加载 jquery 库。您还错过了结束表标记。而且我总是在 DOM 加载表后初始化数据表。

    工作小提琴here

    【讨论】:

    • 它正在产生相同的东西,我不知道发生了什么。是不是我应该在脚本文件夹中有datatables.js
    • 查看页面源代码,点击与javascript库和css文件相关的超链接。确保它们确实可用。
    • 哦等等,你错过了 标签哈哈。更新了代码以使其清晰。
    • 库和文件可用
    • 表格标签已关闭
    猜你喜欢
    • 1970-01-01
    • 2016-05-22
    • 2013-02-12
    • 1970-01-01
    • 2023-03-20
    • 2012-11-19
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    相关资源
    最近更新 更多