Skip to content

Commit

Permalink
Do not clear data for invisible (filtered) dynamic panels fix #7763
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewtelnov committed Jan 30, 2024
1 parent 01351a9 commit e4cd0df
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 13 deletions.
20 changes: 8 additions & 12 deletions src/question_baseselect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1502,24 +1502,20 @@ export class QuestionSelectBase extends Question {
private get hasChoicesUrl(): boolean {
return this.choicesByUrl && !!this.choicesByUrl.url;
}
public clearIncorrectValues() {
if (!this.hasValueToClearIncorrectValues()) return;
if(this.carryForwardQuestion && !this.carryForwardQuestion.isReady) return;
if (
!!this.survey &&
this.survey.questionsByValueName(this.getValueName()).length > 1
)
return;
if (this.hasChoicesUrl &&
(!this.choicesFromUrl || this.choicesFromUrl.length == 0)
)
return;
public clearIncorrectValues(): void {
if (!this.hasValueToClearIncorrectValues() || !this.canClearIncorrectValues()) return;
if (this.clearIncorrectValuesCallback) {
this.clearIncorrectValuesCallback();
} else {
this.clearIncorrectValuesCore();
}
}
private canClearIncorrectValues(): boolean {
if(this.carryForwardQuestion && !this.carryForwardQuestion.isReady) return false;
if (!!this.survey && this.survey.questionsByValueName(this.getValueName()).length > 1) return false;
if (this.hasChoicesUrl && (!this.choicesFromUrl || this.choicesFromUrl.length == 0)) return false;
return true;
}
protected hasValueToClearIncorrectValues(): boolean {
if(!!this.survey && this.survey.keepIncorrectValues) return false;
return !this.keepIncorrectValues && !this.isEmpty();
Expand Down
4 changes: 3 additions & 1 deletion src/question_paneldynamic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1702,10 +1702,12 @@ export class QuestionPanelDynamicModel extends Question
}
private clearValueInPanelsIfInvisible(reason: string): void {
for (var i = 0; i < this.panelsCore.length; i++) {
var questions = this.panelsCore[i].questions;
const panel = this.panelsCore[i];
var questions = panel.questions;
this.isSetPanelItemData = {};
for (var j = 0; j < questions.length; j++) {
const q = questions[j];
if(q.visible && !panel.isVisible) continue;
q.clearValueIfInvisible(reason);
this.isSetPanelItemData[q.getValueName()] = this.maxCheckCount + 1;
}
Expand Down
138 changes: 138 additions & 0 deletions tests/question_paneldynamic_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6677,3 +6677,141 @@ QUnit.test("panel dynamic & addPanel/removePanel with non-build panels, #7693",
assert.equal(dynamicPanel.panelCount, 2, "panelCount #3");
assert.equal(dynamicPanel.getQuestionFromArray("q2", 0).name, "q2", "Panels are created");
});
QUnit.test("panel dynamic & panel visibleIf & checkbox vs carry forward, #7693", function (assert) {
const survey = new SurveyModel({
"pages": [
{
"elements": [
{
"type": "paneldynamic",
"name": "aboutMe",
"valueName": "household",
"templateElements": [
{
"type": "text",
"name": "firstName"
},
{
"type": "text",
"name": "lastName"
},
{
"type": "expression",
"name": "fullName",
"visible": false,
"expression": "{panel.firstName} + ' ' + {panel.lastName}"
}
],
"panelCount": 2,
"minPanelCount": 1,
"templateVisibleIf": "{panelIndex} = 0"
}
]
},
{
"elements": [
{
"type": "paneldynamic",
"name": "addChildren",
"valueName": "household",
"isRequired": true,
"templateElements": [
{
"type": "text",
"name": "childFirstName",
"valueName": "firstName"
},
{
"type": "text",
"name": "childLastName",
"valueName": "lastName"
},
{
"type": "expression",
"name": "childFullName",
"visible": false,
"valueName": "fullName",
"expression": "{panel.firstName} + ' ' + {panel.lastName}"
}
],
"panelCount": 1,
"minPanelCount": 1,
"templateVisibleIf": "{panelIndex} > 0"
}
]
},
{
"elements": [
{
"type": "checkbox",
"name": "addIncomeFor",
"choicesFromQuestion": "addChildren",
"choiceValuesFromQuestion": "fullName"
}
]
},
{
"elements": [
{
"type": "paneldynamic",
"name": "householdIncome",
"valueName": "household",
"templateElements": [
{
"type": "text",
"name": "question1"
}
],
"templateTitle": "{panel.fullName}",
"panelsState": "collapsed",
"templateVisibleIf": "{addIncomeFor} contains {panel.fullName}"
}
]
}
]
});
survey.data = {
"household": [
{
"firstName": "first1",
"fullName": "first1 last1",
"lastName": "last1",
"question1": "text1"
},
{
"firstName": "first2",
"fullName": "first2 last2",
"lastName": "last2"
},
{
"firstName": "first3",
"fullName": "first3 last3",
"lastName": "last3",
"question1": "test3"
}
],
"addIncomeFor": [
"first1 last1",
"first3 last3"
]
};
survey.doComplete();
assert.deepEqual(survey.data, {
"household": [
{
"firstName": "first1",
"lastName": "last1",
"question1": "text1"
},
{
"firstName": "first2",
"lastName": "last2"
},
{
"firstName": "first3",
"lastName": "last3",
"question1": "test3"
}
]
});
});

0 comments on commit e4cd0df

Please sign in to comment.