How to handle velocity variable in a multiselect custom field

363 views
Skip to first unread message

Alex

unread,
Sep 7, 2018, 11:47:32 AM9/7/18
to dotCMS User Group
Below is the code for a multiselect custom field:

I am having an issue displaying the selected values when I open the contentlet. If I see the velocityVarName valuable just printing it like this:

$context.get("${field.velocityVarName}") I can see the values all isted as (1,2,3,) ending with a comma.

I have tried using a loop:

 <option value="$con.identifier" 
               #foreach($id in $context.get("${field.velocityVarName}"))

                      #if($id == $con.identifier)selected#end
               #end>$con.name</option>
    
    #end
    </select>

But it does not display the selected values. Any suggestions are welcome!

Thank you,

Alex

<select multiple="multiple" size="scrollable" id="myMultiSelectField" onchange="updatemyMultiSelectField()" style="width: 200px;" ""="">
  
  #foreach($con in $dotcontent.pull("xxxx"))
 

    <option value="$con.indentifier" 

#if($context.get("${field.velocityVarName}") == $con.identifier)selected#end - I have to put this condition through a loop or something.

>$con.name</option>
   
    #end
    
    </select>
##field to store the value
<input type="hidden" id="myMultiSelectFieldId" value="">

    <script type="text/javascript">
        function updatemyMultiSelectField() {
            var myMultiSelectFieldValueList = "";
            var multiselect = $('myMultiSelectField');
            for(var i = 0; i < multiselect.options.length; i++) {
                if(multiselect.options[i].selected) {
                    myMultiSelectFieldValueList += multiselect.options[i].value + ",";
                }
            }
            $('myMultiSelectFieldId').value = myMultiSelectFieldValueList;
        }

        updatemyMultiSelectField();
    </script>

Falzone, Chris

unread,
Sep 7, 2018, 3:04:03 PM9/7/18
to dot...@googlegroups.com
So I don't think you can get the field's velocity var name that way.  We always pass it as a variable set before the rest of the code for the field. 

Here's the code we use for this, we made it generic enough to be reusable since we have a lot of these:

Hope that helps!

--
http://dotcms.com - Open Source Java Content Management
---
You received this message because you are subscribed to the Google Groups "dotCMS User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dotcms+un...@googlegroups.com.
To post to this group, send email to dot...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dotcms/0661a207-70c1-46b9-bfb2-e158f459166b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Alex

unread,
Sep 9, 2018, 9:36:35 PM9/9/18
to dotCMS User Group
Dear Chris:

Thank YOU so much!  This has sparkled so many insights about custom fields!

I modified it a bit and it works great!

I also have custom fields for single select field, but with JQuery.  Do you also have a sample of a single select field?

Regards,

Alex

Falzone, Chris

unread,
Sep 10, 2018, 11:12:29 AM9/10/18
to dot...@googlegroups.com
For single Select we use the dojo FilteringSelect.  I am not sure if this still works or is deprecated at all in the latest version of dotCMS we are still on 4.1.1 and I haven't researched if this needs to be updated for 5.x.  I also included an older one we have using jQuery:


Hope it helps!

Reply all
Reply to author
Forward
0 new messages