我已经查看了许多示例,但没有一个与我尝试做的完全相同。
我所拥有的基本上可以工作,但它并不完全正确。
这里有一个演示来说明问题。
注意顶部的设置可以正常工作,但是它是静态定义的。底部的设置(Tom、Steve、Bob)“基本上”可以工作,但标题元素最终既出现在可折叠的标题中,也出现在被隐藏的可折叠部分中。
似乎我必须做错了什么,但我还没有找出来。
有什么想法吗?
我已经查看了许多示例,但没有一个与我尝试做的完全相同。
我所拥有的基本上可以工作,但它并不完全正确。
这里有一个演示来说明问题。
注意顶部的设置可以正常工作,但是它是静态定义的。仅供参考,对于任何遇到此问题的人,后来发现这至少在事后看起来有些显然。
Knockout内置的“匿名”模板在许多情况下都很好用,但是在JQMobile中,它可能有些古怪。
这是因为JQMobile会在页面加载时调整匿名模板的内容,就像它处理所有其他内容一样。
然后,稍后当您使用knockout的ApplyBindings函数时,knockout将添加适用的元素,就像它应该做的那样。正如许多帖子和答案所暗示的那样,您必须通过类似以下方式对新创建的元素调用collapsible()。
$("div[data-role='collapsible']").collapsible({refresh: true});
没有任何问题。然而,如果JQM已经应用了格式,则匿名模板已经被JQM“渲染”,因此通过调用可折叠的方式再次呈现它会导致所有种类的奇怪结果,包括重复标题、嵌套可折叠等。
对我来说解决方案是使用Knockout的“命名模板”功能,只需将用于呈现可折叠元素的模板放入一个<script type="text/html">
标签中,像这样:
<script type="text/html" id="alarm-template"></script>
$("div[data-role='collapsible-set']").collapsibleset('refresh');
Set up your foreach binding as you normally would for me it looked like this
<div data-bind="foreach: promotions">
<h3 data-bind="text: Title"></h3>
<p>Creator:<span data-bind="text: Creator"></span></p>
<p>Effective Date:<span data-bind="text: EffectiveDate"></span></p>
<span data-bind="text: Description"></span>
<a data-bind="text: ButtonText, attr: {href: ButtonLink}"></a>
Wrap that in a div with class="collapsible like so
<div data-role="collapsible-set" data-bind="foreach: promotions">
<div class="collapsible">
<h3 data-bind="text: Title"></h3>
<p>Creator:<span data-bind="text: Creator"></span></p>
<p>Effective Date:<span data-bind="text: EffectiveDate"></span></p>
<span data-bind="text: Description"></span>
<a data-bind="text: ButtonText, attr: {href: ButtonLink}"></a>
Apply the collapsible widget via jquery mobile after you do your binding like so:
$(document).ready(function () {
ko.mapping.fromJS(data, dataMappingOptions, PromotionViewModel.promotions);
ko.applyBindings(PromotionViewModel);
$('.collapsible').collapsible();
});
For a collapsible set the same idea can be applied just set the class="collapsible-set" on your foreach div. Hope this helps