Knockout “if” binding doesn't work with expression in a sortable list?

房东的猫 提交于 2019-12-07 00:05:53

问题


I just run into another problem with my To-do-like sortable list made with Knockout and Knockout-sortable plugin.

I need to put a red delimiter under the element placed in the current time and cancel the sort if a element is dropped before that delimiter.

I tried with a "visible" binding and it works in some way, but the visibile binding just hide the DOM element and it mess up the sortable arrayIndex, adding unnecessary element to it.

<div class="delimiter" data-bind="visible: time() == $root.limit()"></div>

The "if" binding would be better because it insert DOM element only if necessary, but the expression I used with visible is always evaluated to true and I can't figure out why...

<div class="delimiter" data-bind="if: time() == $root.limit()"></div>

Here is the fiddle: http://jsfiddle.net/ingro/VaqqF/

Any help is appreciated, thank you!


回答1:


You've simply misunderstood the if-binding: It removes the content of the node it has been applied to, not the node itself. If you want to remove a node without creating a wrapper around it (that you can use to add the binding), there is also the comment-version of the if-binding, called the containerless control flow syntax:

<!-- ko if: time() == $root.limit() -->
  <div class="delimiter"></div>
<!-- /ko -->

http://jsfiddle.net/VaqqF/11/

Ref: http://knockoutjs.com/documentation/if-binding.html



来源:https://stackoverflow.com/questions/11031057/knockout-if-binding-doesnt-work-with-expression-in-a-sortable-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!