Skip to content

Commit dfc1f32

Browse files
committed
Fixes rstudio#929: defer setValue() for selectize until the options have been loaded from server
Previously I was using a hack to set the selected value of selectize, i.e. add the selected option(s) to selectize via addOptions() (because the selected option(s) may not have existed); this hack can be removed and we can set the value in the `success` callback of the Ajax request, by which time the options will be available
1 parent e6fd30f commit dfc1f32

File tree

2 files changed

+5
-9
lines changed

2 files changed

+5
-9
lines changed

R/update-input.R

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -474,13 +474,9 @@ updateSelectizeInput <- function(session, inputId, label = NULL, choices = NULL,
474474
return(updateSelectInput(session, inputId, label, choices, selected))
475475
}
476476
value <- unname(selected)
477-
selected <- choicesWithNames(selected)
478477
message <- dropNulls(list(
479478
label = label,
480479
value = value,
481-
selected = if (length(selected)) {
482-
columnToRowData(list(label = names(selected), value = selected))
483-
},
484480
url = session$registerDataObj(inputId, choices, selectizeJSON)
485481
))
486482
session$sendInputMessage(inputId, message)

srcjs/input_binding_select.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ $.extend(selectInputBinding, {
5555
if (data.hasOwnProperty('url')) {
5656
selectize = this._selectize(el);
5757
selectize.clearOptions();
58+
var thiz = this;
5859
selectize.settings.load = function(query, callback) {
5960
var settings = selectize.settings;
6061
$.ajax({
@@ -71,19 +72,18 @@ $.extend(selectInputBinding, {
7172
},
7273
success: function(res) {
7374
callback(res);
75+
if (data.hasOwnProperty('value'))
76+
thiz.setValue(el, data.value);
7477
}
7578
});
7679
};
7780
// perform an empty search after changing the `load` function
7881
selectize.load(function(callback) {
7982
selectize.settings.load.apply(selectize, ['', callback]);
8083
});
81-
if (data.hasOwnProperty('selected'))
82-
selectize.addOption(data.selected);
83-
}
84-
85-
if (data.hasOwnProperty('value'))
84+
} else if (data.hasOwnProperty('value')) {
8685
this.setValue(el, data.value);
86+
}
8787

8888
if (data.hasOwnProperty('label'))
8989
$(el).parent().parent().find('label[for="' + $escape(el.id) + '"]').text(data.label);

0 commit comments

Comments
 (0)