Have an idea?

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

Is there a value stored from a numeric grid Totals Column?

Is there a way to display the value from a numeric grid Totals Column, and also to use that value in a subsequent Constant Sum question?

Thanks.
asked Mar 11, 2016 by Jan Rasmussen

1 Answer

+1 vote
Hi Jan,

You could use a hidden free format variable to store the value from the total field, but I found it easier to just use the numeric fields themselves.  The below JavaScript can be used on the same page as the grid question to display the total in real time.  Just be sure to replace 'gridquestion' with your question's name and update the 'r1'/'c1' stuff with the relevant rows/columns of your question.

Sum: <div id="gridsum"><span>0</span></div>

<script type="text/javascript">
$(document).ready(function(){

    var updateSum = function(){
        var column1 = Number($('#gridquestion_r1_c1').val()) || 0;
        var column2 = Number($('#gridquestion_r1_c2').val()) || 0;
        var column3 = Number($('#gridquestion_r1_c3').val()) || 0;
        var column4 = Number($('#gridquestion_r1_c4').val()) || 0;
        var sum = column1 + column2 + column3 + column4;
        $('#gridsum span').text(sum);
    };

    $('#gridquestion_r1_c1').keyup(function(){
        updateSum();
    });
    $('#gridquestion_r1_c2').keyup(function(){
        updateSum();
    });
    $('#gridquestion_r1_c3').keyup(function(){
        updateSum();
    });
    $('#gridquestion_r1_c4').keyup(function(){
        updateSum();
    });
    
})
</script>


If you're on a different page that comes after the grid question, the total can be easily displayed with code to this effect:

Sum: <div id="gridsum"><span>0</span></div>

<script type="text/javascript">
$(document).ready(function(){
    var total = [% gridquestion_r1_c1 + gridquestion_r1_c2 + gridquestion_r1_c3 + gridquestion_r1_c4 %];
    $('#gridsum span').text(total);
})
</script>


For the constant sum question, you can set the total value with Sawtooth Script:

[% gridquestion_r1_c1 + gridquestion_r1_c2 + gridquestion_r1_c3 + gridquestion_r1_c4 %]
answered Mar 11, 2016 by Zachary Platinum Sawtooth Software, Inc. (94,150 points)
Thank you Zachary -- this was very helpful.
...