select2 +大量记录

时间:2018-12-10 09:11:00

标签: jquery-select2 large-data largenumber

我正在使用select2下拉列表。对于少量项目,它工作正常。 但是,当列表很大(超过40000个项目)时,它的确会放慢速度。在IE中最慢。

否则,简单的Dropdownlist可以非常快速地工作,直到1000条记录。有没有针对这种情况的解决方法?

1 个答案:

答案 0 :(得分: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);
    }