Welcome, Guest
Username: Password: Remember me

TOPIC: Save and round totals

Save and round totals 2 years 7 months ago #75914

  • TAdams
  • TAdams's Avatar
  • OFFLINE
  • Junior Lime
  • Posts: 24
  • Thank you received: 1
  • Karma: 0
I have an array with 4 columns of numeric values and a total on rows column. I have used .tofixed(1) on the textboxes within the array to round to 1 decimal place. However, I am not able to use this operation on the totals boxes. I would also like to save these totals boxes within the database. Do you have any suggestions?

Thank you in advance.
The administrator has disabled public write access.

Re: Save and round totals 2 years 7 months ago #75923

  • DenisChenu
  • DenisChenu's Avatar
  • OFFLINE
  • Moderator Lime
  • Posts: 6429
  • Thank you received: 839
  • Karma: 249
Hello,

Think the best is to add a "TOTAL" subquestion.

For this total : put a readonly attribute at document loading .

Each time the answer of the column change, calculate the sum and put it in the "TOTAL" value.

Some javascripting ;).

Denis
The administrator has disabled public write access.

Re: Save and round totals 2 years 7 months ago #75932

  • TAdams
  • TAdams's Avatar
  • OFFLINE
  • Junior Lime
  • Posts: 24
  • Thank you received: 1
  • Karma: 0
I will do that Shnoulle. I wasn't sure if there was a quick and simple option I was over looking but javascripting it is!
Thanks again :)
The administrator has disabled public write access.

Re: Save and round totals 2 years 7 months ago #75936

  • TMSWhite
  • TMSWhite's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 759
  • Thank you received: 82
  • Karma: 36
In 1.92, you can do this without JavaScript.

You can create a hidden Equation question for each total you want to compute and store within the database. Say your question is "q1", your rows are named 1-4, and your sub-questions are named a-d. To compute the row total (let's call it q1_a_rowt), and round it to one decimal place, you would put this in the question field of that equation:
{round(sum(q1_a_1, q1_a_2, q1_a_3, q1_a_4),1)}

Then, assuming you did the same for rows b-d, the grand total would be:
{round(sum{q1_a_rowt, q1_b_rowt, q1_c_rowt, q1_d_rowt),1)}

Each of those equations values are automatically re-computed and stored in the database.
The administrator has disabled public write access.

Re: Save and round totals 2 years 7 months ago #75942

  • TAdams
  • TAdams's Avatar
  • OFFLINE
  • Junior Lime
  • Posts: 24
  • Thank you received: 1
  • Karma: 0
Thanks TMS, if 1.92 was an option for me right now I would happily take your advice.
I have turned off the show totals for rows option in the settings. However now when I preview the question and enter some trial numbers I get the error:
Webpage error details
Message: 'null' is null or not an object
Line: 730
Char: 4
Code: 0
URI: xxx.limequery.org/scripts/survey_runtime.js

I have traced the runtime.js script back to a javascript function calc(e), calculates a row or col or both runs the grand totals if required. So it looks to me like while I've removed the calculate totals option the script is searching for the objects to calculate. This is preventing my new sum function from running properly.
Any suggestions?
The administrator has disabled public write access.

Re: Save and round totals 2 years 7 months ago #76004

  • TMSWhite
  • TMSWhite's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 759
  • Thank you received: 82
  • Karma: 36
TAdams-

Download 1.92 and take a look at it's implementation of survey_runtime.js It has the rounding built into it (plus support for using comma as the radix separator). You can adapt the 1.92 version to your needs for 1.91.

So, if you use that version, you don't need your custom JavaScript function - you can have calc() do the rounding for you. You'll see lines like round(q6,12). Change those to round(qt,1) to round to one decimal place.

/Tom
The administrator has disabled public write access.

Re: Save and round totals 2 years 7 months ago #76014

  • TAdams
  • TAdams's Avatar
  • OFFLINE
  • Junior Lime
  • Posts: 24
  • Thank you received: 1
  • Karma: 0
Thanks again Tom. Due to my hosting service the upgrade is not available to me at this time. I want to make the changes to my survey in the version it's going to go out in at the end of the month.
The administrator has disabled public write access.

Re: Save and round totals 2 years 7 months ago #76315

  • TAdams
  • TAdams's Avatar
  • OFFLINE
  • Junior Lime
  • Posts: 24
  • Thank you received: 1
  • Karma: 0
The solution I found:
Create the round function:
function Rnd1() {
var T1=document.getElementsByName("Textbox-total-name_total")[0].value;
T1=parseFloat(T1).toFixed(1); //rounds to one decimal place
document.getElementsByName("Textbox-total-name_total")[0].value=T1;
}
Call Rnd1 function in an onchange event in any/all textboxes in the array.
Limesurvey automatically assigns the name of the textboxes in the total column and does not provide the textbox ID, so you need to get the element by name. Find the name of the total textbox in the question preview, view source.

Hope this helps.
The administrator has disabled public write access.
Moderators: ITEd
Time to create page: 0.278 seconds
Donation Image