Skip to content

Commit

Permalink
Reduce survey value changed on adding new panel in dynamic panel with…
Browse files Browse the repository at this point in the history
… complex calculations fix #7005 (#7006)
  • Loading branch information
andrewtelnov authored Sep 25, 2023
1 parent 086b1df commit 87b630b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
14 changes: 7 additions & 7 deletions src/question_paneldynamic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1499,7 +1499,7 @@ export class QuestionPanelDynamicModel extends Question
}
public runCondition(values: HashTable<any>, properties: HashTable<any>) {
super.runCondition(values, properties);
this.runPanelsCondition(values, properties);
this.runPanelsCondition(this.panels, values, properties);
}
private reRunCondition() {
if (!this.data) return;
Expand All @@ -1508,10 +1508,7 @@ export class QuestionPanelDynamicModel extends Question
this.getDataFilteredProperties()
);
}
protected runPanelsCondition(
values: HashTable<any>,
properties: HashTable<any>
) {
protected runPanelsCondition(panels: PanelModel[], values: HashTable<any>, properties: HashTable<any>): void {
var cachedValues: { [index: string]: any } = {};
if (values && values instanceof Object) {
cachedValues = JSON.parse(JSON.stringify(values));
Expand All @@ -1520,8 +1517,8 @@ export class QuestionPanelDynamicModel extends Question
cachedValues[QuestionPanelDynamicItem.ParentItemVariableName] = (<any>this.parent).getValue();
}
this.isValueChangingInternally = true;
for (var i = 0; i < this.panels.length; i++) {
const panel = this.panels[i];
for (var i = 0; i < panels.length; i++) {
const panel = panels[i];
var panelValues = this.getPanelItemData(panel.data);
//Should be unique for every panel due async expression support
const newValues = Helpers.createCopy(cachedValues);
Expand Down Expand Up @@ -1760,6 +1757,9 @@ export class QuestionPanelDynamicModel extends Question
panel.renderWidth = "100%";
panel.updateCustomWidgets();
new QuestionPanelDynamicItem(this, panel);
if(!this.isDesignMode && !this.isReadOnly && !this.isValueEmpty(panel.getValue())) {
this.runPanelsCondition([panel], this.getDataFilteredValues(), this.getDataFilteredProperties());
}
panel.onFirstRendering();
var questions = panel.questions;
for (var i = 0; i < questions.length; i++) {
Expand Down
5 changes: 3 additions & 2 deletions tests/question_paneldynamic_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4374,6 +4374,7 @@ QUnit.test("defaultValue & survey.onValueChanged on adding new panel", function
name: "panel",
type: "paneldynamic",
templateElements: [
{ type: "expression", name: "q5", expression: "{panel.q4} + {panel.q3}" },
{ type: "text", name: "q1", defaultValue: 1 },
{ type: "text", name: "q2", defaultValue: 2 },
{ type: "text", name: "q3", defaultValueExpression: "{panel.q1} + {panel.q2}" },
Expand All @@ -4389,10 +4390,10 @@ QUnit.test("defaultValue & survey.onValueChanged on adding new panel", function
});
const panel = <QuestionPanelDynamicModel>survey.getQuestionByName("panel");
panel.addPanel();
assert.deepEqual(panel.value, [{ q1: 1, q2: 2, q3: 3, q4: 4 }], "panel.value #1");
assert.deepEqual(panel.value, [{ q1: 1, q2: 2, q3: 3, q4: 4, q5: 7 }], "panel.value #1");
assert.equal(counter, 1, "survey.onValueChanged call times #1");
panel.addPanel();
assert.deepEqual(panel.value, [{ q1: 1, q2: 2, q3: 3, q4: 4 }, { q1: 1, q2: 2, q3: 3, q4: 4 }], "panel.value #2");
assert.deepEqual(panel.value, [{ q1: 1, q2: 2, q3: 3, q4: 4, q5: 7 }, { q1: 1, q2: 2, q3: 3, q4: 4, q5: 7 }], "panel.value #2");
assert.equal(counter, 2, "survey.onValueChanged call times #2");
});
QUnit.test(
Expand Down

0 comments on commit 87b630b

Please sign in to comment.