Skip to content

Commit 48c9a22

Browse files
committed
Fix the bug: The order is incorrect on setting value initial in SortableJS, #77
1 parent 621a028 commit 48c9a22

File tree

1 file changed

+44
-15
lines changed

1 file changed

+44
-15
lines changed

src/sortablejs.js

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -70,28 +70,57 @@ function init(Survey) {
7070
}
7171
return false;
7272
};
73+
var addChoiceToWidget = function(choice, inResults) {
74+
var srcEl = inResults ? resultEl : sourceEl;
75+
var newEl = document.createElement("div");
76+
newEl.innerHTML =
77+
"<div class='sjs-sortablejs-item' style='" +
78+
self.itemStyle +
79+
"'>" +
80+
choice.text +
81+
"</div>";
82+
newEl.dataset["value"] = choice.value;
83+
srcEl.appendChild(newEl);
84+
};
85+
var getChoicesNotInResults = function() {
86+
var res = [];
87+
question.activeChoices.forEach(function(choice) {
88+
if (!hasValueInResults(choice.value)) {
89+
res.push(choice);
90+
}
91+
});
92+
return res;
93+
};
94+
var getChoicesInResults = function() {
95+
var res = [];
96+
var val = question.value;
97+
if (!Array.isArray(val)) return res;
98+
for (var i = 0; i < val.length; i++) {
99+
var item = Survey.ItemValue.getItemByValue(
100+
question.activeChoices,
101+
val[i]
102+
);
103+
if (!!item) {
104+
res.push(item);
105+
}
106+
}
107+
return res;
108+
};
73109
var isUpdatingQuestionValue = false;
74110
var updateValueHandler = function() {
75111
if (isUpdatingQuestionValue) return;
76112
resultEl.innerHTML = "";
77113
resultEl.appendChild(emptyEl);
78114
sourceEl.innerHTML = "";
79-
var wasInResults = false;
80-
question.activeChoices.forEach(function(choice) {
81-
var inResutls = hasValueInResults(choice.value);
82-
wasInResults = wasInResults || inResutls;
83-
var srcEl = inResutls ? resultEl : sourceEl;
84-
var newEl = document.createElement("div");
85-
newEl.innerHTML =
86-
"<div class='sjs-sortablejs-item' style='" +
87-
self.itemStyle +
88-
"'>" +
89-
choice.text +
90-
"</div>";
91-
newEl.dataset["value"] = choice.value;
92-
srcEl.appendChild(newEl);
115+
var notInResults = getChoicesNotInResults();
116+
var inResults = getChoicesInResults();
117+
emptyEl.style.display = inResults.length > 0 ? "none" : "";
118+
inResults.forEach(function(choice) {
119+
addChoiceToWidget(choice, true);
120+
});
121+
notInResults.forEach(function(choice) {
122+
addChoiceToWidget(choice, false);
93123
});
94-
emptyEl.style.display = wasInResults ? "none" : "";
95124
};
96125
result = question.resultEl = Sortable.create(resultEl, {
97126
animation: 150,

0 commit comments

Comments
 (0)