2017-01-30 97 views
1

如何以编程方式选择插入的表格行?该表绑定到BeanContainer,每次单击添加按钮时,我想要插入一行并使其不带ItemClick可选。如何在表中以编程方式选择插入的行?

我看过SQLContainerhere的另一个例子,但它不适用于我。

下面是按钮,成功地插入该行的听众:

addButton.addClickListener(new ClickListener() {    
    @Override 
    public void buttonClick(ClickEvent event) { 
     Object itemId = addList(); 
     table.addItem(itemId); 
     table.getItem(itemId).getItemProperty("PS_SECTION").setValue(n);      
     table.setValue(itemId); 
     table.select(itemId); 
     table.commit(); 
    } 
}); 
+0

你有没有试图把'table.commit'之后的'table.select(itemId)'? –

+0

@defaultlocale我现在试过了,它没有工作。你有任何其他想法尝试? – natso

+0

抱歉,不知道,我没有准备好环境。我会尝试手动选择一个项目,检查'table.getValue()'返回什么,然后尝试模拟它。另外,尝试在提交后放置'setValue'。 –

回答

1

Select a row programmatically

下面是代码:

@Theme("mytheme") 
public class MyUI extends UI { 

@Override 
protected void init(VaadinRequest vaadinRequest) { 
    final VerticalLayout layout = new VerticalLayout(); 
    layout.setMargin(true); 
    layout.setSpacing(true); 
    setContent(layout); 

    //cache the beans 
    ArrayList<MyBean> beans = getBeans(); 

    BeanItemContainer container = new BeanItemContainer<>(MyBean.class, beans); 

    Table table = new Table(); 
    table.setSelectable(true); 
    table.setImmediate(true); 
    table.setWidth("200px"); 
    table.setPageLength(5); 

    table.setContainerDataSource(container); 

    //select programmatically 
    table.select(beans.get(1));//this is the key idea! Provide the same bean from cache, for selection. 

    layout.addComponent(table); 
} 

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

public class MyBean { 

    private int id; 
    private String field; 

    public MyBean(int id, String field) { 
     this.id = id; 
     this.field = field; 
    } 

    public int getId() { 
     return id; 
    } 

    public String getField() { 
     return field; 
    } 

} 

public ArrayList<MyBean> getBeans() { 
    ArrayList<MyBean> beans = new ArrayList<>(); 

    MyBean bean = new MyBean(1, "Vikrant"); 
    beans.add(bean); 

    bean = new MyBean(2, "John"); 
    beans.add(bean); 

    bean = new MyBean(3, "Rahul"); 
    beans.add(bean); 

    return beans; 
} 

}

+0

谢谢!那帮助了我 – natso

相关问题