Jump to content

Count number of times a dropdown option selected: Is this script close?

Recommended Posts

Have a basic form field, drop down, 3 options to select one of:

Form field "CME_Category": choices  stroke, cardiology, trauma

I just want to count number of occurrences of each of those three selection options (stroke, cardiology, trauma) and have them appear in the datapage report.  A basic COUNTIF function in Excel.

Im using this code I saw in action for a Yes/No field example, but cant get it to work within this situation.  Is it not happy with JQuery?

This is in FOOTER. Although I have split it in Header, Footer, HTML block etc and still doesnt work.  Im just guessing now ;^)  The DIV of course appears but it doesnt count.

<script type="text/javascript">
$('[@field:CME_Category]').change(function() {
    // get all selects
    var allSelects = $(CME_Category');

    // set values count by type
    var Stroke = 0;
    var Trauma = 0;
    var Cardiology = 0;
    // for each select increase count
    $.each(allSelects, function(i, s) {
        // increase count
        if($(s).val() == 'Stroke') { Stroke++; }
        if($(s).val() == 'Trauma') { Trauma++; }
        if($(s).val() == 'Cardiology') { Cardiology++; }

    // update count values summary

<div id="CME_Category">
    stroke  = <span class="cnt-stroke">0</span> <br>
    trauma  = <span class="cnt-trauma">0</span> <br>
    cardiology  = <span class="cnt-cardiology">0</span> <br>

Need these to show 2 and 1 for Cardiology and Stroke respectively.



Link to comment
Share on other sites

No problem.  Appreciate any help. 

Im trying to count from the field CME_category.  Grouping and Aggregate doesn't do the best job of counting individual entries or placing them in an easy to see stacked view like Im trying to achieve.  As these categories grow and the number entries get huge grouping and aggregate numbers will get spread out and some off screen as the entries grow.  A simple report tally in one spot will be helpful and more useful and easily digestible to me users.  The one above should read.  I only included 3 categories for test.

cardiology = 3
critical care = 2
EM = 2
Medicine = 1
stroke = 1 
trauma = 1 

I dont have editable turned on as I want it to be just a visual summary report, non editable.  Hope that makes sense.

Link to comment
Share on other sites

It does now, so you are trying to count how many values you have in the table. I dont think you are going the correct way. What would happen if this table is 3 pages long? How would you track the amounts in the div.

The way I see it you have these three options:

  1. Getting a custom JS function to add 1 to a JSON object for every single line and then do the counting to place the results in the div (now sure how it will work in large reports)
  2. API calls from a view to count records (You have a limitation of 1000 rows max per call so if your report is greater than 1000, multiple calls are needed)
  3. Use two datapages. One for the table and the other one should be tabular too with grouping to show results in the way you want to see it. This datapage should be deployed as an iFrame in the main Table footer.

In all ways, you may need to get external help in case you feel stuck, again it can be done but I dont want you to get fustrated.

Please also take into consideration that this is how I see it, I am sure there may be other people that can suggest something different and it would be ok as soon as it works.

Sorry for not being able to help you with a final coding but a further review is needed.

Link to comment
Share on other sites

Thats fine.  All that info is helpful.  I kind of like the frustration I think ;^)  After searching online and seeing many others trying to accomplish this seemingly easy function made me headstrong.  In excel this is just a COUNTIF= function - count occurences of X and put tally here.

I thought the code was close.  Think its missing something obviously as its not counting.  Was hoping it was just me incorrectly calling the field entries.  The example online I got this from was essentially doing the same thing but from an HTML forms input fields.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...