1


我有一个在 com.smartgwt.client.widgets.Window.Window() 中打开的搜索表单。在其中,我有一个 VLayout,其中有一个搜索表单:

DynamicForm search = new DynamicForm();
// setMargin, setTitle, setNumCols
TextItem name = new TextItem();
name.setFormatOnFocusChange(true);
//setEditorValueFormatter, etc.
search.setFields(/*some fields*/, name, /*other fields*/);
name.focusInItem();

并且焦点不在项目中(它不存在)。为什么呢?
先感谢您!
编辑:
这是两个中介的代码:

public class MainMediator extends Mediator {
private Window popup = new Window();

protected void initView(){
        // here I have a Form with fields and icon on one TextItem, on which I do:
    searchField.addIconClickHandler(new IconClickHandler() {
    popup = new Window();
    popup.setIsModal(true);
    popup.setShowModalMask(true);
    });
}

public final void handleNotification(final INotification notification){
    // if the right notification is sent, execute this code:
    PopupMediator m = (PopupMediator) this.getFacade().retreiveMediator(PopupMediator.NAME);
        VLayout popupLayout = (VLayout) m.getViewComponent();
    popup.addItem(popupLayout);
        popup.show();
    } 
}

public class PopupMediator extends Mediator {
    protected void initView(){
    viewComponent = new VLayout();
    DynamicForm searchForm = new DynamicForm();
    // searchForm props
    TextItem name = new TextItem();
    // name props and some other fields
    searchForm.setFields(name /* and the others */);
        VLayout searchFormContainer = new VLayout();
    // searchFormContainer props
        searchFormContainer.setMembers(seachForm);
        name.focusInItem(); // not working on popup shown
    HLayout searchContainer = new HLayout();
    // searchContainer props
    searchContainer.setMembers(grid1, searchFormContainer);
    VLayout container = new VLayout();
    // container props
        container.setMembers (searchContainer, grid2);
        ((VLayout)viewComponent).setMembers(container, buttons);
}
4

3 回答 3

4

您遇到此问题是因为仅formitem.focusInItem()绘制或说在浏览器中呈现后才有效。添加in不会绘制它。formitemformitemDynamicForm

我不知道您将 放在哪里DynamicForm,但要完全理解它,请查看以下代码:

Window window = new Window();
window.setSize("900px", "500px");
VLayout layout = new VLayout();
DynamicForm dynamicForm = new DynamicForm();
dynamicForm.setSize("800px", "400px");
TextItem item = new TextItem();
dynamicForm.setFields(item);
item.focusInItem(); // This won't work.
layout.addMember(dynamicForm);
window.addItem(layout);
item.focusInItem(); // This won't work.
window.show();
item.focusInItem(); // This will work.

因此,相应地更改您的代码。

于 2013-06-18T15:13:35.780 回答
1

不确定如何接收 handleNotification() 回调,但不应在其中使用 window.addItem() 。
这将导致每次调用回调时添加/覆盖多个项目。

如果需要handleNotification()回调,它应该只用于window.show(),加上任何表单字段填充/设置焦点/等。

如果 Window 的内容不会从一个回调更改为另一个,请在创建窗口期间初始化窗口布局。
如果 Window 的内容将从一个回调更改为另一个,您将需要删除以前添加的项目。

这是一个简单的工作实现,它在单击按钮时弹出窗口并将焦点设置在给定字段上。

TextItem name1 = new TextItem("name1", "Name 1");
final TextItem name2 = new TextItem("name2", "Name 2"); // setting focus to name2
TextItem name3 = new TextItem("name3", "Name 3");

final DynamicForm searchForm = new DynamicForm();
// searchForm.setAutoFocus(true); // sets focus to first focusable field
searchForm.setFields(name1, name2, name3);

VLayout searchFormContainer = new VLayout();
searchFormContainer.setMembers(searchForm);

final Window window = new Window();
window.setIsModal(true);
window.setShowModalMask(true);
window.setAutoCenter(true);
window.setSize("400px", "300px");
window.addItem(searchFormContainer);

Button button = new Button("Search");
button.addClickHandler(new ClickHandler() {
    public void onClick(ClickEvent event) {
        window.show();
        name2.focusInItem();
       // searchForm.focusInItem(name2); // this also works
    }
});

可以使用DynamicForm.setAutoFocus自动关注表单中的第一个可聚焦字段。

于 2013-07-25T10:27:13.047 回答
0

您为什么不尝试关注表单本身:

search.focus();
于 2013-06-18T13:52:58.450 回答