Cannot add NativeSelect to Grid

I am not able to put a NativeSelect into a Grid. The result is always: Screenshot - NativeSelect in Grid.JPG
Can someone please help me?


method init

[code]
package de.thalia;

import javax.servlet.annotation.WebServlet;
import com.vaadin.annotations.Theme;
import com.vaadin.annotations.Title;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.*;
import java.util.Arrays;
import java.util.List;

@Title(“My UI”)
@Theme(“mytheme”)
public class MyUI extends UI {

@Override
protected void init(VaadinRequest request) {

    final VerticalLayout layout = new VerticalLayout();

    NativeSelect nativeSelect = new NativeSelect<>();
    nativeSelect.setItems("Option1", "Option2");

    List<Person> people = Arrays.asList(
            new Person("Marc", "Born", nativeSelect),
            new Person("Andrew", "Moritz", nativeSelect),
            new Person("Michael", "Toast", nativeSelect));

    Grid<Person> grid = new Grid<>();
    grid.setItems(people);
    grid.addColumn(Person::getFirstName).setCaption("First Name");
    grid.addColumn(Person::getSecondName).setCaption("Second Name");
    grid.addColumn(Person::getHobbies).setCaption("Hobbies");;

    layout.addComponent(grid);

    setContent(layout);
}

@WebServlet(urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true)
@VaadinServletConfiguration(ui = MyUI.class, productionMode = false)
public static class MyUIServlet extends VaadinServlet {
}

}
[/code]
class person

package de.thalia;

import com.vaadin.ui.NativeSelect;

public class Person {

    String firstName;
    String secondName;
    NativeSelect nativeSelect;

    public Person(String firstName, String secondName, NativeSelect nativeSelect) {
        this.firstName = firstName;
        this.secondName = secondName;
        this.nativeSelect = nativeSelect;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getSecondName() {
        return secondName;
    }

    public void setSecondName(String secondName) {
        this.secondName = secondName;
    }

    public NativeSelect getHobbies() {
        return nativeSelect;
    }

    public void setHobbies(NativeSelect nativeSelect) {
        this.nativeSelect = nativeSelect;
    }
}

34010.jpg

Hi,

use grid.addComponentColumn (available since 8.1 prereleases) to add components to Grid . Something like this:

grid.addComponentColumn(person -> { NativeSelect<String> ns = new NativeSelect<>(); List<String> items = new ArrayList<>(); items.add("foo"); items.add("bar"); ns.setItems(items); return ns; }); Of course, you probably want to get the list to display from the Person object lambda parameter instead. I don’t recommend storing the NativeSelect in the Person class - store a List of hobbies instead.

Hope this helps,

Olli

Great to hear!

-Olli

Hi Olli,

thanks for your help. Your proposal has solved the problem.

Olli Tietäväinen:
Hi,

use grid.addComponentColumn (available since 8.1 prereleases) to add components to Grid . Something like this:

grid.addComponentColumn(person -> {
            NativeSelect<String> ns = new NativeSelect<>();
            List<String> items = new ArrayList<>();
            items.add("foo");
            items.add("bar");
            ns.setItems(items);
            return ns;
 });

Of course, you probably want to get the list to display from the Person object lambda parameter instead. I don’t recommend storing the NativeSelect in the Person class - store a List of hobbies instead.

Hope this helps,

Olli

I’m very glad to know that Vaadin 8.1 has this feature. But, for me, it seems a little bit late, cause I have to put a custom component into a column of Grid with Vaadin 7.6.8. I think that it can be only implemented by custom renderer on Vaadin 7.6.8

Hi,

As it happens, there’s [SelectRenderer]
(https://vaadin.com/directory/component/selectrenderer) in the Vaadin Directory you could use.

Regards,

Olli