How to show/hide div in WinJS Template dynamically
Asked Answered
G

1

9

I have a Windows 8 app with a template that contains a div I want to show or hide based on the value of a property inside a data-win-control="WinJS.Binding.Template". I have tried the following without luck:

<div data-win-bind="visible: isMore"> ..content... </div>

where isMore is a boolean property of the databound item.

How can I do that? I guess the visible property does not exist?

Gastrointestinal answered 17/5, 2012 at 6:28 Comment(0)
L
14

You are right - the visible property doesn't exist, but you can control the appearance using CSS and a binding converter.

First, use WinJS.Binding.converter to create a converter function that translates a boolean to a value value for the CSS display property, like this:

var myConverter = WinJS.Binding.converter(function (val) {
    return val ? "block" : "none";
});

Make sure that the function is globally available - I use WinJS.Namespace.define to create collections of these converters that I can get to globally.

Now you can use the converter in your data binding to control the CSS display property, like this:

<div data-win-bind="style.display: isMore myConverter"> ..content... </div>
Lectra answered 19/5, 2012 at 16:45 Comment(2)
even better would be to set the display style to "initial" instead of "block" so it can be used for span elements too.Nixie
I use display:none property.. data removed from the element but element with blank display exist there... It takes size of first row by default.Ligialignaloes

© 2022 - 2024 — McMap. All rights reserved.