admin管理员组文章数量:1357377
I'm using Select2 to fill Dropdowns. Select2 is slow when opening its dropdown menu if the underlying select has a fair number of items. I have more than 10.000+ elements in my dropdown.
Here is my code:
$.ajax({
url: "/Companies/GetCompanies",
method: "get",
success: function (data) {
if (data != null) {
var newWorkPlaceId = $("#newWorkPlaceId");
newWorkPlaceId.empty();
newWorkPlaceId.append("<option value=''> -- Choose-- </option>");
$.each(data, function (index, item) {
newWorkPlaceId.append(
$('<option>', {
value: item.id,
text: item.text
}, '</option>'));
});
$("#newWorkPlaceId").select2({
placeholder: {
id: "",
placeholder: " -- Choose--"
},
allowClear: true
});
}
clearconsole();
}
});
Is there a way to make the Select2 widget (or another searchable dropdown) respond faster?
Note: I am using ASP.NET CORE
I'm using Select2 to fill Dropdowns. Select2 is slow when opening its dropdown menu if the underlying select has a fair number of items. I have more than 10.000+ elements in my dropdown.
Here is my code:
$.ajax({
url: "/Companies/GetCompanies",
method: "get",
success: function (data) {
if (data != null) {
var newWorkPlaceId = $("#newWorkPlaceId");
newWorkPlaceId.empty();
newWorkPlaceId.append("<option value=''> -- Choose-- </option>");
$.each(data, function (index, item) {
newWorkPlaceId.append(
$('<option>', {
value: item.id,
text: item.text
}, '</option>'));
});
$("#newWorkPlaceId").select2({
placeholder: {
id: "",
placeholder: " -- Choose--"
},
allowClear: true
});
}
clearconsole();
}
});
Is there a way to make the Select2 widget (or another searchable dropdown) respond faster?
Note: I am using ASP.NET CORE
Share Improve this question edited Mar 5, 2019 at 13:05 Fatikhan Gasimov asked Feb 22, 2019 at 6:52 Fatikhan GasimovFatikhan Gasimov 9532 gold badges16 silver badges41 bronze badges 1- 1 It's never a good idea to have 10k+ options in a select element: that's going to be bad UX experience, because of (1) the number of choices the user has to scroll through and (2) the 10k+ DOM nodes rendering slowing down the browser. What you should consider is a typeahead solution, i.e. suggest options as the user types. In that way, you are always only displaying a limited subset of possible options. – Terry Commented Feb 22, 2019 at 6:59
2 Answers
Reset to default 3You have to use AJAX data and pagination like below
$('#mySelect2').select2({
ajax: {
url: 'https://api.github./search/repositories',
data: function (params) {
var query = {
search: params.term,
page: params.page || 1
}
// Query parameters will be ?search=[term]&page=[page]
return query;
}
}
});
Example
$('#mySelect2').select2({
ajax: {
url: function (params) {
return 'https://api.github./search/repositories?q='+params.term+'&page='+params.page || 1;
},
processResults: function (data) {
return {
results: $.map(data.items, function (item) {
return {
text: item.name,
id: item.id
}
})
};
}}
});
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare./ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare./ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>
<select style="width:50%" id="mySelect2"></select>
After long research I found that 2 symbols limitation will help me to fix this. Here is my solution
$("#newWorkPlaceId").select2({
allowClear: true,
placeholder: {
id: "",
placeholder: "Choose."
},
language: {
inputTooShort: function (args) {
return "2 or more symbol.";
},
noResults: function () {
return "Not Found.";
},
searching: function () {
return "Searching...";
}
},
minimumInputLength: 2,
ajax: {
url: 'url',
dataType: 'json',
type: "GET",
quietMillis: 50,
data: function(term) {
return {
term: term.term
};
},
processResults: function (response) {
return {
results: response
};
}
}
});
本文标签: javascriptSelect2 is slow on opening with 10000 elementsStack Overflow
版权声明:本文标题:javascript - Select2 is slow on opening with 10.000+ elements - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744079807a2587419.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论