Skip to content

Commit

Permalink
Implement choicesFromValueName&choicesFromTextName #6061
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewtelnov committed Aug 9, 2023
1 parent 2b7f029 commit ca92209
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 15 deletions.
47 changes: 40 additions & 7 deletions src/question_baseselect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class QuestionSelectBase extends Question {
}
});
this.registerPropertyChangedHandlers(
["choicesFromQuestion", "choicesFromQuestionMode", "showNoneItem"],
["choicesFromQuestion", "choicesFromQuestionMode", "choicesFromValueName", "choicesFromTextName", "showNoneItem"],
() => {
this.onVisibleChoicesChanged();
}
Expand Down Expand Up @@ -93,7 +93,7 @@ export class QuestionSelectBase extends Question {
public getType(): string {
return "selectbase";
}
public dispose() {
public dispose(): void {
super.dispose();
for (var i = 0; i < this.dependedQuestions.length; i++) {
this.dependedQuestions[i].choicesFromQuestion = "";
Expand All @@ -118,7 +118,7 @@ export class QuestionSelectBase extends Question {
public get isUsingCarryForward(): boolean {
return !!this.carryForwardQuestionType;
}
private get carryForwardQuestionType(): string {
public get carryForwardQuestionType(): string {
return this.getPropertyValue("carryForwardQuestionType");
}
private setCarryForwardQuestionType(selBaseQuestion: boolean, arrayQuestion: boolean): void {
Expand Down Expand Up @@ -729,6 +729,18 @@ export class QuestionSelectBase extends Question {
public set choicesFromQuestionMode(val: string) {
this.setPropertyValue("choicesFromQuestionMode", val);
}
public get choicesFromValueName(): string {
return this.getPropertyValue("choicesFromValueName");
}
public set choicesFromValueName(val: string) {
this.setPropertyValue("choicesFromValueName", val);
}
public get choicesFromTextName(): string {
return this.getPropertyValue("choicesFromTextName");
}
public set choicesFromTextName(val: string) {
this.setPropertyValue("choicesFromTextName", val);
}
/**
* Specifies whether to hide the question if no choice items are visible.
*
Expand Down Expand Up @@ -1050,12 +1062,19 @@ export class QuestionSelectBase extends Question {
for(var i = 0; i < val.length; i ++) {
const obj = val[i];
if(!Helpers.isValueObject(obj)) continue;
const keys = Object.keys(obj);
if(keys.length === 0) continue;
res.push(this.createItemValue(obj[keys[0]]));
const key = this.getValueKeyName(obj);
if(!!key && !this.isValueEmpty(obj[key])) {
const text = !!this.choicesFromTextName ? obj[this.choicesFromTextName] : undefined;
res.push(this.createItemValue(obj[key], text));
}
}
return res;
}
private getValueKeyName(obj: any): string {
if(this.choicesFromValueName) return this.choicesFromValueName;
const keys = Object.keys(obj);
return keys.length > 0 ? keys[0] : undefined;
}
private getChoicesFromSelectQuestion(question: QuestionSelectBase): Array<ItemValue> {
if (this.isDesignMode) return [];
const res: Array<ItemValue> = [];
Expand Down Expand Up @@ -1786,7 +1805,21 @@ Serializer.addClass(
choices: ["all", "selected", "unselected"],
dependsOn: "choicesFromQuestion",
visibleIf: (obj: any) => {
return !!obj.choicesFromQuestion;
return obj.carryForwardQuestionType === "select";
},
},
{
name: "choicesFromValueName",
dependsOn: "choicesFromQuestion",
visibleIf: (obj: any) => {
return obj.carryForwardQuestionType === "array";
},
},
{
name: "choicesFromTextName",
dependsOn: "choicesFromQuestion",
visibleIf: (obj: any) => {
return obj.carryForwardQuestionType === "array";
},
},
{
Expand Down
56 changes: 48 additions & 8 deletions tests/question_baseselecttests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1091,30 +1091,70 @@ QUnit.test("Use carryForward with matrix dynamic", function (assert) {
assert.equal(q2.visibleChoices.length, 3, "There are three choice");
assert.equal(q2.visibleChoices[2].value, "C", "the third value is correct");
});
/*

QUnit.test("Use carryForward with matrix dynamic + choicesFromValueName", function (assert) {
const survey = new SurveyModel({ elements: [
{ type: "matrixdynamic", name: "q1", columns: [{ name: "col1", cellType: "text" }, { name: "col2", cellType: "text" }] },
{ type: "checkbox", name: "q2", choicesFromQuestion: "q1", choicesFromValueName: "" }
{ type: "checkbox", name: "q2", choicesFromQuestion: "q1", choicesFromValueName: "col2" }
] });
const q1 = <QuestionMatrixDynamicModel>survey.getQuestionByName("q1");
const q2 = <QuestionSelectBase>survey.getQuestionByName("q2");
assert.equal(q2.choicesFromQuestion, "q1", "choicesFromQuestion is set");
assert.equal(q2.choicesFromValueName, "col2", "choicesFromValueName is set");
assert.equal(q2.isUsingCarryForward, true, "Carryforward flag is set");
assert.equal(q2.visibleChoices.length, 0, "There is no choices");
assert.equal(q2.visibleChoices.length, 0, "There is no choices, row is empty");
q1.visibleRows[0].cells[0].value = "A";
assert.deepEqual(survey.data, { q1: [{ col1: "A" }, {}] }, "survey.data is correct");
assert.equal(q2.visibleChoices.length, 0, "There is no choices, col2 is empty");
q1.visibleRows[0].cells[1].value = "AA";
assert.equal(q2.visibleChoices.length, 1, "There is one choice");
assert.equal(q2.visibleChoices[0].value, "A", "the first value is correct");
assert.equal(q2.visibleChoices[0].value, "AA", "the first value is correct");
q1.visibleRows[1].cells[0].value = "B";
q1.visibleRows[1].cells[1].value = "BB";
assert.equal(q2.visibleChoices.length, 2, "There are two choice");
assert.equal(q2.visibleChoices[1].value, "B", "the second value is correct");
assert.equal(q2.visibleChoices[1].value, "BB", "the second value is correct");
q1.addRow();
assert.equal(q2.visibleChoices.length, 2, "There are two choice, new row is empty");
q1.visibleRows[2].cells[0].value = "C";
assert.deepEqual(survey.data, { q1: [{ col1: "A" }, { col1: "B" }, { col1: "C" }] }, "survey.data is correct, #2");
assert.equal(q2.visibleChoices.length, 2, "There are two choice, col2 is empty");
q1.visibleRows[2].cells[1].value = "CC";
assert.equal(q2.visibleChoices.length, 3, "There are three choice");
assert.equal(q2.visibleChoices[2].value, "C", "the third value is correct");
assert.equal(q2.visibleChoices[2].value, "CC", "the third value is correct");
});
QUnit.test("Use carryForward with panel dynamic + choicesFromValueName&choicesFromTextName", function (assert) {
const survey = new SurveyModel({ elements: [
{ type: "paneldynamic", name: "q1", panelCount: 2,
templateElements: [{ name: "q1-q1", type: "text" }, { name: "q1-q2", type: "text" }, { name: "q1-q3", type: "text" }]
},
{ type: "checkbox", name: "q2", choicesFromQuestion: "q1", choicesFromValueName: "q1-q2", choicesFromTextName: "q1-q3" }
] });
const q1 = <QuestionPanelDynamicModel>survey.getQuestionByName("q1");
const q2 = <QuestionSelectBase>survey.getQuestionByName("q2");
assert.equal(q2.choicesFromQuestion, "q1", "choicesFromQuestion is set");
assert.equal(q2.choicesFromValueName, "q1-q2", "choicesFromValueName is set");
assert.equal(q2.choicesFromTextName, "q1-q3", "choicesFromTextName is set");
assert.equal(q2.isUsingCarryForward, true, "Carryforward flag is set");
assert.equal(q2.visibleChoices.length, 0, "There is no choices");
q1.panels[0].getQuestionByName("q1-q1").value = "A";
assert.equal(q2.visibleChoices.length, 0, "There is no choices, q1-q2 is empty");
q1.panels[0].getQuestionByName("q1-q2").value = "AA";
q1.panels[0].getQuestionByName("q1-q3").value = "AA-aa";
assert.equal(q2.visibleChoices.length, 1, "There is one choice");
assert.equal(q2.visibleChoices[0].value, "AA", "the first value is correct");
assert.equal(q2.visibleChoices[0].text, "AA-aa", "the first text is correct");
q1.panels[1].getQuestionByName("q1-q1").value = "B";
q1.panels[1].getQuestionByName("q1-q2").value = "BB";
q1.panels[1].getQuestionByName("q1-q3").value = "BB-bb";
assert.equal(q2.visibleChoices.length, 2, "There are two choice");
assert.equal(q2.visibleChoices[1].value, "BB", "the second value is correct");
assert.equal(q2.visibleChoices[1].text, "BB-bb", "the second text is correct");
q1.addPanel();
assert.equal(q2.visibleChoices.length, 2, "There are two choice, new panel is empty");
q1.panels[2].getQuestionByName("q1-q1").value = "C";
assert.equal(q2.visibleChoices.length, 2, "There are two choice, q1-q2 is empty");
q1.panels[2].getQuestionByName("q1-q2").value = "CC";
q1.panels[2].getQuestionByName("q1-q3").value = "CC-cc";
assert.equal(q2.visibleChoices.length, 3, "There are three choice");
assert.equal(q2.visibleChoices[2].value, "CC", "the third value is correct");
assert.equal(q2.visibleChoices[2].text, "CC-cc", "the third text is correct");
});
*/

0 comments on commit ca92209

Please sign in to comment.