SmartGWT TextItem - focusInItem()方法不起作用?

时间:2013-06-18 09:20:52

标签: window focus smartgwt dynamicform


我有一个在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);
}

3 个答案:

答案 0 :(得分:4)

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

我不知道您放置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.

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

答案 1 :(得分: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自动关注表单中的第一个可聚焦字段。

答案 2 :(得分:0)

为什么不尝试专注于表单本身:

search.focus();