英文:
Setting value for select element is not showing in UI
问题
私はVaadin Selectを使用して、州の選択メニューを表示しています。
private Select<States> states = new Select<>();
states.setLabel("State");
states.setItems(facade.stateService().findAllStates());
states.setItemLabelGenerator(States::getName);
Optional<States> state = facade.stateService().findByCode(location.getLocationState());
if (state.isPresent()) {
states.setValue(state.get()); // これは動作していません
}
states.setValue()
を使用して州の値を取得し設定していますが、選択メニューは更新された州を表示していません。メソッドは呼び出されています。選択メニューを選択状態にする方法は何ですか?お願いします。
英文:
I am using vaadin select to display a select menu with states.
private Select<States> states = new Select<>();
states.setLabel("State");
states.setItems(facade.stateService().findAllStates());
states.setItemLabelGenerator(States::getName);
Optional<States> state = facade.stateService().findByCode(location.getLocationState());
if (state.isPresent()) {
states.setValue(state.get()); // this is not working
}
I am getting the state value and setting it using states.setValue()
but the select is not displaying the updated state. The method is getting called. How to make select the menu selected? Thanks.
答案1
得分: 5
无法根据您分享的代码确定这一点。很可能由location.getLocationState().get()
返回的States
对象与facade.stateService().findAllStates()
返回的值不共享相同的对象标识。即使对象的属性(例如“name”)相同,也可能发生这种情况。通常的解决方法是以一种方式在类中实现hashCode
和equals
方法,使标识仅依赖于实体的ID。
英文:
It's not possible to tell based on the code you have shared. Quite likely the States
object returned by location.getLocationState().get()
does not share the same object identity with a value returned by facade.stateService().findAllStates()
. This can happen even if the properties (such as the "name") of the objects are the same. Typically the answer is to implement the hashCode
and equals
methods in the class in a way that identity depends only on the id of the entity.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论