Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Array (Text): Calculate 2 columns of sub-questions

  • squidy
  • squidy's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
1 year 4 months ago - 1 year 4 months ago #245517 by squidy
Your LimeSurvey version: 5.3
Own server or LimeSurvey hosting: Own
Survey theme/template: fruity
==================
My array (text) question is as follows:

Y-Scale
Line 1
Line 2
Line 3
Line 4
Line 5

X-Scale
Column 1: text
Column 2: number
Column 3: dropdown menu
Column 4: number
Column 5: number

I want column 5 (total price) to receive the result of column 2 (quantity) * column 4 (unit price)..
Column 5 = Column 2 * Column 4

 

Is there a way to do this?

Using LS 5.x
Attachments:
Last edit: 1 year 4 months ago by squidy.

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
1 year 4 months ago - 1 year 4 months ago #245518 by Joffm
Hi,
do you really want to sum these two colimns?
I should say, you want to multiply.

An easy way is to insert this calculation into the subquestion text, just ExpressionScript
If you want to use this result later in the survey, you may use an equation to store it into this question.

Quick example:
 
Something like
{if(!is_empty(ITENS_ITEM01_SQ002) AND !is_empty(ITENS_ITEM01_SQ004),"VALOR TOTAL R$: "+round(ITENS_ITEM01_SQ002*ITENS_ITEM01_SQ004,2),"")}
and some styling by css

But you have set the decimal of your inputmask to "dot", It doesn't work with "comma".
Furthermore you have to remove the thousand separator.
Or you use
{if(!is_empty(ITENS_ITEM01_SQ002) AND !is_empty(ITENS_ITEM01_SQ004),"VALOR TOTAL R$: "+round(ITENS_ITEM01_SQ002*str_replace(",",".",str_replace(".","",ITENS_ITEM01_SQ004)),2),"")}

Joffm


 

Volunteers are not paid.
Not because they are worthless, but because they are priceless
Last edit: 1 year 4 months ago by Joffm.

Please Log in to join the conversation.

  • squidy
  • squidy's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
1 year 4 months ago #245527 by squidy
Thanks again, Joffm...

Any chance to change the answertext column to the end of the line?

Using LS 5.x

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
1 year 4 months ago - 1 year 4 months ago #245593 by Joffm
Hi,
if you insist on showing the total here, well, with a small javascript snippet




And the script:
Code:
<script type="text/javascript" charset="utf-8">
$(document).on('ready pjax:scriptcomplete',function(){
    var thisQuestion = $('#question{QID}');
 
    $('.answer-item.answer_cell_X002', thisQuestion).addClass('quantity');
    $('.answer-item.answer_cell_X004', thisQuestion).addClass('amount');
    $('.answer-item.answer_cell_X005', thisQuestion).addClass('sum');
 
    $('.sum input[type="text"]', thisQuestion).prop('readonly', true);
  
    $('.quantity', thisQuestion).on('change', function(i) {
      if($(this).closest('.answer-item').hasClass('answer_cell_X002')) {
         handleSum($(this));
      }
    });
    $('.amount', thisQuestion).on('change', function(i) {
      if($(this).closest('.answer-item').hasClass('answer_cell_X004')) {
         handleSum($(this));
      }
    });
 
    function handleSum(thisSelect) {
      var thisRow = $(thisSelect).closest('tr.subquestion-list');
      var item2 = $('.answer_cell_X002', thisRow);
      var item4 = $('.answer_cell_X004', thisRow);
      var item5 = $('.answer_cell_X005', thisRow);
      var w1=$('input:text', item2).val();
      var w2=$('input:text', item4).val();
      var w3=(w1*w2).toFixed(2);;
      if(w3>0) {
        $('input:text', item5).val('R$ ' + w3).trigger('change');
      } else {
        $('input:text', item5).val('').trigger('change');
      }
    }
});
</script>

You see, I coded the x-axis "X001", "X002",...
As I am not familiar with javascript I merged parts from yet available scripts here.
Seems to work.
Other - more experienced - may improve 

Joffm

BTW: I know that it should be "amount" 

Volunteers are not paid.
Not because they are worthless, but because they are priceless
Last edit: 1 year 4 months ago by Joffm.

Please Log in to join the conversation.

  • squidy
  • squidy's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
1 year 4 months ago #245612 by squidy
Very cool, Joffm. Thank you very much.

Using LS 5.x

Please Log in to join the conversation.

Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose