Have an idea?

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

How to make exclusive columns in a grid?

Hello, everyone!

The situation is the following. I have a question Q11. It is grid, 5rows, 6 columns, all rows except the 4th one are select checkboxes, the 4th one is an openend. Question direction - rows. All rows allows to check any number of checkboxes. The last row must be exclusive - i.e. if I check it all other checkboxes in this particular columns must be unchecked.

At the moment I use the following code.


        function SSI_CustomGraphicalCheckbox(graphicalObj, inputObj, bln) {
        var columnsArray =[/Q11_r([0-9]+)_c1/,/Q11_r([0-9]+)_c2/,/Q11_r([0-9]+)_c3/,/Q11_r([0-9]+)_c4/,/Q11_r([0-9]+)_c5/,/Q11_r([0-9]+)_c6/];
        for(var j = 1; j <=6; j++){
            var match = inputObj.name.match(columnsArray[j-1]);
            if (match !== null && bln) {
                if (Number(match[1]) == 5) {
                    for (var i = 1; i <= 4; i++) {
                        SSI_SetSelect('Q11_r'+ i + '_c' + j, false);
                else {SSI_SetSelect('Q11_r5_c'+ j, false);}


Is any better option?

Thank you in advance.

asked Feb 23 by Vasyl Bronze (725 points)
edited Feb 23 by Vasyl

1 Answer

+2 votes
Best answer
I'm glad you've asked.  It's always great to see users who try to not just write code that works, but who try to write the best code that works.  Perhaps I can offer some advice.

First, just a note on formatting. I would lessen the indentation of line 1 so that the function declaration is at the same indentation as the function's closing brace.  I would also recommend splitting line 14 into three lines of code, similar to the if statement a few lines above.  This is of course optional and may not seem like a big deal for smaller scripts, but forcing yourself to follow certain code styles can be a tremendous help with debugging in the long term.

Second, I see you are already using regular expressions and capture groups to determine the clicked on row.  You can take this a step further and add a second capture group to get the clicked on column.  This way you don't need the outer for loop.

Beyond that, I would just make some small touch ups.  We can use the QuestionName function instead of hard coding the question name, so the code won't break if you ever want to rename the question.  If we check "bln" before doing the regex tests, we can make the code a little bit faster.  We can choose to leave off the Number function and "!==" if we're comfortable with JavaScript's dynamic typing and truthiness.

I've taken a stab at making these changes and this was the result:

function SSI_CustomGraphicalCheckbox(graphicalObj, inputObj, bln) {
    if (bln) {
        var regex = /^[% QuestionName() %]_r([0-9]+)_c([0-9]+)$/;
        var match = inputObj.name.match(regex);
        if (match) {
            var row = match[1];
            var column = match[2];
            if (row == 5) {
                for (var i = 1; i <= 3; i++) {
                    SSI_SetSelect('[% QuestionName() %]_r' + i + '_c' + column, false);
            else {
                SSI_SetSelect('[% QuestionName() %]_r5_c' + column, false);

If you have questions about my changes, I'd love to answer them.
answered Feb 25 by Zachary Platinum Sawtooth Software, Inc. (120,900 points)
selected Feb 26 by Vasyl
Dear Zachary,

Many thanks for your detailed reply. I will definitely use your recommendations.