java GWT - 列表框 - 预选项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1440564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
GWT - ListBox - pre-selecting an item
提问by Stein G. Strindhaug
I got a doubt regarding pre-selecting(setSelectedIndex(index)) an item in a ListBox, Im using Spring + GWT.
我对setSelectedIndex(index)在 ListBox 中预先选择()一个项目有疑问,我使用的是 Spring + GWT。
I got a dialog that contains a panel, this panel has a FlexPanel, in which I've put a couple ListBox, this are filled up with data from my database.
我有一个包含面板的对话框,这个面板有一个 FlexPanel,我在里面放了几个 ListBox,里面装满了我数据库中的数据。
But this Panel is for updates of an entity in my database, thus I wanted it to pre-select the current properties for this items, allowing the user to change at will.
但是这个面板用于更新我数据库中的一个实体,因此我希望它预先选择这个项目的当前属性,允许用户随意更改。
I do the filling up in the update method of the widget.
我在小部件的更新方法中进行填充。
I tried setting the selectedItem in the update method, but it gives me an null error.
我尝试在更新方法中设置 selectedItem,但它给了我一个空错误。
I've searched a few places and it seems that the ListBox are only filled at the exact moment of the display. Thus pre-selecting would be impossible.
我搜索了几个地方,似乎 ListBox 只在显示的确切时刻填充。因此预选将是不可能的。
I thought about some event, that is fired when the page is displayed.
我想到了一些事件,即在页面显示时触发。
onLoad() doesnt work..
onLoad() 不起作用..
Anyone have something to help me out in here?
有没有人在这里帮我一下?
回答by Stein G. Strindhaug
I really think you canset the selection before it's attached and displayed, but you have to have added the data before you can select an index. If this is a single select box you could write something like this:
我真的认为您可以在附加和显示之前设置选择,但是您必须先添加数据,然后才能选择索引。如果这是一个单一的选择框,你可以这样写:
void updateListContent(MyDataObject selected, List<MyDataObject> list){
for (MyDataObject anObject : list) {
theListBox.addItem(anObject.getTextToDisplay(), anObject.getKeyValueForList());
}
theListBox.setSelectedIndex(list.indexOf(selected));
}
If this is a multiple select box something like this may work:
如果这是一个多选框,这样的事情可能会起作用:
void updateListContent(List<MyDataObject> allSelected, List<MyDataObject> list){
for (MyDataObject anObject : list) {
theMultipleListBox.addItem(anObject.getTextToDisplay(), anObject.getKeyValueForList());
}
for (MyDataObject selected : allSelected) {
theMultipleListBox.setItemSelected(list.indexOf(selected), true);
}
}
(Note I haven't actually compiled this, so there might be typos. And this assumes that the selected element(s) is really present in the list of possible values, so if you cant be sure of this you'll need to add some bounds checking.)
(注意我还没有真正编译这个,所以可能有错别字。这里假设所选元素确实存在于可能值的列表中,所以如果你不能确定这一点,你需要添加一些边界检查。)
回答by Dhinakar
private void setSelectedValue(ListBox lBox, String str) {
String text = str;
int indexToFind = -1;
for (int i = 0; i < lBox.getItemCount(); i++) {
if (lBox.getValue(i).equals(text)) {
indexToFind = i;
break;
}
}
lBox.setSelectedIndex(indexToFind);
}
回答by Andrew
I've been happily setting both the values and the selection index prior to attachment so as far as I'm aware it should work. There's a bug however when setting the selected index to -1 on IE, see http://code.google.com/p/google-web-toolkit/issues/detail?id=2689.
我一直很高兴在附件之前设置值和选择索引,据我所知它应该可以工作。但是,在 IE 上将所选索引设置为 -1 时会出现错误,请参阅http://code.google.com/p/google-web-toolkit/issues/detail?id=2689。
回答by mico
Pre-selection should work also with setValue()-function. Thus, no complicated code is needed.
预选也适用于 setValue() 函数。因此,不需要复杂的代码。

