IChoiceRenderer 的使用

发布于 2024-11-05 15:44:00 字数 973 浏览 3 评论 0原文

在 wicket 中,DropDownChoice 的 IChoiceRenderer 的使用方式如下:

IChoiceRenderer renderer = new IChoicerRenderer() {
    public Object getDisplayValue(Object object) {
        return ((Country) object).getName();
    }

    public String getIdValue(Object object, int index) {    
        return ((Country) object).getId() + "";
    }
};

countries.setChoiceRenderer(renderer);

IChoiceRenderer 类的规范指出:

呈现一个选择。将用于内部表示的“id”值与“显示值”分开,“显示值”是向使用此渲染器的组件的用户显示的值。

getDisplayValue()的描述是:

获取向最终用户显示的值。

这意味着它有助于显示国家/地区的名称。正确的?

getIdValue()的描述是:

调用此方法是为了获取对象的 id 值(用作选择元素的 value 属性) id 可以像主键一样从对象中提取,或者如果列表稳定,您可以只返回索引的 toString。

这是什么意思?

一般来说,各种 wicket 组件(例如这里的 DropDownChoice)的模型的 id 属性是 Long 类型。 getIdValue() 有助于排序吗?

或者帮助生成 HTML 的 id 标签?

上述的“主键”是什么概念呢?

谢谢和问候。

In wicket IChoiceRenderer for DropDownChoice is used like :

IChoiceRenderer renderer = new IChoicerRenderer() {
    public Object getDisplayValue(Object object) {
        return ((Country) object).getName();
    }

    public String getIdValue(Object object, int index) {    
        return ((Country) object).getId() + "";
    }
};

countries.setChoiceRenderer(renderer);

The specification of the IChoiceRenderer class state that:

Renders one choice. Separates the 'id' values used for internal representation from 'display values' which are the values shown to the user of components that use this renderer.

The description of getDisplayValue() is:

Get the value for displaying to an end user.

That means it helps to display the name of the country. Right?

And the description of getIdValue() is:

This method is called to get the id value of an object (used as the value attribute of a choice element) The id can be extracted from the object like a primary key, or if the list is stable you could just return a toString of the index.

What does it mean?

In general id property of the models of various wicket component like DropDownChoice here, is of the type Long. Is getIdValue() helps to sort it?

Or helps to generate id tag for HTML?

What is concept of the aforesaid "Primary Key"?

Thanks and Regards.

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

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

发布评论

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

评论(2

迎风吟唱 2024-11-12 15:44:00

想象一下,对象将被放入映射中,其中 id 是键,值是您希望它引用的对象。如果您的两个对象共享相同的 id,或者对象的 id 发生更改,您的地图将无法正常工作。

这就是他们所说的应该是主键的意思。

顺便说一句,您不必在简单的情况下从头开始实现 IChoiceRenderer,在您的情况下,您可以使用 new ChoiceRenderer( "name", "id" );

Imagine that the objects will be put in a map, where the id is the key and the value is the object you want it to refer to. If two of your objects share the same id, or if the id of an object changes, your map won't work properly.

This is what they mean by saying it should be a primary key.

By the way, you don't have to implement IChoiceRenderer from scratch in simple situations, in your case you can use new ChoiceRenderer( "name", "id" );

终陌 2024-11-12 15:44:00

在下拉列表中,您将看到键值对的项目。因此,使用您的国家/地区示例,请考虑以下国家/地区到国家/地区代码的映射:

Key             Value
---------------------
Afghanistan     AF
Aland Islands   AX
Albania         AL
Algeria         DZ
American Samoa  AS
Andorra         AD
Angola          AO 

如果用户选择阿尔及利亚,则使用键 DZ 来唯一标识他们的选择。因此,如果主对象是具有 countryOfCitizenship 属性的 Person,则该属性将设置为 ID 为 DZ< 的 Country /代码>。 Wicket 使用 id 将下拉列表中的选择设置为属性值。它还使用 id 来确定在为设置了该属性的对象显示页面时从下拉列表中选择哪个值。

In a drop down list you will have items that are key value pairs. So using your countries example, consider the following mapping of country to country code:

Key             Value
---------------------
Afghanistan     AF
Aland Islands   AX
Albania         AL
Algeria         DZ
American Samoa  AS
Andorra         AD
Angola          AO 

If the user selects Algeria then the key DZ gets used to uniquely identify their choice. So if the main object is a Person with a countryOfCitizenship property, that property would be set to the Country with the id DZ. Wicket uses the id to set the selection from the drop down as the value for the property. It also uses the id to determine which value to select from the drop down when the page is displayed for an object that has that property set.

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