0

How can I bind an int value to radiogroup for selectedItem? It wants String?

asked 2012-09-07 02:29:32 +0800

rickcr gravatar image rickcr
704 7

In my ViewModel I have an object that represents what I'm capturing data from the form for. (For simplicity using Person here)

The problem is one of the properties is an int that I need to bind to a radiogroup, but ZK seems to want a String to decide what value to set?

<radiogroup id="age" selectedItem="@bind(vm.person.ageRangeId)">
	<radio id="a1" label="10-18" value="1"></radio>
	<radio id="a2" label="19-25" value="2"></radio>
</radiogroup>

The radio button won't be set if "ageRangeId" is an int. It works if I have it a String. The somewhat easy way out is to bind the selected item to a vm "ageRangeAsString" and then in the getter/setter I do the conversion from/to the ageRangeId of the Person object. Seems a bit awkward though. Curious why ZK can't try to force the conversion (simply throw an exception if it fails.)

Am I missing something or am I correct that the selectedItem only works off of a string (when working off the value)?

delete flag offensive retag edit

17 Replies

Sort by ยป oldest newest

answered 2012-09-07 14:30:40 +0800

jj gravatar image jj
638 3

Did you try Integer?

link publish delete flag offensive edit

answered 2012-09-09 00:07:30 +0800

rdgrimes gravatar image rdgrimes
735 7

updated 2012-09-09 00:12:18 +0800

When you bind to vm.person.ageRangeId, ZK actually looks for vm.getPerson().getAgeRangeId() and vm.getPerson().setAgeRangeId(). What does that tell you? You control the getter return type and setter argument type. So, even though your ageRangeId may be int, you can always create additional methods, such as getAgeRangeIdStr() and setAgeRangeIdStr() methods. You would then @bind to vm.person.ageRangeIdStr . You don't need an actual property called ageRangeIdStr because ZK won't look for one. It will only look for the getter and setter methods I described. Make sense? Then, your get/setAgeRangeIdStr() methods can handle the conversion between int and String in communicating the ageRangeId value back and forth.

link publish delete flag offensive edit

answered 2012-09-12 06:45:51 +0800

hawk gravatar image hawk
2620 1 5
http://hawkphoenix.blogsp... ZK Team

updated 2012-09-13 00:06:50 +0800

Hi, rickcr
Auto conversion from int to string should be a good for users. To deal with this problem, excepting adding a getter, you could implement a converter to convert int to string.

link publish delete flag offensive edit

answered 2012-09-12 13:37:06 +0800

rickcr gravatar image rickcr
704 7

@rdgrimes Typically yes you have control over the domain objects and you could add a getItemAsString/setItemAsString type of accessor... but in my opinion that often pollutes your domain objects and sometimes you are using a jar of domain objects that you don't even have pure control of - think of another department at the company. Yes, you could extend the class or create a wrapper class (adapter pattern) but it seems like a bit of a hack just to set a property that happens to use a different data type.

This is often a valid knock on Java.. sure pros cons exist I know. But when you are working things like groovy you typically don't have to worry so much about these kinds of things. I often prefer my front end to be loosely typed but the backend end value objects more strongly typed. On the front end it should try to coerce what it can at least for some basic number types.

I didn't want to pollute the domain object with a get/setString accessor so I opted to put that accessor in the view model itself and the accessor then works with the underlying domain object in the view model. (It's about 10 lines of code including braces that I really think could be done away with.)

link publish delete flag offensive edit

answered 2012-09-12 16:17:33 +0800

rdgrimes gravatar image rdgrimes
735 7

My suggestion was based on the reality of what you/we have to deal with. Ideally, yes, you are right. But, sometimes you have to sacrifice the ideal for the practical.

link publish delete flag offensive edit

answered 2012-09-12 18:54:43 +0800

rickcr gravatar image rickcr
704 7

"sometimes you have to sacrifice the ideal for the practical." I hear you rdgrimes. Thanks for offering the suggestion on the approach to take.

link publish delete flag offensive edit

answered 2012-09-13 00:22:45 +0800

hawk gravatar image hawk
2620 1 5
http://hawkphoenix.blogsp... ZK Team

Hi,
I think the converter (http://books.zkoss.org/wiki/ZK_Developer%27s_Reference/MVVM/Data_Binding/Converter ) is a way to keep your domain object clean and also fulfill your requirement.

link publish delete flag offensive edit

answered 2012-09-13 16:12:27 +0800

rdgrimes gravatar image rdgrimes
735 7

Wow! What a lot of code to accomplish such a simple thing.

link publish delete flag offensive edit

answered 2012-09-13 18:15:14 +0800

rickcr gravatar image rickcr
704 7

updated 2012-09-13 18:15:38 +0800

Ha yea. Converter is way overkill imo just for coercing an int to a string:) If there is a place to submit feature requests this should be one of them.

For now I'll just stick to a prop on the viewModel itself that gets/sets the int from/on the domain object...

public String getProdValTypeString() {
        return String.valueOf(selectedBuyerBlock.getProdValType());
}

public void setProdValTypeString(String prodValTypeString) {
	this.prodValTypeString = prodValTypeString;
        //set the int property on the domain model:
	this.selectedBuyerBlock.setProdValType(Integer.valueOf(prodValTypeString));
}

link publish delete flag offensive edit

answered 2012-09-18 08:16:10 +0800

hawk gravatar image hawk
2620 1 5
http://hawkphoenix.blogsp... ZK Team

Hi,

another way is to bind radiogroup's model to an integer list and use template to create <radio>, bind radio's value to each object which is also a integer now. Then the selection will work correctly because ]vm.person.ageRangeId and each are both integer now.

<radiogroup id="age" selectedItem="@bind(vm.person.ageRangeId) model="@load(vm.ageList)">
	<template name="model">
		<radio label="@load(each)" value="@load(each)"></radio>
	</template>
</radiogroup>

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2012-09-07 02:29:32 +0800

Seen: 503 times

Last updated: Dec 02 '16

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More