Home > front end >  Vaadin change value from nested Layout
Vaadin change value from nested Layout

Time:01-03

I use Vaadin 14 and would know whether it is possible to report changes in the nested list to objects in the main view.

A rough example is shown in the picture. Above you can see the sum as size (here 2), if I press Delete it should change to 1. Is that possible and how?

concept

I don't have any code yet, it's a thought where I would like to have a hint about what would be possible, e.g. Observer Pattern or something, but code could look something like this code:

@Rout("")
public class MainView extends VerticalLayout {

 private List<CustomDetails> customDetails = new ArrayList<>();

 public MainView(){
  final var form = new FormLayout();
  customDetails.forEach(form::add);
  add(H1("Header"), form)
 }
}

public class CustomDetails extends Details{
 private CustomForm customForm;
 private final Service service;
 public CustomDetails(){
  customForms = new CustomForm(service.getListOfObjects());
  this.setContent(customForms)
 }
}

public class CustomForm extend FormLayout{

 private FormLayout formLayout = new FormLayout();
 private List<Object> objects = new LinkedList<>();
 public CustomForm(List<Object> list){
 
  this.objects = list;
  setUp();
  add(new Paragraph("SUM: "  list.size()), layout);
 }

 private void setUp(){
  objects.forEarch(o->{
   ....
   layout.add(...)
  })
 }
}

CodePudding user response:

Observer Pattern or something...

Yes, that is a solution. It is a lot of work and has been done before.

One such library is Beanbag.

Note: I wrote this (or rather, I started writing it a day ago).

EDIT:

As of this edit, we have the ObservableCollection interface. You can use it like so:

// You have a collection called "strings".

// Wrap it in an ObservableCollection.
final ObservableCollection<String, Collection<String>, BasicObservableCollection.Default<String, Collection<String>>> observableStrings = ObservableCollections.observableCollection(strings);

// Add a removed observer.
observableStrings.addElementRemovedObserver(observation -> System.out.println("\""   observation.getValue()   "\" was removed.");

// Remove an element.
observableStrings.remove("hello");

If you need the wrapper to have List methods, just wait until tomorrow evening EST. I'll have the code up by then and will update this post accordingly.

CodePudding user response:

In Vaadin there is an utility class Binder which is used to bind data to forms. If your use case is related to this, i.e. your so called nested layout is in fact a form and objects you refer to are data beans you want bind into that form. I recommend to study that first.

If you have list editor, I would also investigate if it fits your application to implement it with Grid or IronList/VirtualList, which is backed by DataProvider. Say you edit one item, and after saving the item, you can call dataProvider.refreshItem(item) to update the view.

  • Related