Have an idea?

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

Setting a Predefined Value for an Entire Column in a Grid with Constructed Lists

Hi there,

I have a grid question with constructed lists for both the columns and the rows. So, both the number of columns and rows depend on previous answers. I would like to set the values for the entire first column of the main grid to 100. The maximum number of rows can be 15 and the max number of columns is 8.

How do I display 100 for the entire first column with a grid having constructed lists? Could you help me out?

Thanks a lot!
asked Apr 19 by Elif

1 Answer

0 votes
 
Best answer
There are a few ways you could approach this.

One option would be to use Sawtooth Script functions.  The ListValuesArray function returns all the items of the list in a JavaScript array, so we could use that to get all the rows.  ListValue works similarly - returning just the list item at a certain position - so we could use that to determine what the first column is.

[% ListValuesArray(ConstRowsList) %] // returns array, like [4, 10, 5]
[% ListValue(ConstColsList) %] // returns first item of the constructed list, like 4


But this solution will require upkeep if we ever change the grid question to use different lists.  There are two hidden input elements on every page with a grid question that represent the rows and columns of the grid.  If we use these instead, our solution can just work all the time.

var rows = $('input[name="hid_row_list_[% QuestionName() %]"]').val().split(',');
var columns = $('input[name="hid_col_list_[% QuestionName() %]"]').val().split(',');


Going down that route, this would be my solution:

<script>
$(document).ready(function(){
    var rows = $('input[name="hid_row_list_[% QuestionName() %]"]').val().split(',');
    var columns = $('input[name="hid_col_list_[% QuestionName() %]"]').val().split(',');
    
    rows.forEach(function(row){
        var variable = '[% QuestionName() %]_r' + row + '_c' + columns[0];
        if (!$('#' + variable).val()) {
            $('#' + variable).val(100);
        }
    });
})
</script>
answered Apr 19 by Zachary Platinum Sawtooth Software, Inc. (171,525 points)
Thank you, Zachary! It worked perfectly once again
...