设置选择元素的值在用户界面中不显示

huangapple go评论52阅读模式
英文:

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&lt;States&gt; states = new Select&lt;&gt;();
states.setLabel(&quot;State&quot;);
states.setItems(facade.stateService().findAllStates());
states.setItemLabelGenerator(States::getName);
Optional&lt;States&gt; 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”)相同,也可能发生这种情况。通常的解决方法是以一种方式在类中实现hashCodeequals方法,使标识仅依赖于实体的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.

huangapple
  • 本文由 发表于 2023年2月10日 15:04:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75407887.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定