0

Is there a best-practice with knockout.js and/or CSS to prevent the extra divs that get created inside a foreach loop from displaying their extra whitespace height in the browser?

Source:

<div data-bind="foreach: jobs" >
  <div data-bind="if: JobPhase.Id() == 3">
    <div data-bind="text: JobPhase.Id"></div>                            
  </div>
</div>

Result:

<div data-bind="if: JobPhase.Id() == 3"></div>
<div data-bind="if: JobPhase.Id() == 3"></div>
<div data-bind="if: JobPhase.Id() == 3"></div>
<div data-bind="if: JobPhase.Id() == 3">
  //This one matched so it will display the content.
</div>

The first three items did not match but I'm still seeing their whitespace. Thoughts?

1
  • I actually think it was something else that was causing it. Commented May 19, 2012 at 3:53

1 Answer 1

3

In your solution divs will be rendered, and will be rendered empty, that is not really good practice. You should use another "if" statement. Like in Example.

<div data-bind="foreach: jobs" >  
  <!-- ko if: Id == 3 -->  
    <div data-bind="text: Id"></div>                         
  <!-- /ko-->
</div>

So you will be able to create div blocks only if you need them, instead of creating them all the time.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.