Skip to content

Commit

Permalink
Dynamic Ranking Question: Choices are not rendered properly based on …
Browse files Browse the repository at this point in the history
…previous selection fixed #5959
  • Loading branch information
andrewtelnov committed Apr 11, 2023
1 parent 5caa9b3 commit fac13aa
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
15 changes: 6 additions & 9 deletions src/question_ranking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,12 @@ export class QuestionRankingModel extends QuestionCheckboxModel {

private removeFromValueByVisibleChoices() {
const newValue = this.value.slice();

this.value.forEach((valueItem: string, index: number) => {
let isValueItemToRemove = true;
this.visibleChoices.forEach((choice) => {
if (choice.value === valueItem) isValueItemToRemove = false;
});
isValueItemToRemove && newValue.splice(index, 1);
});

const choices = this.visibleChoices;
for(let i = this.value.length - 1; i >= 0; i --) {
if(!ItemValue.getItemByValue(choices, this.value[i])) {
newValue.splice(i, 1);
}
}
this.value = newValue;
}

Expand Down
26 changes: 25 additions & 1 deletion tests/question_ranking_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,4 +289,28 @@ QUnit.test("Ranking: separateSpecialChoices ", function (assert) {
const prop = "separateSpecialChoices";
assert.ok(Serializer.findProperty("checkbox", prop).visible, "checkbox separateSpecialChoices is visible");
assert.notOk(Serializer.findProperty("ranking", prop).visible, "ranking separateSpecialChoices is invisible");
});
});
QUnit.test("Ranking: items visibleIf and value, Bug#5959", function(assert) {
var survey = new SurveyModel({
elements: [
{ type: "checkbox", name: "q1", choices: [1, 2] },
{
type: "ranking",
name: "q2",
choices: [
{ value: "a", visibleIf: "{q1} contains 1" },
{ value: "b", visibleIf: "{q1} contains 1" },
{ value: "c", visibleIf: "{q1} contains 2" },
{ value: "d", visibleIf: "{q1} contains 2" },
]
}
]
});
var q1 = survey.getQuestionByName("q1");
q1.value = [1, 2];
var q2 = <QuestionRankingModel>survey.getQuestionByName("q2");
q2.value = ["c", "d", "a", "b"];
q1.value = [1];
assert.deepEqual(q2.value, ["a", "b"], "value is correct");
assert.equal(q2.rankingChoices.length, 2, "2 items are shown");
});

0 comments on commit fac13aa

Please sign in to comment.