Javascript 对于大数据,多选下拉搜索速度非常慢

Javascript 对于大数据,多选下拉搜索速度非常慢,javascript,jquery,html,Javascript,Jquery,Html,您好,我正在使用multiselect下拉列表,使用select2 jquery 4.0.3 我使用Viewbag获取数据,并在Viewbag中加载大约9000个数据,下面是下拉列表 @Html.DropDownListFor(m => m.Tags, ViewBag.tags1 as IEnumerable<SelectListItem> , "----Select tags----", new { @class = "Tags form-control", multiple

您好,我正在使用multiselect下拉列表,使用select2 jquery 4.0.3 我使用Viewbag获取数据,并在Viewbag中加载大约9000个数据,下面是下拉列表

@Html.DropDownListFor(m => m.Tags, ViewBag.tags1 as IEnumerable<SelectListItem> , "----Select tags----", new { @class = "Tags form-control", multiple = "multiple", @id = "Tags" })

<script>
    $(document).ready(function () {
        $("#Tags").select2({
            placeholder: "Select Tags",
            minimumInputLength: 3,
            tags: true
        })
     });
</script>
@Html.DropDownListFor(m=>m.Tags,ViewBag.tags1为IEnumerable,“----选择Tags----”,新建{@class=“Tags表单控件”,multiple=“multiple”,@id=“Tags”})
$(文档).ready(函数(){
$(“#标记”)。选择2({
占位符:“选择标签”,
最小输入长度:3,
标签:真的
})
});
ViewBag.tags1
包含我的数据,现在我的页面加载非常完美,但在搜索时(在下拉搜索框中键入所需数据),下拉反应非常慢

感觉系统被挂起了,搜索框中的任何操作都非常缓慢

有什么解决办法吗?
需要帮助。

加载9000项并将其插入DOM是个坏主意

请参阅下面的代码,它将很容易实现。这将允许您按页面加载数据

您需要创建一个返回JSON的端点

$(".js-data-example-ajax").select2({
  ajax: {
    url: "https://api.github.com/search/repositories",
    dataType: 'json',
    delay: 250,
    data: function (params) {
      return {
        q: params.term, // search term
        page: params.page
      };
    },
    processResults: function (data, params) {
      // parse the results into the format expected by Select2
      // since we are using custom formatting functions we do not need to
      // alter the remote JSON data, except to indicate that infinite
      // scrolling can be used
      params.page = params.page || 1;

      return {
        results: data.items,
        pagination: {
          more: (params.page * 30) < data.total_count
        }
      };
    },
    cache: true
  },
  escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
  minimumInputLength: 1,
  templateResult: formatRepo, // omitted for brevity, see the source of this page
  templateSelection: formatRepoSelection // omitted for brevity, see the source of this page
});
$(“.js数据示例ajax”)。选择2({
阿贾克斯:{
url:“https://api.github.com/search/repositories",
数据类型:“json”,
延误:250,
数据:函数(参数){
返回{
q:params.term,//搜索项
页码:params.page
};
},
processResults:函数(数据、参数){
//将结果解析为Select2所需的格式
//因为我们使用的是自定义格式函数,所以不需要
//更改远程JSON数据,除非指示无限
//可以使用滚动
params.page=params.page | | 1;
返回{
结果:数据项,
分页:{
更多:(params.page*30)
加载9000个项目并将其插入DOM是个坏主意

请参阅下面的代码,它将很容易实现。这将允许您按页面加载数据

您需要创建一个返回JSON的端点

$(".js-data-example-ajax").select2({
  ajax: {
    url: "https://api.github.com/search/repositories",
    dataType: 'json',
    delay: 250,
    data: function (params) {
      return {
        q: params.term, // search term
        page: params.page
      };
    },
    processResults: function (data, params) {
      // parse the results into the format expected by Select2
      // since we are using custom formatting functions we do not need to
      // alter the remote JSON data, except to indicate that infinite
      // scrolling can be used
      params.page = params.page || 1;

      return {
        results: data.items,
        pagination: {
          more: (params.page * 30) < data.total_count
        }
      };
    },
    cache: true
  },
  escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
  minimumInputLength: 1,
  templateResult: formatRepo, // omitted for brevity, see the source of this page
  templateSelection: formatRepoSelection // omitted for brevity, see the source of this page
});
$(“.js数据示例ajax”)。选择2({
阿贾克斯:{
url:“https://api.github.com/search/repositories",
数据类型:“json”,
延误:250,
数据:函数(参数){
返回{
q:params.term,//搜索项
页码:params.page
};
},
processResults:函数(数据、参数){
//将结果解析为Select2所需的格式
//因为我们使用的是自定义格式函数,所以不需要
//更改远程JSON数据,除非指示无限
//可以使用滚动
params.page=params.page | | 1;
返回{
结果:数据项,
分页:{
更多:(params.page*30)
我在下面的链接中找到了我的答案


datahourting.com/mvc/select2-remote-data-example-in-asp-net-‌​mvc

我在下面的链接中找到了我的答案


datahourting.com/mvc/select2-remote-data-example-in-asp-net-‌​mvc

仅加载前50个选项,当用户开始键入时,然后从db获取。您好,感谢您的重播,我能够以数组格式返回数据,但它不起作用,通过ajax获取数据并返回json…任何示例,请仅共享前50个选项,当用户开始键入时,从db获取数据。您好,感谢您的重播,我能够以数组格式返回数据,但它不起作用,通过ajax获取数据并返回json…任何示例,请共享。您好,感谢您的重播,我使用ajax并使用json返回数据。但它不起作用,我无法使用我获取的数据填充下拉列表items@KalpeshKoli,创建一个提琴,以便人们可以看到您的代码,或者在您的帖子中显示示例,以便我们可以看到问题。如果您发现了一些错误,请将其包括在内,以便我们可以帮助您。上面的示例已经填充了来自服务器的数据。我在这里找到了我的答案“谢谢大家,谢谢你们的重播,我使用了ajax并使用json返回数据。但是它不起作用,我无法使用我正在获取的数据填充下拉列表items@KalpeshKoli,创建一个提琴,以便人们可以看到您的代码,或者在您的帖子中显示示例,以便我们可以看到问题。如果您发现了一些错误,请将其包括在内,以便我们可以帮助您。上面的示例已经填充了来自服务器的数据。我在这里找到了我的答案“谢谢你们,你们应该提供更多信息,而不仅仅是一个链接。”。将来很容易断开连接。请看,您应该提供更多信息,而不仅仅是一个链接。将来很容易断开连接。看见