【发布时间】:2014-05-25 16:00:40
【问题描述】:
这个 jquery 移动表正在被淘汰。
<table data-role="table" id="report-table" class="ui-body-a ui-shadow ui-responsive table-stripe"
data-column-btn-theme="a" data-column-btn-text="Spalten..." data-column-popup-theme="a" data-mode="columntoggle"">
<thead>
<tr data-bind="foreach: columns">
<th data-bind="text: $data.Caption, attr: { 'data-priority': 1 + Math.floor($index() / 4) }"></th>
</tr>
</thead>
<tbody data-bind="foreach: { data: rows, afterRender: tableRowAfterRender }">
<tr data-bind="foreach: $parent.columns">
<!-- ko template: { name: $data.template } -->
<!-- /ko -->
</tr>
</tbody>
</table>
为了让“columntoggle”真正起作用,我目前使用“aferRender”事件:
self.tableRowAfterRender = function (element, data) {
// Skip unless data.Items()[i] is not the last element in the rows collections
for (var i = 0; i < data.Items().length - 1; i++) {
if (data.Items()[i] !== self.rows()[self.rows().length - 1].Items()[i])
return;
}
// refresh table after 100ms delay
setTimeout(function () { $("#report-table").table("refresh"); }, 100);
}
这很不稳定,我讨厌 setTimeout() 做事的方式,这种情况在我使用 jquery mobile 和淘汰赛时变得很常见。一旦所有的淘汰赛渲染或理想情况下与表格元素内的元素相关的所有渲染完成后,我需要一种强大的方法来引发事件。在某些此类情况下,我能够使用自定义绑定,但我不知道如何在此处执行此操作。
【问题讨论】:
-
@akhlesh:我看到了那个帖子(甚至在那里发表了评论),它是关于(或至少看起来很满意)在一个 foreach 呈现时获得一个事件。我在示例中使用了建议的技术。但这对嵌套的 foreach 甚至不相关的 foreach(例如我的示例中的 cols 和 rows foreach)没有帮助。