【问题标题】:select2 + large number of recordsselect2 + 大量记录
【发布时间】:2019-05-11 03:56:35
【问题描述】:

我正在使用 select2 下拉菜单。它适用于较少数量的项目。 但是当列表很大(超过 40000 项)时,它真的会变慢。它在 IE 中最慢。

否则简单的下拉列表工作得非常快,直到 1000 条记录。这种情况有什么解决办法吗?

【问题讨论】:

  • 这可能会在 select2 错误跟踪器上得到更好的接收?
  • 好的,谢谢,我用下面的代码解决了问题
  • 感谢您发布答案。这对未来的用户可能很有价值。
  • 是的,多门,谢谢

标签: jquery-select2 large-data largenumber


【解决方案1】:
///////////////**** Jquery Code *******///////////////
var CompanypageSize = 10;

function initCompanies() {
        var defaultTxtOnInit = 'a';
        $("#DefaultCompanyId").select2({
            allowClear: true,
            ajax: {
                url: "/SignUpTemplate/GetCompanies",
                dataType: 'json',
                delay: 250,
                global: false,
                data: function (params) {
                    params.page = params.page || 1;
                    return {
                        keyword: params.term ? params.term : defaultTxtOnInit,
                        pageSize: CompanypageSize,
                        page: params.page
                    };
                },
                processResults: function (data, params) {
                    params.page = params.page || 1;
                    return {
                        results: data.result,
                        pagination: {
                            more: (params.page * CompanypageSize) < data.Counts
                        }
                    };
                },
                cache: true
            },
            placeholder: {
                id: '0', // the value of the option
                text: '--Select Company--'
            },
            width: '100%',
            //minimumInputLength: 3,
        });
    }


//////////******* Have to initialise in .ready *******///////////////

 $(document).ready(function () {

        initCompanies();
    });

//////////******* C# code :: Controller is : SignUpTemplateController************/////

public JsonResult GetCompanies(string keyword, int? pageSize, int? page)
    {
        int totalCount = 0;
        if (!string.IsNullOrWhiteSpace(keyword))
        {
            List<Companies> listCompanies = Companies.GetAll(this.CurrentTenant, (keyword ?? string.Empty).Trim(), false, 11, page.Value, pageSize.Value, ref totalCount, null, null, null, null, null).ToList();
            var list = listCompanies.Select(x => new { text = x.CompanyName, id = x.CompanyId }).ToList();

            return Json(new { result = list, Counts = totalCount }, JsonRequestBehavior.AllowGet);
        }

        return Json(null, JsonRequestBehavior.AllowGet);
    }

【讨论】:

  • 我自己找到了答案。只需将 Select2 与 Ajax 一起使用。就像 Select2 + Pagging 一样。我所做的是,C#:使用 pagenumber 和 pageValue 获取 PagedList,它返回文本,id Jquery:使用选择 2 分页事件 HTML:使用简单选择 2 html
猜你喜欢
  • 2013-10-03
  • 2014-05-12
  • 1970-01-01
  • 1970-01-01
  • 2011-05-14
  • 2015-03-31
  • 2017-12-18
  • 2013-07-20
  • 1970-01-01
相关资源
最近更新 更多