xenoterracide
xenoterracide

Reputation: 16847

Is there a way with a vaadin 8 custom layout to modify the element class attribute on a parent element?

So with a Custom Layout you basically have html. We have this in our template

                    <div id="test-kit-wrap" class="cell cell-above">
                        <div class="sub-cols-wrap">
                            <div id="test-kit" class="cell">
                                <div class="label detail-page-heading">Device/Kit</div>
                                <div location="kit" class="value">Yes</div>
                            </div>

We're using templates because of the complexity of this layout. This template currently works except that it's displaying this div when it shouldn't.

What I want to do is add a style to #test-kit-wrap that equates to display: none when a specific permission is set on the backend. However I can't see a good way to do this. Is there a way to do this?

Upvotes: 0

Views: 200

Answers (2)

xenoterracide
xenoterracide

Reputation: 16847

Here's what I ended up doing.

since #test-kit-wrap is completely getting display none, then the elements within don't actually matter.

 <div id="test-kit-wrap" class="cell cell-above">

So I changed the html from the above, to

  <div location="test-kit-wrap" id="test-kit-wrap" class="cell cell-above">

and then in my java when I don't want the contents of that element I set an empty CssLayout with the class I want, and the correct location. I suspect I could also toggle visibility.

        else {
            String locId = "test-kit-wrap";
            Layout value = new CssLayout();
            value.addStyleName( "hidden" );
            value.setId( locId );
            addComponent( value, locId );
        }

Upvotes: 1

tremendous7
tremendous7

Reputation: 751

invoke the following method passing as parameters the layout or one of the ancestors of the layout and "test-kit-wrap"

/**
 * Hides the component with given id after searching for it in the given parent.
 */
public static void hideComponentById(Component parent, String id) {
    if (null == parent) {
        return;
    }
    if (null != parent.getId() && parent.getId().equals(id)) {
        parent.setVisible(false);
        return;
    }
    if (HasComponents.class.isAssignableFrom(parent.getClass())) {
        for (Component componentChild : (HasComponents) parent) {
            hideComponentById(componentChild, id);
        }
    }
}

Upvotes: 0

Related Questions