zk 以编程方式选择组合框项目

发布于 2024-08-25 18:57:16 字数 9094 浏览 7 评论 0原文

我无法以编程方式设置组合框的值,有人可以告诉我代码中缺少什么吗

 public class Profile extends Window implements AfterCompose {

    @Override
    public void afterCompose() {
       Session session = Sessions.getCurrent(false);
                ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(
                (ServletContext) getDesktop().getWebApp().getNativeContext());

        UsersDao usersDao = (UsersDao) ctx.getBean("daoUsers");


        User user = (User) session.getAttribute("user");
        user = usersDao.getUser(user.getUsername(),user.getPassword());

        Textbox username_t = (Textbox) this.getFellow("username");
        Textbox password_t = (Textbox) this.getFellow("password");
        Textbox conpassword_t = (Textbox) this.getFellow("con_password");
        Textbox firstname_t = (Textbox) this.getFellow("firstName");
        Textbox lastname_t = (Textbox) this.getFellow("lastName");
        Textbox email_t = (Textbox) this.getFellow("email");
        Combobox hintQuestion_t = (Combobox) this.getFellow("hintQuestion");
        Textbox hintAnswer_t = (Textbox) this.getFellow("hintAnswer");
        Combobox locale_t = (Combobox) this.getFellow("locale");
        Combobox authority_t = (Combobox) this.getFellow("authority");

        username_t.setText(user.getUsername());
        firstname_t.setText(user.getUserDetails().getFirstName());
        lastname_t.setText(user.getUserDetails().getLastName());
        email_t.setText(user.getUserDetails().getEmail());
        Comboitem selectedItem = getSelectedIndexComboboxItem(hintQuestion_t, user.getHintQuestion());
        hintQuestion_t.setSelectedItem(selectedItem);
        hintAnswer_t.setText(user.getHintAnswer());
        selectedItem = getSelectedIndexComboboxItem(locale_t, user.getUserDetails().getLocale());
        locale_t.setSelectedItem(selectedItem);
        selectedItem = getSelectedIndexComboboxItem(authority_t, ((Authority)user.getAuthorities().toArray()[0]).getRole());
        authority_t.setSelectedItem(selectedItem);


    }

   private Comboitem getSelectedIndexComboboxItem(Combobox combobox, String value) {
        List<Comboitem> items = combobox.getItems();
        Comboitem item = items.get(0);
        for (int i = 0; i < items.size(); i++) {
            Comboitem comboitem = items.get(i);
            String label = (String)comboitem.getLabel();
            String cval = (String)comboitem.getValue();
            if ((label!=null && label.equalsIgnoreCase(value)) || (cval != null  && cval.equalsIgnoreCase(value))) {
                item = comboitem;
                break;
            }
        }
        return item;
    }
}

// zk 文件

<window id="profile" use="com.jf.web.zk.ui.Profile">
        <tabbox  id="tabbox" width="40%" >
            <tabs>
                <tab label="Account Information"/>
                <tab label="Personal Information"/>
                <tab label="Contact Details"/>
            </tabs>
            <tabpanels>
                <tabpanel>
                    <grid>
                        <rows>
                            <row>
                                <label value="${i18nUtils.message('user.username')}"/>
                                <hbox>
                                    <textbox id="username" />*,a-zA-Z,0-9
                                </hbox>
                            </row>
                            <row>
                                <label value="${i18nUtils.message('user.password')}"/>
                                <hbox>
                                    <textbox id="password" type="password"/>*
                                </hbox>
                            </row>
                            <row>
                                <label value="${i18nUtils.message('registration.user.password.confirm')}"/>
                                <hbox>
                                    <textbox id="con_password" type="password"/>*
                                </hbox>
                            </row>
                            <row>
                                <label value="${i18nUtils.message('user.details.first.name')}"/>
                                <hbox>
                                    <textbox id="firstName" type="text"/>*
                                </hbox>
                            </row>
                            <row>
                                <label value="${i18nUtils.message('user.details.last.name')}"/>
                                <hbox>
                                    <textbox id="lastName" type="text"/>*
                                </hbox>
                            </row>
                            <row>
                                <label value="${i18nUtils.message('user.details.email')}"/>
                                <hbox>
                                    <textbox id="email" type="text"/>*
                                </hbox>
                            </row>
                            <row>
                                <label value="${i18nUtils.message('user.hint.question')}"/>
                                <hbox>
                                    <combobox id="hintQuestion" onCreate='self.setSelectedIndex(1);'>
                                        <comboitem label="${i18nUtils.message('user.hint.question.possible.value1')}" />
                                        <comboitem label="${i18nUtils.message('user.hint.question.possible.value2')}" />
                                        <comboitem label="${i18nUtils.message('user.hint.question.possible.value3')}" />
                                        <comboitem label="${i18nUtils.message('user.hint.question.possible.value4')}" />
                                        <comboitem label="${i18nUtils.message('user.hint.question.possible.value5')}" />
                                    </combobox>*
                                </hbox>
                            </row>
                            <row>
                                <label value="${i18nUtils.message('user.hint.answer')}"/>
                                <hbox>
                                    <textbox id="hintAnswer" type="text"/>*
                                </hbox>
                            </row>
                            <row>
                                <label value="${i18nUtils.message('user.details.locale')}"/>
                                <hbox>
                                    <combobox id="locale" onCreate='self.setSelectedIndex(1);self.setReadonly(true);'>
                                        <comboitem label="${i18nUtils.message('user.details.locale.en')}" value="en_US"/>
                                        <comboitem label="${i18nUtils.message('user.details.locale.bg')}" value="bg_BG"/>
                                    </combobox>*
                                </hbox>
                            </row>
                            <row>
                                <label value="${i18nUtils.message('authority.account.type')}"/>
                                <hbox>
                                    <combobox id="authority" onCreate='self.setSelectedIndex(0);self.setReadonly(true);'>
                                        <comboitem label="${i18nUtils.message('authority.job.seeker')}" value="Job Seeker"/>
                                        <comboitem label="${i18nUtils.message('authority.employer')}" value="Employer"/>
                                        <comboitem label="${i18nUtils.message('authority.hra')}" value="Human Resource Agency"/>
                                        <comboitem label="${i18nUtils.message('authority.advertiser')}" value="Advertiser"/>
                                    </combobox>*
                                </hbox>
                            </row>
                        </rows>
                    </grid>
                </tabpanel>
            </tabpanels>
        </tabbox>
        <grid width="40%">
            <rows>
                <row>
                    <button label="${i18nUtils.message('bttn.save')}" onClick="save()"/>
                    <button label="${i18nUtils.message('bttn.cancel')}" onClick="cancel()"/>
                </row>
            </rows>
        </grid>
    </window>
</zk>

“getSelectedIndexComboboxItem()”确实返回了正确的选定项目,但似乎对 UI 没有影响。例如,区域设置设置为默认保加利亚语,我需要将其设置为英语。

I cannot set the value of combobox programmatically can some one tell me what missing in the code

 public class Profile extends Window implements AfterCompose {

    @Override
    public void afterCompose() {
       Session session = Sessions.getCurrent(false);
                ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(
                (ServletContext) getDesktop().getWebApp().getNativeContext());

        UsersDao usersDao = (UsersDao) ctx.getBean("daoUsers");


        User user = (User) session.getAttribute("user");
        user = usersDao.getUser(user.getUsername(),user.getPassword());

        Textbox username_t = (Textbox) this.getFellow("username");
        Textbox password_t = (Textbox) this.getFellow("password");
        Textbox conpassword_t = (Textbox) this.getFellow("con_password");
        Textbox firstname_t = (Textbox) this.getFellow("firstName");
        Textbox lastname_t = (Textbox) this.getFellow("lastName");
        Textbox email_t = (Textbox) this.getFellow("email");
        Combobox hintQuestion_t = (Combobox) this.getFellow("hintQuestion");
        Textbox hintAnswer_t = (Textbox) this.getFellow("hintAnswer");
        Combobox locale_t = (Combobox) this.getFellow("locale");
        Combobox authority_t = (Combobox) this.getFellow("authority");

        username_t.setText(user.getUsername());
        firstname_t.setText(user.getUserDetails().getFirstName());
        lastname_t.setText(user.getUserDetails().getLastName());
        email_t.setText(user.getUserDetails().getEmail());
        Comboitem selectedItem = getSelectedIndexComboboxItem(hintQuestion_t, user.getHintQuestion());
        hintQuestion_t.setSelectedItem(selectedItem);
        hintAnswer_t.setText(user.getHintAnswer());
        selectedItem = getSelectedIndexComboboxItem(locale_t, user.getUserDetails().getLocale());
        locale_t.setSelectedItem(selectedItem);
        selectedItem = getSelectedIndexComboboxItem(authority_t, ((Authority)user.getAuthorities().toArray()[0]).getRole());
        authority_t.setSelectedItem(selectedItem);


    }

   private Comboitem getSelectedIndexComboboxItem(Combobox combobox, String value) {
        List<Comboitem> items = combobox.getItems();
        Comboitem item = items.get(0);
        for (int i = 0; i < items.size(); i++) {
            Comboitem comboitem = items.get(i);
            String label = (String)comboitem.getLabel();
            String cval = (String)comboitem.getValue();
            if ((label!=null && label.equalsIgnoreCase(value)) || (cval != null  && cval.equalsIgnoreCase(value))) {
                item = comboitem;
                break;
            }
        }
        return item;
    }
}

// zk file

<window id="profile" use="com.jf.web.zk.ui.Profile">
        <tabbox  id="tabbox" width="40%" >
            <tabs>
                <tab label="Account Information"/>
                <tab label="Personal Information"/>
                <tab label="Contact Details"/>
            </tabs>
            <tabpanels>
                <tabpanel>
                    <grid>
                        <rows>
                            <row>
                                <label value="${i18nUtils.message('user.username')}"/>
                                <hbox>
                                    <textbox id="username" />*,a-zA-Z,0-9
                                </hbox>
                            </row>
                            <row>
                                <label value="${i18nUtils.message('user.password')}"/>
                                <hbox>
                                    <textbox id="password" type="password"/>*
                                </hbox>
                            </row>
                            <row>
                                <label value="${i18nUtils.message('registration.user.password.confirm')}"/>
                                <hbox>
                                    <textbox id="con_password" type="password"/>*
                                </hbox>
                            </row>
                            <row>
                                <label value="${i18nUtils.message('user.details.first.name')}"/>
                                <hbox>
                                    <textbox id="firstName" type="text"/>*
                                </hbox>
                            </row>
                            <row>
                                <label value="${i18nUtils.message('user.details.last.name')}"/>
                                <hbox>
                                    <textbox id="lastName" type="text"/>*
                                </hbox>
                            </row>
                            <row>
                                <label value="${i18nUtils.message('user.details.email')}"/>
                                <hbox>
                                    <textbox id="email" type="text"/>*
                                </hbox>
                            </row>
                            <row>
                                <label value="${i18nUtils.message('user.hint.question')}"/>
                                <hbox>
                                    <combobox id="hintQuestion" onCreate='self.setSelectedIndex(1);'>
                                        <comboitem label="${i18nUtils.message('user.hint.question.possible.value1')}" />
                                        <comboitem label="${i18nUtils.message('user.hint.question.possible.value2')}" />
                                        <comboitem label="${i18nUtils.message('user.hint.question.possible.value3')}" />
                                        <comboitem label="${i18nUtils.message('user.hint.question.possible.value4')}" />
                                        <comboitem label="${i18nUtils.message('user.hint.question.possible.value5')}" />
                                    </combobox>*
                                </hbox>
                            </row>
                            <row>
                                <label value="${i18nUtils.message('user.hint.answer')}"/>
                                <hbox>
                                    <textbox id="hintAnswer" type="text"/>*
                                </hbox>
                            </row>
                            <row>
                                <label value="${i18nUtils.message('user.details.locale')}"/>
                                <hbox>
                                    <combobox id="locale" onCreate='self.setSelectedIndex(1);self.setReadonly(true);'>
                                        <comboitem label="${i18nUtils.message('user.details.locale.en')}" value="en_US"/>
                                        <comboitem label="${i18nUtils.message('user.details.locale.bg')}" value="bg_BG"/>
                                    </combobox>*
                                </hbox>
                            </row>
                            <row>
                                <label value="${i18nUtils.message('authority.account.type')}"/>
                                <hbox>
                                    <combobox id="authority" onCreate='self.setSelectedIndex(0);self.setReadonly(true);'>
                                        <comboitem label="${i18nUtils.message('authority.job.seeker')}" value="Job Seeker"/>
                                        <comboitem label="${i18nUtils.message('authority.employer')}" value="Employer"/>
                                        <comboitem label="${i18nUtils.message('authority.hra')}" value="Human Resource Agency"/>
                                        <comboitem label="${i18nUtils.message('authority.advertiser')}" value="Advertiser"/>
                                    </combobox>*
                                </hbox>
                            </row>
                        </rows>
                    </grid>
                </tabpanel>
            </tabpanels>
        </tabbox>
        <grid width="40%">
            <rows>
                <row>
                    <button label="${i18nUtils.message('bttn.save')}" onClick="save()"/>
                    <button label="${i18nUtils.message('bttn.cancel')}" onClick="cancel()"/>
                </row>
            </rows>
        </grid>
    </window>
</zk>

The "getSelectedIndexComboboxItem()" does return the correct selected item but there seems no effect on the UI. Like for example the locale is set to default Bulgarian language and I need to set it to English.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你爱我像她 2024-09-01 18:57:16

我认为这是你的 ZK 版本的一个错误。您可以尝试使用 setSelectedItemApi 或 setSelectedItemIndex 而不是 setSelectedItem。

另一方面,即使它有效,您的解决方案也不正确。组合框允许输入与预定义列表中不同的值。组合框唯一真正的结果是文本标签。我认为为组合项目提供单独的“值”并不是一个好主意。
在组合框中使用 setText(),或者如果要将选项限制为预定义列表,请使用列表框。

I think it is a bug in your version of ZK. You could try using setSelectedItemApi or setSelectedItemIndex instead of setSelectedItem.

On the other hand, even if it works, your solution is not right. Comboboxes allow entering different values than what's in the predefined list. The only real result of a combobox is the text label. I think having a separate 'value' of a Comboitem is not a good idea.
Use setText() on the Combobox, or if you want to restrict the choices to a predefined list, use Listbox.

笔芯 2024-09-01 18:57:16

只要看看你的代码,除了 Miklos 指出的之外,我还可以看到一些错误。

您正在使用来自基于 Java 的作曲家的 getfellow。但是,您不应该这样做,因为 ZK 会为您自动装配组件。您需要使用 MVC 模式并扩展 GenericForwardComposer。以下是一个示例:

ZUL 文件

<window title="composer5 example" border="normal" width="300px" apply="example.MyComposer">
    <grid>
        <rows>
            <row>First Name: <textbox id="firstName"/></row><!-- forward is removed -->
            <row>Last Name: <textbox id="lastName"/></row><!-- forward is removed -->
            <row>Full Name: <label id="fullName"/></row>
        </rows>
    </grid>
</window>

Java 文件

package example;

public class MyComposer extends GenericForwardComposer {
    private Textbox firstName;
    private Textbox lastName;
    private Label fullName;

    @Override
    public void doAfterCompose(Component comp) throws Exception {
        // TODO Auto-generated method stub
        super.doAfterCompose(comp);
    }

    //onChange event from firstName component
    public void onChange$firstName(Event event) { 
        fullName.setValue(firstName.getValue()+" "+lastName.getValue());
    }

    //onChange event from lastName component
    public void onChange$lastName(Event event) {
        fullName.setValue(firstName.getValue()+" "+lastName.getValue());
    }
}

有关详细信息,您可以查阅 文档

对于检索所选项目的功能,为什么不直接使用 Combobox 的 getSelectedItem 呢?即使你像 Miklos 建议的那样使用 Listbox,你也可以使用相同的功能。

关于设置所选项目,您需要将ZK版本升级到最新。我这样做没有问题。

Just looking at your code I can see a few things wrong in addition to what Miklos pointed out.

You are using getfellow from a Java based composer. However, you should not do this as ZK will autowire components for you. You need to use the MVC pattern and extend GenericForwardComposer. Here is an example:

ZUL File

<window title="composer5 example" border="normal" width="300px" apply="example.MyComposer">
    <grid>
        <rows>
            <row>First Name: <textbox id="firstName"/></row><!-- forward is removed -->
            <row>Last Name: <textbox id="lastName"/></row><!-- forward is removed -->
            <row>Full Name: <label id="fullName"/></row>
        </rows>
    </grid>
</window>

Java File

package example;

public class MyComposer extends GenericForwardComposer {
    private Textbox firstName;
    private Textbox lastName;
    private Label fullName;

    @Override
    public void doAfterCompose(Component comp) throws Exception {
        // TODO Auto-generated method stub
        super.doAfterCompose(comp);
    }

    //onChange event from firstName component
    public void onChange$firstName(Event event) { 
        fullName.setValue(firstName.getValue()+" "+lastName.getValue());
    }

    //onChange event from lastName component
    public void onChange$lastName(Event event) {
        fullName.setValue(firstName.getValue()+" "+lastName.getValue());
    }
}

For more information you can consult the documentation

With to the function to retrieve the selected item why not just use Combobox's getSelectedItem? Even if you use Listbox like Miklos suggested then you can also use the same function.

With regard to setting the selected item, you need to upgrade your ZK version to the latest. I have no problem doing so.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文