Skip to content

Commit

Permalink
Add this.panel into expression function context in panel dynamic ques…
Browse files Browse the repository at this point in the history
…tion fix #6806 (#6807)
  • Loading branch information
andrewtelnov committed Aug 26, 2023
1 parent 8a700ce commit 766386f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 12 deletions.
8 changes: 4 additions & 4 deletions src/question_matrixdropdownbase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,14 +358,14 @@ implements ISurveyData, ISurveyImpl, ILocalizableOwner {
}
values[MatrixDropdownRowModelBase.IndexVariableName] = this.rowIndex;
values[MatrixDropdownRowModelBase.RowValueVariableName] = this.rowName;
if (!properties) properties = {};
properties[MatrixDropdownRowModelBase.RowVariableName] = this;
const newProps = Helpers.createCopy(properties);
newProps[MatrixDropdownRowModelBase.RowVariableName] = this;
for (var i = 0; i < this.cells.length; i++) {
values[MatrixDropdownRowModelBase.RowVariableName] = this.value;
this.cells[i].runCondition(values, properties);
this.cells[i].runCondition(values, newProps);
}
if (!!this.detailPanel) {
this.detailPanel.runCondition(values, properties);
this.detailPanel.runCondition(values, newProps);
}
}
public clearValue() {
Expand Down
14 changes: 8 additions & 6 deletions src/question_paneldynamic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1518,14 +1518,16 @@ export class QuestionPanelDynamicModel extends Question
cachedValues[QuestionPanelDynamicItem.ParentItemVariableName] = (<any>this.parent).getValue();
}
for (var i = 0; i < this.panels.length; i++) {
var panelValues = this.getPanelItemData(this.panels[i].data);
const panel = this.panels[i];
var panelValues = this.getPanelItemData(panel.data);
//Should be unique for every panel due async expression support
var newValues = Helpers.createCopy(cachedValues);
newValues[
QuestionPanelDynamicItem.ItemVariableName.toLowerCase()
] = panelValues;
const newValues = Helpers.createCopy(cachedValues);
const panelName = QuestionPanelDynamicItem.ItemVariableName;
newValues[panelName] = panelValues;
newValues[QuestionPanelDynamicItem.IndexVariableName.toLowerCase()] = i;
this.panels[i].runCondition(newValues, properties);
const newProps = Helpers.createCopy(properties);
newProps[panelName] = panel;
panel.runCondition(newValues, newProps);
}
}
onAnyValueChanged(name: string) {
Expand Down
2 changes: 1 addition & 1 deletion tests/entries/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export * from "../surveyLocalizationTests"; //
export * from "../surveyquestiontests"; //
export * from "../question_matrixdynamictests";
export * from "../question_matrixdropdownbasetests";
export * from "../surveypaneldynamictests";
export * from "../question_paneldynamic_tests";
export * from "../surveyserializationtests"; //
export * from "../surveytests"; //
export * from "../surveyWindowTests"; //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6037,4 +6037,35 @@ QUnit.test("paneldynamic.removePanelUI & confirmActionAsync, #6736", function(as
assert.deepEqual(panel.value, [{ q1: 1 }, { q1: 3 }], "Row is deleted correctly");

settings.confirmActionAsync = prevAsync;
});
});
QUnit.test("panel property in custom function", function (assert) {
const panelCustomFunc = function (params: any) {
if(!this.panel) return "";
const q = this.panel.getQuestionByName(params[0]);
if(q && !q.isEmpty()) return q.value + q.value;
return "";
};
FunctionFactory.Instance.register("panelCustomFunc", panelCustomFunc);
const survey = new SurveyModel({
elements: [
{
type: "paneldynamic",
name: "panel1",
templateElements: [
{ name: "q1", type: "text" },
{
name: "q2",
type: "expression",
expression: "panelCustomFunc('q1')",
},
],
panelCount: 2,
},
],
});
const question = <QuestionPanelDynamicModel>survey.getQuestionByName("panel1");
const panel = question.panels[0];
panel.getQuestionByName("q1").value = "abc";
assert.equal(panel.getQuestionByName("q2").value, "abcabc", "Custom function with row property works correctly");
FunctionFactory.Instance.unregister("panelCustomFunc");
});

0 comments on commit 766386f

Please sign in to comment.