Working with MultiSelects in velocity

11 views
Skip to first unread message

Mark Pitely

unread,
Mar 25, 2021, 11:22:35 AM3/25/21
to dot...@googlegroups.com
There's a very common problem when dealing with Multiselects in dotCMS, here's a (relatively) easy example and solution, that shows how you can construct your own objects on the fly:

Here's the velocity output for '$program.category':
com.dotcms.rendering.velocity.viewtools.content.MultiSelectMap@19a545e5[options=[Architecture & Science, Art & Performance, Business & Management, Communication & Language, Education & Teaching, Health & Wellness, Liberal Arts & Social Sciences],values=[A&S, A&P, B&M, C&L, E&T, H&W, LA&SS],selectedValues=[B&M]] 

The issue is, how do I get the 'option' (human readable, first part of a select) from the matching list of selectedValues?
Here's some example code to build a custom map so you can do it:
#set ($programs= $dotcontent.pull("+contentType:ProgramsOfStudy",0,"ProgramsOfStudy.title"))
#set ($progfirst=$programs.get(0))
## get the first entry so we can extract the data from the fields

#set($namer = $contents.getEmptyMap())
#foreach ($val in $progfirst.category.value)
##NOTE it is 'value' despite directly above in the viewtool code it has 'values' (it *should* be values, but it is not!)
#set($_dummy = $namer.put("$val","$progfirst.category.options.get($velocityCount)"))
#end

The '$_dummy' is just to suppress output (otherwise it would show on the screen).

Now, you can do something like
#foreach ($picked in $progfirst.category.selectedValues)
$namer[$picked] <br>
#end


Perhaps someone has a more clever method, especially something that doesn't require all that work?

dotCMS, if you are listening: 1) fix the typo (value->values) and 2) give us another output like SelectedOptions or SelectedIndexes?

Mark Pitely
Marywood University
Reply all
Reply to author
Forward
0 new messages