javascript - Jquery DataTables Live DOM 排序在复选框列上不起作用

标签 javascript jquery html datatables

我的 .NET Core 应用程序中有一个数据表,其中有一个复选框列。我希望能够使用表标题中的箭头按钮按复选框值对表进行排序/排序。为了实现这一点,我使用了 DataTables Live DOM Ordering 示例中的代码:https://datatables.net/examples/plug-ins/dom_sort.html

但是,列重新排序不起作用。如果我检查元素,我可以看到元素闪烁时发生了一些事情,但是行的顺序没有改变。

这是 HTML:

    <table id="airportsTable" class="table table-striped table-bordered dt-responsive nowrap" width="100%" cellspacing="0">
        <thead>
            <tr>
                <th>IsoCode</th>
                <th>Name</th>
                <th>TimezoneName</th>
                <th>TimezoneStandardName</th>
                <th>
                    Required
                    <input type="checkbox" id="selectAllCheckbox" value="false" />
                </th>
            </tr>
        </thead>
        <tbody>
            @{ foreach (var airport in airportList) //Index , update after iteration, so that non-model data can be attached
                {
                    string iso = airportList[tableIndex].IsoCode; 
                    <tr>
                        <td>
                            <label id="isoCode">@airportList[tableIndex].IsoCode</label>
                        </td>
                        <td>
                            <label id="name">@airportList[tableIndex].Name</label>
                        </td>
                        <td>
                            <label id="timeZoneName">@airportList[tableIndex].TimeZoneName</label>
                        </td>
                        <td>
                            <label id ="timeZoneStandardName">@airportList[tableIndex].TimeZoneStandardName</label>
                        </td>
                        <td>
                            <input type="checkbox" asp-for="@airportList[tableIndex].Required" id="requiredCheckbox" onclick="requiredChanged(this, '@airportList[tableIndex].IsoCode' )" />
                        </td>
                    </tr>

                    tableIndex++;
                }
            }
        </tbody>
    </table>

这是 JQuery:

$.fn.dataTable.ext.order['dom-checkbox'] = function  ( settings, col )
    {
        return this.api().column( col, {order:'index'} ).nodes().map( function ( td, i ) {
            return $('input', td).prop('checked') ? '1' : '0';
        } );
    }

    $(document).ready(function () {
         var airportsTable = $('#airportsTable').DataTable({
            "pageLength": 50,
            "columns": [
                null,
                null,
                null,
                null,
                { "orderDataType": "dom-checkbox" }
            ]
        });

任何帮助将不胜感激,TIA!

最佳答案

将其添加到表格的列部分之后:

“columnDefs”:[ { “orderDataType”:“dom-checkbox”,目标:5 } ]

https://datatables.net/reference/option/columns.orderDataType

关于javascript - Jquery DataTables Live DOM 排序在复选框列上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54180373/

相关文章:

html - 如何卡住表列

javascript - 如何使用javascript或jquery检查元素的父元素本身是否是最后一个子元素

javascript - 根据算术级数动态分配高度

javascript - Primefaces、JavaScript 和 JSF 不能很好地协同工作,或者我做错了什么

javascript - 触摸不适用于 Twitter Bootstrap 模板中的移动浏览器

javascript - 使用 jQuery.siblings() 找不到 &lt;header&gt; 标记

Javascript/Jquery - 如何替换一个单词但仅当它不是另一个单词的一部分时?

javascript - 如何识别所选对象的类型?

javascript - 协助将jquery捆绑到js变量中以进行条件输出

javascript - 第 n 个类型元素的 css 选择器 - nth-of-type() 的奇怪实现