Have an idea?

Visit Sawtooth Software Feedback to share your ideas on how we can improve our products.

Sum in rows & columns in a constructed question

Hi All!
I got  a grid constructed question where in table in rows we summarize answers. Would need to sum up also answers in columns. The issue is that grid question enables a sum only in one direction (rows or columns).
Table consists of 2 columns and 7 rows and I would like to avoid using  a free format question as constructed question does the trick with selected answers from a previous question.  My idea is to add one  more row called 'total' (row no 8) then disable it using below code
<script type="text/javascript">
 document.getElementById("Q1b_r8_c1").disabled = 'true';
 document.getElementById("Q1b_r8_c2").disabled = 'true';
</script>
and pipe-in sum of columns into Q1b_r8_c1 and Q1b_r8_c2.
Exp. Q1b_r1_c1+...+ Q1b_r7_c1=sum1 -> Q1b_r8_c1
Q1b_r1_c2+...+ Q1b_r7_c2=sum2 -> Q1b_r8_c2
but how to do it ..... ?

Would be appreciated for any advice !
robson
asked Jul 21, 2016 by robson Bronze (635 points)
retagged Jul 21, 2016 by Walter Williams

1 Answer

+2 votes
 
Best answer
Hi Robson,

it seems you are still using v7. Then you are correct, you have to create row 8 here to store total. Then use below code in footer

<script type="text/javascript">
 document.getElementById("Q1b_r8_c1").disabled = 'true';
 document.getElementById("Q1b_r8_c2").disabled = 'true';
</script>

<script type="text/javascript"> 
var obj11 = SSI_GetFormObject("Q1b_r1_c1"); 
var obj12 = SSI_GetFormObject("Q1b_r2_c1"); 
var obj13 = SSI_GetFormObject("Q1b_r3_c1"); 
var obj14 = SSI_GetFormObject("Q1b_r4_c1"); 
var obj15 = SSI_GetFormObject("Q1b_r5_c1"); 
var obj16 = SSI_GetFormObject("Q1b_r6_c1"); 
var obj17 = SSI_GetFormObject("Q1b_r7_c1"); 

obj11.addEvent("keyup", hello1); 
obj12.addEvent("keyup", hello1); 
obj13.addEvent("keyup", hello1); 
obj14.addEvent("keyup", hello1); 
obj15.addEvent("keyup", hello1); 
obj16.addEvent("keyup", hello1); 
obj17.addEvent("keyup", hello1); 

var obj21 = SSI_GetFormObject("Q1b_r1_c2"); 
var obj22 = SSI_GetFormObject("Q1b_r2_c2"); 
var obj23 = SSI_GetFormObject("Q1b_r3_c2"); 
var obj24 = SSI_GetFormObject("Q1b_r4_c2"); 
var obj25 = SSI_GetFormObject("Q1b_r5_c2"); 
var obj26 = SSI_GetFormObject("Q1b_r6_c2"); 
var obj27 = SSI_GetFormObject("Q1b_r7_c2"); 

obj21.addEvent("keyup", hello2); 
obj22.addEvent("keyup", hello2); 
obj23.addEvent("keyup", hello2); 
obj24.addEvent("keyup", hello2); 
obj25.addEvent("keyup", hello2); 
obj26.addEvent("keyup", hello2); 
obj27.addEvent("keyup", hello2); 

</script> 


then you have to use below script in Advance ->head tag.

<script type="text/javascript"> 
function hello1()
{

    document.getElementById("Q1b_r8_c1").value=SSI_GetValue("Q1b_r1_c1")+SSI_GetValue("Q1b_r2_c1")+SSI_GetValue("Q1b_r3_c1")+SSI_GetValue("Q1b_r4_c1")+SSI_GetValue("Q1b_r5_c1")+SSI_GetValue("Q1b_r6_c1")+SSI_GetValue("Q1b_r7_c1");
}
function hello2()
{

    document.getElementById("Q1b_r8_c2").value=SSI_GetValue("Q1b_r1_c2")+SSI_GetValue("Q1b_r2_c2")+SSI_GetValue("Q1b_r3_c2")+SSI_GetValue("Q1b_r4_c2")+SSI_GetValue("Q1b_r5_c2")+SSI_GetValue("Q1b_r6_c2")+SSI_GetValue("Q1b_r7_c2");
}
</script>


Let me know, If you need more help or If you are using another version.
answered Jul 21, 2016 by Rajesh Rana Gold (23,530 points)
selected Jul 22, 2016 by robson
Thank you RanaRajesh!
Yes, Im using v7.
code makes sense  but works only when all rows are enabled. But the table is dynamic (constructed-list) and code fails when selected is only part of rows... Thank you, Robson
in This case,  you just need to put code in footer.


 
<script type="text/javascript">
 document.getElementById("Q1b_r8_c1").disabled = 'true';
 document.getElementById("Q1b_r8_c2").disabled = 'true';
</script>

<script type="text/javascript"> 
if([%ListHasParentMember(Q1bConstructed,1)%]==1)
{
    var obj11 = SSI_GetFormObject("Q1b_r1_c1"); 
    obj11.addEvent("keyup", hello1); 
    var obj21 = SSI_GetFormObject("Q1b_r1_c2");
    obj21.addEvent("keyup", hello2); 
}
if([%ListHasParentMember(Q1bConstructed,2)%]==1)
{
    var obj12 = SSI_GetFormObject("Q1b_r2_c1"); 
    obj12.addEvent("keyup", hello1); 
    var obj22 = SSI_GetFormObject("Q1b_r2_c2");
    obj22.addEvent("keyup", hello2); 
}
if([%ListHasParentMember(Q1bConstructed,3)%]==1)
{
    var obj13 = SSI_GetFormObject("Q1b_r3_c1"); 
    obj13.addEvent("keyup", hello1); 
    var obj23 = SSI_GetFormObject("Q1b_r3_c2");
    obj23.addEvent("keyup", hello2); 
}
if([%ListHasParentMember(Q1bConstructed,4)%]==1)
{
    var obj14 = SSI_GetFormObject("Q1b_r4_c1"); 
    obj14.addEvent("keyup", hello1); 
    var obj24 = SSI_GetFormObject("Q1b_r4_c2");
    obj24.addEvent("keyup", hello2); 
}
if([%ListHasParentMember(Q1bConstructed,5)%]==1)
{
    var obj15 = SSI_GetFormObject("Q1b_r5_c1"); 
    obj15.addEvent("keyup", hello1); 
    var obj25 = SSI_GetFormObject("Q1b_r5_c2");
    obj25.addEvent("keyup", hello2); 
}
if([%ListHasParentMember(Q1bConstructed,6)%]==1)
{
    var obj16 = SSI_GetFormObject("Q1b_r6_c1"); 
    obj16.addEvent("keyup", hello1); 
    var obj26 = SSI_GetFormObject("Q1b_r6_c2");
    obj26.addEvent("keyup", hello2); 
}
if([%ListHasParentMember(Q1bConstructed,7)%]==1)
{
    var obj17 = SSI_GetFormObject("Q1b_r7_c1"); 
    obj17.addEvent("keyup", hello1); 
    var obj27 = SSI_GetFormObject("Q1b_r7_c2");
    obj27.addEvent("keyup", hello2); 
}
</script> 


and in advance.
<script type="text/javascript"> 
function hello1()
{
    var flag=0;
    if([%ListHasParentMember(Q1bConstructed,1)%]==1)
    {
        flag=flag+SSI_GetValue("Q1b_r1_c1");
    }
    if([%ListHasParentMember(Q1bConstructed,2)%]==1)
    {
        flag=flag+SSI_GetValue("Q1b_r2_c1");
    }
    if([%ListHasParentMember(Q1bConstructed,3)%]==1)
    {
        flag=flag+SSI_GetValue("Q1b_r3_c1");
    }
    if([%ListHasParentMember(Q1bConstructed,4)%]==1)
    {
        flag=flag+SSI_GetValue("Q1b_r4_c1");
    }
    if([%ListHasParentMember(Q1bConstructed,5)%]==1)
    {
        flag=flag+SSI_GetValue("Q1b_r5_c1");
    }
    if([%ListHasParentMember(Q1bConstructed,6)%]==1)
    {
        flag=flag+SSI_GetValue("Q1b_r6_c1");
    }
    if([%ListHasParentMember(Q1bConstructed,7)%]==1)
    {
        flag=flag+SSI_GetValue("Q1b_r7_c1");
    }

    document.getElementById("Q1b_r8_c1").value=flag;
}
function hello2()
{

        var flag1=0;
    if([%ListHasParentMember(Q1bConstructed,1)%]==1)
    {
        flag1=flag1+SSI_GetValue("Q1b_r1_c2");
    }
    if([%ListHasParentMember(Q1bConstructed,2)%]==1)
    {
        flag1=flag1+SSI_GetValue("Q1b_r2_c2");
    }
    if([%ListHasParentMember(Q1bConstructed,3)%]==1)
    {
        flag1=flag1+SSI_GetValue("Q1b_r3_c2");
    }
    if([%ListHasParentMember(Q1bConstructed,4)%]==1)
    {
        flag1=flag1+SSI_GetValue("Q1b_r4_c2");
    }
    if([%ListHasParentMember(Q1bConstructed,5)%]==1)
    {
        flag1=flag1+SSI_GetValue("Q1b_r5_c2");
    }
    if([%ListHasParentMember(Q1bConstructed,6)%]==1)
    {
        flag1=flag1+SSI_GetValue("Q1b_r6_c2");
    }
    if([%ListHasParentMember(Q1bConstructed,7)%]==1)
    {
        flag1=flag1+SSI_GetValue("Q1b_r7_c2");
    }

    document.getElementById("Q1b_r8_c2").value=flag1;
}
</script>
Thank you Rajesh Rana, It really works !
Im very grateful!
Robson
...