Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Array (Texts) button to add new answer row

  • francisakabo
  • francisakabo's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 6 months ago #216641 by francisakabo
Array (Texts) button to add new answer row was created by francisakabo
Hello, instead of defining absolute number of row entries for array (texts), I want respondent to able to enter multiple answers using an add button as shown in the attachment. I tried setting the relevance equation as shown in the attachment but it doesn't work. I am using LimeSurvey Version 3.15.5+181115 with a customized Fruity Theme.

 
The topic has been locked.
  • holch
  • holch's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 6 months ago #216647 by holch
Replied by holch on topic Array (Texts) button to add new answer row
You want to show the first one always, so don't use a relevance equation. From the second row onwards, you want to show the next row if the previous row contains something.

So basically, you need to write your relevance equation based on what is happening in the previous row, not in the current row as you did (according to your screenshot).

Also, I think you are using the wrong code. You have an array with 4 columns, so the subquestion of SQ001 has 4 different fields that can be filled, you will need to somehow access and check these fields, not just the subquestion.

Do a test and just write a text display question after this array with {Q2_SQ002}, I bet it is always empty. You will probably need something like Q2_SQ001_1 (depending on your answer codes). Have look in the question logic file how to access the fields.

I answer at the LimeSurvey forum in my spare time, I'm not a LimeSurvey GmbH employee.
No support via private message.

The following user(s) said Thank You: francisakabo
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 6 months ago #216648 by Joffm
Replied by Joffm on topic Array (Texts) button to add new answer row
Hi,
1. Your subquestion relevance ids not correct.
a. The first row is always displayed -> no relevance equation
b. The second row is displayed if the first one is not empty -> !is_empty(Q1_SQ001)
c. The third row is displayed if the second one is not empty -> !is_empty(Q1_SQ002)

2. You said you would like to have buttons.
Like this?
 



Here's the javascript.
This you have to enter in your question text (in source code mode)
Code:
<script>
$(document).ready(function() {
 
   // A function to add or remove rows of an Array (Multi Flexible)(Text) question
    function varLengthArray(qID) {
        
        if ($('#question'+qID+'').length > 0) {
            
            // The HTML content of the Add/Remove elements - modify as you wish
            var addContent = '[+] add row';
            var removeContent = '[-] remove row';
 
            // Create the Add and Remove elements &amp; insert them
            var el1 = document.createElement('div');
            el1.setAttribute('id','addButton'+qID);
            el1.setAttribute('class','btn btn-primary');
            document.body.appendChild(el1);
            var el2 = document.createElement('div');
            el2.setAttribute('id','removeButton'+qID);
            el2.setAttribute('class','btn btn-primary');
            document.body.appendChild(el2);
 
            // Move them to after the array
            $( 'div#addButton'+qID ).appendTo($( '#question' + qID + ' table.ls-answers' ).parent());
            $( 'div#removeButton'+qID ).appendTo($( '#question' + qID + ' table.ls-answers' ).parent());
 
            // Insert their HTML
            $( 'div#addButton'+qID ).html( addContent );
            $( 'div#removeButton'+qID ).html( removeContent );
 
            // Style the elements - you can modify here if you wish
            $( 'div#addButton'+qID ).css({
                'margin':'10px 0 0 10px',
                'padding':'1px',
                'text-align':'center',
                'width':'auto',
                'cursor':'pointer',
                'float':'left'
            });
 
            $( 'div#removeButton'+qID ).css({
                'margin':'10px 0 0 10px',
                'padding':'1px',
                'text-align':'center',
                'width':'auto',
                'cursor':'pointer',
                'float':'left'
            });
 
            // Initially hide the Remove element
            $( 'div#removeButton'+qID ).hide();
 
            // Call the functions below when clicked
            $( 'div#addButton'+qID ).click(function (event) {
                addRow(qID);
            });
            $( 'div#removeButton'+qID ).click(function (event) {
                removeRow(qID);
            });
 
            // Function to add a row, also shows the Remove element and hides the
            //Add element if all rows are shown
            function addRow(qID) {
                var arrayRow = '#question' + qID + ' table.ls-answers tr.subquestion-list';
                var rowCount = $( arrayRow ).size() - 1;
                $( arrayRow + '[name="hidden"]:first' ).attr('name', 'visible').show();
                $( 'div#removeButton'+qID ).show();
                if ( $( arrayRow + ':eq(' + rowCount + ')' ).attr('name') == 'visible' )  {
                    $( 'div#addButton'+qID ).hide();
                }
            }
 
            // Function to remove a row, also clears the contents of the removed row,
            // shows the Add element if the last row is hidden and hides the Remove
            // element if only the first row is shown
            function removeRow(qID) {
                var arrayRow = '#question' + qID + ' table.ls-answers tr.subquestion-list';
                var rowCount = $( arrayRow ).size() - 1;
                $( arrayRow + '[name="visible"]:last input[type="text"]' ).val('');
                $( arrayRow + '[name="visible"]:last' ).attr('name', 'hidden').hide();
                $( 'div#addButton'+qID ).show();
                if ( $( arrayRow + ':eq(1)' ).attr('name') == 'hidden' )  {
                    $( 'div#removeButton'+qID ).hide();
                }
            }
 
            // Just some initialization stuff
            var arrayRow = '#question' + qID + ' table.ls-answers tr.subquestion-list';
            var rowCount = '';
 
            // Initially hide all except first row or any rows with populated inputs
            $( arrayRow ).each(function(i) {
                if ( i > 0 ) {
                    // We also need to give the hidden rows a name cause IE doesn't
                    // recognize jQuery :visible selector consistently
                    $( this ).attr('name', 'hidden').hide();
 
                    $('input[type=text]', this).each(function(i) {
                        if ($(this).attr('value') != '') {
                            $(this).parents('tbody:eq(0)').attr('name', 'visible').show();
                            $( 'div#removeButton'+qID ).show();
                        }
                    });
                    rowCount = i;
                }
            });
        }
    }
 
    // Call the function with a question ID
    varLengthArray({QID});
 
});
 
</script>


And in your array you could use dropdowns for the two columns 
  • Is software licensed ? (yes/no)
  • Licence acquired by? (staff/department)
 

Best regards
Joffm


​​​​​​​

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 6 months ago - 3 years 6 months ago #216657 by Joffm
Replied by Joffm on topic Array (Texts) button to add new answer row
Oh, sorry,
reading @holch's answer I realized that I didn't care for the correct code of the cell.
I was thinking of a "multiple text input"
Of course it is
QuestionID_Y-scaleCode_X-scaleCode
E.g. Q1_Y002_X003

So you may decide when the next row is displayed.
If ALL cells are filled, you may use this relevance equation
count(self.sq_Y001)==4
count(self.sq_Y002)==4
...
Very important: Don't use the same codes for the x- and the y-scale.
Read the manual about "self" and "that" and about implemented functions.

And to add something to the "button" solution.
Here is no subquestion relevance needed.

Joffm

PS.
To show what you can do
1. Drop-downs in the array
2. Different columns widths
3. Column 4 depends on column 2 (only if "Yes" in column 2 column 4 is enabled).
 
 

File Attachment:

File Name: limesurvey... (1).lss
File Size:31 KB

 

Volunteers are not paid.
Not because they are worthless, but because they are priceless
Last edit: 3 years 6 months ago by Joffm.
The following user(s) said Thank You: francisakabo
The topic has been locked.
  • francisakabo
  • francisakabo's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 6 months ago - 3 years 6 months ago #216659 by francisakabo
Replied by francisakabo on topic Array (Texts) button to add new answer row
Thank you so much. Is it possible to set the number of sub questions dynamically as you click the add row button ? If the question is mandatory, 2 entries will not be accepted and more than 5 entries cannot be entered.
Last edit: 3 years 6 months ago by francisakabo. Reason: Additional information
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 6 months ago #216660 by Joffm
Replied by Joffm on topic Array (Texts) button to add new answer row
You have to create as many subquestions as you expect to be the maximum number of entries.
A "dynamical creation" of subquestions cannot be done because you cannot change the database of an activated survey

And obviously the question cannot be set to "mandatory".
You see, the whole stuff is only "look&feel" done by javascript.

You have to validate the question that a row is either filled or empty.

Joffm.

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The following user(s) said Thank You: francisakabo
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 6 months ago #216692 by Joffm
Replied by Joffm on topic Array (Texts) button to add new answer row
But, anyway,
you may read this and give it a try.

[url] forums.limesurvey.org/forum/design-issue...dd-additional-fields [/url]

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The topic has been locked.
Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose