Hello to everybody!
I have the following code in new.html.erb allowing a user to
select more questions (item) for creating a questionnaire:
...
<% for item in @itemss %>
<%= check_box_tag "questionnaire[item_ids][]", item.id,
@questionnaire.items.include?(item) %>
<a> <%= item.kind %> <span> <%= item.question
%> </span> </a>
- "<%= item.denomination %>" - p.ti <%= item.score
%> <br/>
<% end %>
...
I would like to show, while the user is making his choices, the
calculation of:
- the number of selected items
- the sum of the scores (each item has its own score)
any suggestions?
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
use jquery and bind click event to some calculcation function. you can set some
value or metadata to count with
eg
= check_box_tag ''name'', value, checked?,
:''data-count'' => 123, :class => ''sums''
and you jquery
var result; // global variable
jQuery(''.sums'').each(function() {
jQuery(this).bind(''click'', function() {
result+= parseInt(jQuery(this).attr(''data-count'')) *
(jQuery(this).attr(''checked'')) ? 1 : -1;
});
});
in data-count you can have the value you want to inc/dec from final result
when user clicks, jquery will evaluate checked state, when checked,
it''ll increase the result, when unchecked, it''ll decrease
tom
On Jul 24, 2011, at 20:15 , mr ma wrote:
> Hello to everybody!
>
> I have the following code in new.html.erb allowing a user to
> select more questions (item) for creating a questionnaire:
>
> ...
> <% for item in @itemss %>
> <%= check_box_tag "questionnaire[item_ids][]",
item.id,
> @questionnaire.items.include?(item) %>
> <a> <%= item.kind %> <span> <%=
item.question %> </span> </a>
> - "<%= item.denomination %>" - p.ti <%=
item.score %> <br/>
> <% end %>
> ...
>
> I would like to show, while the user is making his choices, the
> calculation of:
> - the number of selected items
> - the sum of the scores (each item has its own score)
>
> any suggestions?
>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
> To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
--
==============================================================================Tomas
Meinlschmidt, MS {MCT, MCP+I, MCSE, AER}, NetApp Filer/NetCache
www.meinlschmidt.com www.maxwellrender.cz www.lightgems.cz
==============================================================================
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
Thank you very much Tomas!!! I have not yet used jquery, I will study it a little bit... Marco -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hi Tomas,
I studied a little Javascript and I write this script but
the variable ''result'' is undefined, can you help me? ;-)
=> new.html.erb
...
<p>
<% @itemss.each do |item| %>
<%= check_box_tag "questionnaire[item_ids][]", item.id,
@questionnaire.items.include?(item), :''data-count''
=>
item.score, :class =>
''sums'', :onClick => "calculate();" %>
<a> <%= item.kind %> <span> <%= item.question %>
</span> </a>
- "<%= item.denomination %>" - p.ti <%= item.score
%> <br/>
<% end %>
</p>
...
=> application.html.erb
...
<script language="javascript"
type="text/javascript">
function calculate() {
var sezione = document.getElementById(''scores'');
var result;
n_item = jQuery("input:checkbox:checked").length;
jQuery(''.sums'').each(function() {
jQuery(this).bind(''click'', function() {
result+= parseInt(jQuery(this).attr(''data-count'')) *
(jQuery(this).attr(''checked'')) ? 1 : -1;
});
});
sezione.innerHTML = "N. Item selezionati: " +n_item +" Tot.
punti:
" +result;
}
</script>
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.