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

関連する問題