0

enter image description here

next to the Search Results: I want to add a number of counts of the list

I need to display number of items selected in the multi-select list next to the Total Selected Counts and I'm using <apex:outputText > but not sure how to display the counter next to it?

 <td>
    <apex:selectList value="{!RightSelectedList}" multiselect="true"   style="width:100%" size="10"  >
    <apex:selectOptions value="{!RightOptionList}"   />
    </apex:selectList><apex:outputText >Total Selected Counts:</apex:outputText>
</td>
  • Do you want the number selected the last time the page was saved (do-able with Visualforce) or do you want the number currently selected in the UI i.e. when the user has changed the selection but not yet saved the page (that required JavaScript)? – Keith C Aug 21 '15 at 22:30
  • The last part, selected in the UI – Nick Kahn Aug 21 '15 at 22:31
0

Here is a way to count and display the selected options in HTML using jQuery:

<html>
<body>
<select class="selectMarker" multiple="multiple">
    <option>Flowers</option>
    <option selected="selected">Shrubs</option>
    <option>Trees</option>
    <option selected="selected">Bushes</option>
    <option>Grass</option>
    <option>Dirt</option>
</select>
<div class="countMarker"></div>

<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script>
(function($) {
    function updateCount() {
        var n = $('.selectMarker option:selected').length;
        $('.countMarker').html(n);
    }
    $('.selectMarker').change(function() {
        updateCount();
    });
    updateCount();
})(jQuery.noConflict());
</script>

</body>
</html>

To convert this to Visualforce, just copy the script content and use the styleClass attribute on the Visualforce tags where class is used above on the HTML elements.

  • hmm... i am not sure if this is the right way of doing and since I already have list when the user adds in the multi-select, what i'm looking a way to display the list.count in the visualforce page... it will be much easier then using jquery which i think its not overkill in my opinion. – Nick Kahn Aug 21 '15 at 23:14
  • you see I already know what has been selected RightSelectedList so what I need on the apex side to count and display on the VFP, thats what i am looking a way to display the value – Nick Kahn Aug 21 '15 at 23:15
  • @AbuHamzah The above works when the page is first opened and also works immediately the selection is changed. Not sure why you want the count, but if you only calculate it when the page is opened and the list is editable then the count will be wrong immediately the user edits it and will only be correct after a save and a re-opening of the page. But to compute the count in Apex (avoiding jQuery) you can split the picklist value on the separator for multi-select picklists which is a semi-colon and take the size of the resulting array. – Keith C Aug 21 '15 at 23:25
  • I will post you the screen shot to make more clear – Nick Kahn Aug 21 '15 at 23:53
  • I have updated my question with an image to give more context of my question. - thanks. – Nick Kahn Aug 22 '15 at 0:02
0

Here is how I able to achieve:

Apex:

public string RightSelectedListCount()
{
    return RightSelectedList.size();
}

Visualforce Page:

{!RightSelectedListCount()}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.