【问题标题】:Jquery Datatable - selector throws table/row undefinedJquery Datatable - 选择器抛出未定义的表/行
【发布时间】:2017-09-14 12:51:36
【问题描述】:

我已经寻找解决方案,但我并不清楚它不起作用的原因。你能帮帮我吗?

这是我的代码,它会抛出 Table undefined 错误,当我将 var data = window.table.row($(this)).data() 更改为 var data = window.table.row($(this)).data() 时,错误会更改为

0x800a138f - JavaScript 运行时错误:无法获取属性“行” 未定义或空引用

<hr />
<div class="row">
    <div class="col-xs-12">
        <table class="table table-striped table-bordered nowrap" id="calculation-table">
            <thead>
                <tr>
                    <td>Beneficial Owner</td>
                    <td>Account</td>
                    <td>Country</td>
                    <td>Currency</td>
                    <td>Year</td>
                    <td>Updater</td>
                    <td>Updated</td>
                    <td>Output</td>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <th>Beneficial Owner</th>
                    <th>Account</th>
                    <th>Country</th>
                    <th>Currency</th>
                    <th>Year</th>
                    <th>Updater</th>
                    <th>Updated</th>
                    <th>Output</th>
                </tr>
            </tbody>
        </table>
    </div>
</div>
<script type="text/javascript">
    $(document).ready(function () {
        //Calculation.init();
        $('#calculation-table').DataTable(
            { // set server side processing to true
                bServerSide: true,
                // set controller responsible for sorting and paging
                sAjaxSource: "CalculationTest/AjaxHandler",
                // show processing is happening while getting data
                bProcessing: true,
                sPaginationType: 'full_numbers',
                iDisplayLength: 15,
                lengthMenu: [[15, 25, 50, -1], [15, 25, 50, "All"]],
                columns: [
                    { data: "BeneficialOwner" },
                    { data: "Account" },
                    { data: "Country" },
                    { data: "Currency" },
                    { data: "Year" },
                    { data: "Updater" },
                    { data: "Updated".toString() },
                    {
                        "targets": -1,
                        "data": null,
                        "defaultContent": "<button class='btn btn-primary'>Download File</button>"
                    }
                ],
                "language": {
                    "search": "",
                    "searchPlaceholder": "Search..."
                }
            });

        $('#calculation-table > tbody').on('click', 'button', function () {
            var data = table.row($(this)).data();
            var url = '/Calculation/DownloadCalculation';
            $.ajax({
                type: "GET",
                url: url,
                data: { id: data[5] },
                dataType: "html",
                success: function (data) {
                    //put your code here
                }
            });
        });

    });
</script>

【问题讨论】:

  • var window.table = $('#calculation-table').DataTable(... 并确保与columns 部分中的列数相匹配。

标签: jquery html datatables datatables-1.10


【解决方案1】:
<td> is for <tbody>
<th> is for <thead>

你已经在你的html中交换了

【讨论】:

  • 谢谢!,我没有注意到。我把它改成了应该的样子,但我仍然得到同样的错误
  • 你计算每行的列号了吗?数据表是非常迫切的,它希望有一个完美的表,列数相同,语法完美,没有字符错误等
  • 包含在您的 javascript 代码中,属性“列”
猜你喜欢
  • 1970-01-01
  • 2012-08-28
  • 2011-08-02
  • 1970-01-01
  • 2015-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-06
相关资源
最近更新 更多