3

My TreeGid component shows the relation between images and Products. The relation between image and product objects is n:m (see below for a simplified example, product 60148971 is linked to at least two images).

enter image description here

When a user selects a product (see example) it is important to know its parent node. We need this information from the UI model because the product can have more than one parent. As far as I understand, this is not possible in Vaadin 8.1+. Can anyone confirm this?

My current solution is to use simple Link objects as node, this object contains a reference to the image and the product. For usage, see code snippet (extremely simplified, just to get the idea).

@Override // from HierarchicalDataProvider
public Stream<HasTitle> fetchChildren(final HierarchicalQuery hq) {
    HasTitle item = hq.getParent();
    ...
    } else if (item instanceof Product) {
        final Product p = (Product)item;
        return p.getImages().stream()
            .map(img -> new Link(p, img));                
    ...
}

However, I consider this a hack. So if there is a more efficient way to do this, please let me know!

(using Vaadin 8.2.alpha2)

rmuller
  • 12,062
  • 4
  • 64
  • 92
  • What do you mean exactly with this `context` (forgive me if it it should be obvious)? You want the parent node and this _need this information from the UI_ , is it like that upon selection of child node you want to -for example - change the parent nodes font or do you want to manipulate data in it? – pirho Oct 26 '17 at 20:54
  • @pirho I have simplified my question. An example when you need the parent: when the user wants to unlink a selected product. – rmuller Oct 27 '17 at 05:32
  • Could you add few code snippets how you do it now? At least the parts where you manipulate `Image`, `Product` and the relation between those? Also the `DataProvider` should be described. Did i get it right: when selecting `Product` you might have a list of `Images` in it but when removing `Image` from selected `Product` you need to know what image and only the parent node can tell that? You have kind a _reverse_ structure in the `TreeGrid` compared to the data model? – pirho Oct 27 '17 at 06:50
  • @pirho You nailed it. This is exactly the use-case. Some parts of the complete structure (which is far more complex) are not pure hierarchical. Nevertheless, a tree structure is very easy to understand for the user to show some essential relations, like the product - image relation. – rmuller Oct 27 '17 at 07:11
  • How do you construct `HierarchicalQuery` that is passed to `fetchChildren` (just to clarify some aspects)? – pirho Oct 27 '17 at 09:32
  • @pirho It is the `HierarchicalQuery` as created by Vaadin. We do not use a custom implementation. – rmuller Oct 27 '17 at 11:00
  • Last one: ``HasTitle` is your own iface? And `Image`, `Product` implement it ? – pirho Oct 27 '17 at 11:11
  • Yes, this is the common interface, all objects in the tree view implement. – rmuller Oct 27 '17 at 11:20

0 Answers0