Show sum of the selected options in form


#1

Hi I have an application form that includes a select list. Data from the select list are stored in the data collection.

I would like to show the sum of the selected options attributable to each choice found in the form field.

Does anyone know how to do it.


#2

Using ViewModel:

<body>
  <select size="5" multiple data-bind="
    options: items, 
    value: selectedValues, 
    optionsValue: value, 
    optionsText: letter"></select>
  <p>
    Sum: <span data-bind="text: sum"></span>
  </p>
</body>
Template.body.viewmodel({
  // here items is an array but you can use a function
  // that returns the results of a collection query.
  items: [
    { letter: "A", value: 1 },
    { letter: "B", value: 2 },
    { letter: "C", value: 3 }
  ],
  selectedValues: [],
  sum: function () {
    return this.selectedValues().reduce(function(p, c) { return parseInt(p) + parseInt(c) }, 0);
  }
});

#3

Maybe we should not be expressed.
The collection of data I have a property called “gender” and it may have value: “female” or “male”.

My question is, what to do to show you on the numerical value indicating how many times the property of “gender” is “woman” and how many times the property of “gender” is “man”.