jQueryMobile:使用列切换动态地向表添加行后刷新

时间:2013-06-04 12:17:13

标签: javascript jquery-mobile html-table refresh dynamically-generated

我对jQueryMobile“列切换表模式”有疑问。

通过Javascript动态添加行后,切换出错。并不是说它根本不起作用,但它以某种方式混淆,交换列或类似的奇怪行为。

我完全清楚这种情况确实存在“刷新”-method ,但它在我的示例中不起作用。我也看了How to refresh JQuery mobile table after a row is added dynamically,但它并不适用于我的问题。我发现的唯一其他类似问题是旧的,与JQM的版本< = 1.3.0有关,当时没有刷新方法。

我有这张桌子

<table data-role="table" id="table" data-mode="columntoggle" class="ui-body-d ui-shadow table-stripe ui-responsive" data-column-btn-theme="b" data-column-btn-text="Columns to display..." data-column-popup-theme="a">
            <thead>
                <tr class="ui-bar-d">
                    <th data-priority="1">#</th>
                    <th data-priority="1">Data Code</th>
                    <th>Data Name</th>
                    <th>Value</th>
                    <th data-priority="1">Minimum</th>
                    <th data-priority="1">Maximum</th>
                </tr>
            </thead>
            <tbody>
            ...
            </tbody>
</table>

这个更新它的Javascript代码:

for (var i = 0; i < rows.length; i++) {
    html = html + "<tr>\n";
    for (var j = 0; j < rows[i].length; j++) {
        html = html + "<td>" + rows[i][j] + "</td>\n";
    }
    html = html + "</tr>\n\n";
}

$("#table > tbody").append(html);
$("#table").table("refresh");


请查看此JSFiddle,了解我的问题的最小化但是工作演示:http://jsfiddle.net/kkppd/


如果您尝试使用JSFiddle,请将其与我的发现进行比较:

  1. “运行”代码。该网页应该显示一些在HTML中硬编码的样本数据。列正确切换。
  2. 单击触发更新的按钮,类似于在原始应用程序中自动更新的方式。这会清空表并重新添加相同的内容。然后我调用JQM的refresh-method。
  3. 该表格与以前一样 - 但请尝试“切换列”按钮: “最大”切换“最小”,“最小”切换“#”,“#”切换“数据代码”等。
  4. 我做错了什么?

2 个答案:

答案 0 :(得分:7)

我遇到了同样的问题。在对JQueryMobile代码进行了一些黑客攻击后,我发现了这个解决方法:

$("#table > tbody").append(html);
$("#table").table("refresh");

// Code to add
var columnIndex = 0;
$("#table-popup fieldset").find("input").each(function(){
     var sel = ":nth-child(" + (columnIndex + 1) + ")";
     $(this).jqmData("cells", $("#table").find("tr").children(sel));
     columnIndex++;
});

答案 1 :(得分:1)

看来这是jquery移动表刷新方法中的错误。在您的jsfiddle中,您只需调用表刷新而不更改表数据,问题仍会显示出来。我刚刚尝试使用1.4.0 alpha版本,看起来他们修复了它。