From f6185fad01bce698af2cfac9f3337cf7ffb9b8ea Mon Sep 17 00:00:00 2001 From: tsv2013 Date: Wed, 13 Mar 2024 12:45:10 +0300 Subject: [PATCH] Fixed https://github.com/surveyjs/survey-analytics/issues/403 - Tabulator and Checkboxes - The Comment box value replaces the Other option value --- src/question_baseselect.ts | 21 ++++++++++++++------- tests/question_baseselecttests.ts | 17 +++++++++++++++++ 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/src/question_baseselect.ts b/src/question_baseselect.ts index 71fb284bbd..157e8877a4 100644 --- a/src/question_baseselect.ts +++ b/src/question_baseselect.ts @@ -1131,15 +1131,15 @@ export class QuestionSelectBase extends Question { protected getDisplayArrayValue(keysAsText: boolean, value: any, onGetValueCallback?: (index: number) => any): string { var items = this.visibleChoices; - var strs = []; - const vals = []; + var strs = [] as Array; + const vals = [] as Array; for (var i = 0; i < value.length; i++) { vals.push(!onGetValueCallback ? value[i] : onGetValueCallback(i)); } - if(Helpers.isTwoValueEquals(this.value, vals)) { - this.getMultipleSelectedItems().forEach(item => strs.push(this.getItemDisplayValue(item))); + if (Helpers.isTwoValueEquals(this.value, vals)) { + this.getMultipleSelectedItems().forEach((item, index) => strs.push(this.getItemDisplayValue(item, vals[index]))); } - if(strs.length === 0) { + if (strs.length === 0) { for (var i = 0; i < vals.length; i++) { let valStr = this.getChoicesDisplayValue(items, vals[i]); if (valStr) { @@ -1149,8 +1149,15 @@ export class QuestionSelectBase extends Question { } return strs.join(", "); } - private getItemDisplayValue(item: ItemValue): string { - if(item === this.otherItem && this.comment) return this.comment; + private getItemDisplayValue(item: ItemValue, val?: any): string { + if (item === this.otherItem) { + if (this.hasOther && this.showCommentArea && !!val) { + return val; + } + if (this.comment) { + return this.comment; + } + } return item.locText.textOrHtml; } private getFilteredChoices(): Array { diff --git a/tests/question_baseselecttests.ts b/tests/question_baseselecttests.ts index 192fd2a03b..688bda5ce6 100644 --- a/tests/question_baseselecttests.ts +++ b/tests/question_baseselecttests.ts @@ -1814,3 +1814,20 @@ QUnit.test("Do not show show choices in designer", function(assert) { assert.equal(question.visibleChoices.length, 5 + 1 + 3, "Show choices in designer, #1"); settings.supportCreatorV2 = false; }); +QUnit.test("question checkbox displayValue() with other and comment", (assert) => { + const q1 = new QuestionCheckboxModel("q1"); + q1.fromJSON({ + "type": "checkbox", + "name": "q1", + "showCommentArea": true, + "commentText": "Comment", + "choices": [ + "Item 1", + "Item 2", + "Item 3" + ], + "showOtherItem": true + }); + q1.value = ["Item 1", "Item 2", "Other Value"]; + assert.deepEqual(q1.displayValue, "Item 1, Item 2, Other Value", "Other value should be kept"); +});