Re: how to select checkbox based on a hashmap

Daniel Dekany <[email protected]>
Newsgroups gmane.comp.web.freemarker.user
Message-ID <[email protected]>
Thursday, October 18, 2012, 3:22:37 AM, S Ahmed wrote:

> So I have a hashmap that contains the projects that the checkbox should be checked for:
>
> Map<Integer, Project> userProjects = new HashMap<Integer, Project>();
>
> So far my .ftl looks like:
>
>   <#list projects as project>
>                     <tr>
>                         <td><label class="checkbox"><input
> id="project-${project.id}" name="project-${project.id}"
>                                                           
> type="checkbox" />${project.name}</label></td>
>                     </tr>
>                 </#list>
>  
> How can I make the checkbox 'checked' if the hashMap contains an entry?  The key is project.id

There your run into the good old string-keys-only issue of FM... one
of the main pains (the other being not supporting null as valid
value).

What I always did is not using the Default object-wrapper of FM, which
is still like it is for backward compatibility. I have always used
BeansWrapper instead (maybe extended by me) with
setSimpleMapWrapper(true). It has a hack to work around the problem
that `map[key]` expects strings; you can write map(key) instead, where
`key` can be of any type. Then you can do `<#if
userProjects(project.id)??> checked</#if>`.

If changing the object wrapper is not an option (due to
compatibility), for now you will have to create a
TemplateMethodModelEx for this (`<#assign hasKey =
'com.example.HasKeyMethod'?new()>` in some commonly included/imported
template and then `<#if hasKey(userProjects, project.id)>` in the
normal templates). (Adding built-ins to do things like this is high on
my TODO BTW.)

-- 
Best regards,
 Daniel Dekany


------------------------------------------------------------------------------
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:
http://p.sf.net/sfu/appdyn_sfd2d_oct
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.