2017-01-30 77 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

相關問題