【问题标题】:checkbox cannot retain its state after applying table sorter应用表格排序器后复选框无法保留其状态
【发布时间】:2012-11-06 20:20:41
【问题描述】:

我在 html 表中有一个复选框。 使用 knockoutjs,我将我的 html 表绑定到 json 对象。直到现在一切正常。 但是当我应用 tablesorter 时,之前选中的复选框会被取消选中。 它发生在从下面列出的代码中调用 Buildtable() 函数之后。 我使用的浏览器是IE6。不确定它是否是一个更严重的问题。目前无权访问任何其他浏览器。 任何帮助将不胜感激。

谢谢

    <div id="unassignedDiv" style="text-align:center;display:none;">
    <table class="tablesorter" id="unassignedTable">
    <thead><tr>
    <th align="center">Date</th>
    <th align="center">Rush?</th>
    </thead></tr>
    <tbody id="resultsbody" data-bind="template: { name: 'resultsTemplate', foreach: Results }"></tbody></table>

    <script id="resultsTemplate" type="text/html">
    <tr><td data-bind="text: dateneeded" align="center"></td>
    <td align="center">
    <input type="checkbox" data-bind="checked:rushindicator" disabled="disabled" />
    </td>
    </tr>
    </script>
    </div>

    //Build JsonObject
    BuildArray = function () {
           var searchjson = {
                "Results": [
                   { "dateneeded": "11/08/12", rushindicator: true },
                   { "dateneeded": "11/10/12", rushindicator: false }]};
    };


    BuildResultsPage = function () {
            $j('#unassignedDiv').show();
            var resultArray = BuildArray();
            exported.viewmodelExpanded = ko.mapping.fromJS(resultArray);
            ko.applyBindings(exported.viewmodelExpanded, $j('#unassignedDiv')[0]);        
            BuildTable(); //If this is commented, html loads checkbox with checked.
        };

    BuildTable = function () {
            $j("#unassignedTable").tablesorter({ widgets: ['zebra'], widgetZebra: { css: ["oddcolor", "evencolor"] },
                sortInitialOrder: 'desc',
                headers:
                {
                  0: { sorter: 'Date' },
                  1: { sorter: false }
                }
            }).tablesorterPager({ container: $j("#pager"), removeRows: true });
    };

【问题讨论】:

    标签: jquery html knockout.js tablesorter


    【解决方案1】:

    如果您使用的是原始表格排序器,请尝试this answer中的代码。*

    如果你使用我的fork of tablesorter,请使用这个解析器(demo):

    $.tablesorter.addParser({
        id: 'checkbox',
        is: function(s) {
            return false;
        },
        format: function(s, table, cell, cellIndex) {
            var $t = $(table), $c = $(cell), c,
    
            // resort the table after the checkbox status has changed
            resort = false;
    
            if (!$t.hasClass('hasCheckbox')) {
                $t
                .addClass('hasCheckbox')
                // make checkbox in header set all others
                .find('thead th:eq(' + cellIndex + ') input[type=checkbox]')
                .bind('change', function(){
                    c = this.checked;
                    $t.find('tbody tr td:nth-child(' + (cellIndex + 1) + ') input').each(function(){
                      this.checked = c;
                      $(this).trigger('change');
                    });
                })
                .bind('mouseup', function(){
                    return false;
                });
                $t.find('tbody tr').each(function(){
                    $(this).find('td:eq(' + cellIndex + ')').find('input[type=checkbox]').bind('change', function(){
                        $t.trigger('updateCell', [$(this).closest('td')[0], resort]);
                    });
                });
            }
            // return 1 for true, 2 for false, so true sorts before false
            c = ($c.find('input[type=checkbox]')[0].checked) ? 1 : 2;
            $c.closest('tr')[ c === 1 ? 'addClass' : 'removeClass' ]('checked');
            return c;
        },
        type: 'numeric'
    });
    
    $(function() {
        $('table').tablesorter({
            headers: {
                0: { sorter: 'checkbox' }
            }
        });
    });​
    
    • 此解析器无法与原始插件一起使用的原因是格式函数参数 - cellIndex 不可用。

    【讨论】:

    • 感谢您的回复。我正在使用原始 tablesorter 并按照链接中给出的方式进行操作。它没有用。而且我没有用复选框对我的列进行排序。这仅在 IE6 上发生。我在 chrome 上测试了相同的代码,它没有任何问题。
    • 啊,好吧,我有点困惑,因为我的 fork 中提供了寻呼机 removeRows 选项,但不是原始插件的一部分。您能否提供代码演示或显示生成的 HTML 示例。这很令人困惑,因为模板没有结束 &lt;/tr&gt; 也没有结束 &lt;/script&gt;
    • @Mottie...我已经更新了问题中的 html 和 knockoutjs 脚本,为 tr 和 script 添加了结束标签。我无法添加我的 html 结果的任何图像。
    • 我对结果表 HTML 的样子很感兴趣。是否所有输入都保持禁用状态?还有 BuildResultsPage 函数是如何调用的,它是否被包装在一个文档就绪函数中?
    • 我不知道如何将我的 html 共享为图像。对于那个很抱歉。 HTML 将具有复选框,并根据其绑定的值正确检查。但是在调用 BuildTable() 之后,生成的 HTML 表将在该列中取消选中所有复选框,而不管它绑定到的值如何。是的,BuildResultsPage() 是从文档就绪函数调用的。
    猜你喜欢
    • 2016-12-19
    • 2022-01-17
    • 1970-01-01
    • 2020-06-11
    • 1970-01-01
    • 1970-01-01
    • 2018-03-10
    • 1970-01-01
    • 2016-10-13
    相关资源
    最近更新 更多