Re: how to select checkbox based on a hashmap
S Ahmed <[email protected]>
| Newsgroups | gmane.comp.web.freemarker.user |
|---|---|
| Message-ID | <CAG2rwuO9UGCBw1gJ0cO4Hq-TzLTScw59L4rnZhFx0WnxG_HroA@mail.gmail.com> |
Ok, and what exactly is: com.example.HasKeyMethod'?new() I have to create a package, with a class named HasKeyMethod? What is that exactly, a static method? A HashMap (treemap)? On Fri, Oct 19, 2012 at 8:09 AM, Daniel Dekany <[email protected]> wrote: > Friday, October 19, 2012, 5:14:45 AM, S Ahmed wrote: > > > Ok so let me get this straight, in my .ftl (say if I put all of > > this in my edit.ftl template for now): > > > > <#assign hasKey = > > 'com.example.HasKeyMethod'?new()>` > > > > <#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> > > > > And then inside the list above I do: > > > > <#if hasKey(userProjects, project.id)> > > </#if> > > > > Is that it? > > Yes. Although it's nicer to put the creating of "hasKey" outside the > template that uses it, because it's something that you may want to use > in more templates over time. In a serous app you often end up with > some utils.ftl anyway... that's a good place. (I also prefer #import > over #include, and of course after <#import '/utils.ftl' as u> you > have to write `u.hasKey(userProjects, project.id)`.) > > -- > Best regards, > Daniel Dekany > > > > On Thu, Oct 18, 2012 at 4:25 AM, Daniel Dekany <[email protected]> > wrote: > > 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 > > _______________________________________________ > > FreeMarker-user mailing list > > [email protected] > > https://lists.sourceforge.net/lists/listinfo/freemarker-user > > > > ------------------------------------------------------------------------------ > 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 > _______________________________________________ > FreeMarker-user mailing list > [email protected] > https://lists.sourceforge.net/lists/listinfo/freemarker-user > ------------------------------------------------------------------------------ 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 _______________________________________________ FreeMarker-user mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/freemarker-user