You are adding a Span containing number of notifications to a Button: bellBtn.getElement().appendChild(numberOfNotifications.getElement());
In the javadoc for .getElement() you stated:
If you see a direct call to this method in your applications UI code, you should consider that as a sign that you are probably doing something wrong and you should instead use other methods from your component
And I am wondering how should we tackle this without .getElement() and .append() for those components.
I would like this span to increment/decrement when the number of items changes, respectively to disappear when it is 0.
What do you recommend in this case?
I’m a bit confused, Button class does not have an add() method.
Were you thinking about adding both the button and span to a HorizontalLayout? Or adding the button to the span and treating the span as “the button”?
I implemented it that way on my side but I got an issue when trying to remove the Span from the Button. The case when there are no more notifications and the Span must disappear.
I don’t see any method on the Button to update the whole list of children Elements.
I was thinking about getting the children with new ArrayList<>(button.getElement().getChildren()) as Elements or new ArrayList<>(notificationsButton.getChildren().toList()) as Components, remove the Span by className, put the list back and refresh the UI.
I also tried to set visibility for the span to false, but seems to break the button on refresh (and sometimes the whole UI).
For context, I have a NotificationButton which opens a Dialog containing a grid with notifications.
I declared both the button and span at class level and attached the span during view + hidden grid refresh. I check the number of notifications using the service and make the span visibility false when it returns 0 and true when > 0 (and set the span value). I have a broadcaster which fires grid refresh events when the repository saves/deletes notifications.
It worked as expected, finally