select2的使用(ajax获取数据)

最近项目中用到了select2来作下拉框,数据都是经过ajax从后台获取, 支持动态搜索等。html

使用到的下拉框分有两种状况:ajax

一种是直接发送ajax请求渲染列表;另外一种由于查询回的数据有六万多条,致使整个页面卡顿,因此采用的是先让用户至少输入3个字之后再动态模糊查询数据。json

 

基本的使用方法看官方文档就能够明白,可是在作模糊查询的时候遇到了一些问题,在此记录一下。数据结构

第一种状况的下拉框,先是封装了函数获取数据,并拼接了列表模版,而后设置templateSelection便可。app

复制代码

function getProvinceList(ele) {
        var proList = '<option value="-1">--省--</option>'
        $.ajax({
            type:'get',
            contentType:'application/json;charset=utf-8',
            url: dicUrl + 'queryTwoLayerAddress',
            dataType:'json',
            success: function(res) {
                if(status == 00) {          
                    var resArr = res.data
                    for( var i = 0; i < resArr.length; i++) {
                        proList += '<option value = '+ resArr[i].code +'>'+ resArr[i].codeText +'</option>'     
                    }               
                    ele.html(proList)           
                }   
            }
        })
    }

$('#addrProvince').select2({函数

 

        language: localLang,post

 

        placeholder:'请选择省份',this

 

        templateSelection:  getProvinceList($('#addrProvince'))lua

 

 })url

 

复制代码

 

第二种作法则是按照文档里的作法,初始化select框后再发送ajax请求.

复制代码

$('#bankName').select2({
        minimumInputLength:3,
        id: function(data) {     //把[{id:1, text:"a"}] 转换成[{data.id:1, data.codeText:"a"}], 由于后台返回的数据是[{id:1, codeText:"a"}]
            return data.id
        },
       // text: function(data) {return data.codeText},   //不生效
        formatSelection: function (data) { return data.codeText },  //生效
        ajax: {
            type:'get',
            url: function(params){      
                return dicUrl + 'query/bankCode/'+ params.term
            },
            dataType:'json',    
            data: function(params) { //输入的内容
                return {
                    text:params.term,
                }
            },  
            processResults: function (data, page) {
                //data = { results:[{ItemId:1,ItemText:"a"},{ItemId:2,ItemText:"b"}] };
                //须要将查出来的数据转换成{id:***,text:***},不然点击下拉框的时候没法赋值
                    var array = data.data;
                    var i = 0;
                    while(i < array.length){
                            array[i]["id"] = array[i]['code'];
                            array[i]["text"] = array[i]['codeText'];
                            delete array[i]["ItemId"];
                            delete array[i]["ItemText"];
                    i++;
                    }
                    return { results: array };
                },  
            cache: true,        
        },
        placeholder:'请选择银行名称',
        escapeMarkup: function(markup) { //提示语
            return markup
        },  
        templateResult: formatRepo,
        templateSelection: formatRepoSelection  
    });

    function formatRepo (repo) {
        if (repo.loading) {
            return repo.text;
        }   
        var markup = "<div class='select2-result-repository clearfix'>" +
            "<div class='select2-result-repository__meta'>" +
                "<div class='select2-result-repository__title'>" + repo.codeText + "</div>";    
        if (repo.description) {
            markup += "<div class='select2-result-repository__description'>" + repo.description + "</div>";
        }
        return markup;
    }
    
    function formatRepoSelection (repo) {
        return repo.text;
    }

复制代码

 

select2.js 默认的ajax.results 返回的数据结构是 [{id:1,text:"a"},{id:2,text:"b"}, ...].

 

select2.js

//source code
* @param options.results a function(remoteData, pageNumber, query) that converts data returned form the remote request to the format expected by Select2.
*      The expected format is an object containing the following keys:
*      results array of objects that will be used as choices
*      more (optional) boolean indicating whether there are more results available
*      Example: {results:[{id:1, text:'Red'},{id:2, text:'Blue'}], more:true}
源码中在ajax的success函数中回调ajax.results

复制代码

//source code
success: function (data) {
     // TODO - replace query.page with query so users have access to term, page, etc.
     // added query as third paramter to keep backwards compatibility
     var results = options.results(data, query.page, query);
     query.callback(results);
}

复制代码

 其实ajax.results是把请求回的数据在传递给query.callback以前先格式化成 [{id:a,text:"a"},{id:b,text:"b"}, ...]。

复制代码

//source code
callback: this.bind(function (data) {

                    // ignore a response if the select2 has been closed before it was received
                    if (!self.opened()) return;


                    self.opts.populateResults.call(this, results, data.results, {term: term, page: page, context:context});
                    self.postprocessResults(data, false, false);

                    if (data.more===true) {
                        more.detach().appendTo(results).html(self.opts.escapeMarkup(evaluate(self.opts.formatLoadMore, self.opts.element, page+1)));
                        window.setTimeout(function() { self.loadMoreIfNeeded(); }, 10);
                    } else {
                        more.remove();
                    }
                    self.positionDropdown();
                    self.resultsPage = page;
                    self.context = data.context;
                    this.opts.element.trigger({ type: "select2-loaded", items: data });
                })});

复制代码

 

 query.callback则处理一些逻辑,确保下拉框选项被选中时触发 .selectChoice。

复制代码

//source code
selectChoice: function (choice) {

            var selected = this.container.find(".select2-search-choice-focus");
            if (selected.length && choice && choice[0] == selected[0]) {

            } else {
                if (selected.length) {
                    this.opts.element.trigger("choice-deselected", selected);
                }
                selected.removeClass("select2-search-choice-focus");
                if (choice && choice.length) {
                    this.close();
                    choice.addClass("select2-search-choice-focus");
                    this.opts.element.trigger("choice-selected", choice);
                }
            }
        }

复制代码

 

所以,若是results格式错误,就会致使在执行.selectChoice的时候.select2-search-choice-focus不能被添加到DOM元素上(会致使点击选项之后,选项并不会被选中)

解决方案:

1

2

3

4

5

6

7

8

9

10

11

12

13

results: function (data, page) {

  //data = { results:[{ItemId:1,ItemText:"a"},{ItemId:2,ItemText:"b"}] };

    var array = data.results;

    var i = 0;

    while(i < array.length){

        array[i]["id"] = array[i]['ItemId'];

        array[i]["text"] = array[i]['ItemText'];

        delete array[i]["ItemId"];

        delete array[i]["ItemText"];

    i++;

    }

    return { results: array };

  }

  

也能够手动更改对象的属性名.

select.js是这么处理的的

1

2

3

4

5

6

7

8

9

10

11

12

//source code<br>id: function (e) { return e == undefined ? null : e.id; },

    text: function (e) {

      if (e && this.data && this.data.text) {

        if ($.isFunction(this.data.text)) {

          return this.data.text(e);

        else {

          return e[this.data.text];

        }

      else {

        return e.text;

      }

    },

因此,咱们只要添加函数就能够覆盖默认的对象属性名了。

$('#mySelect').select2({

  id: function (item) { return item.ItemId },

  // text: function (item) { return item.ItemText }, //not work  formatSelection: function (item) { return item.ItemText } //works

});

  

另外一个遇到的问题就是语言本地化。

发现直接引入语言包并不生效,因此直接使用函数改写了提示语言。

var localLang = {

        noResults: function() {

            return '未找到匹配选项'

        },

        inputTooShort: function (args) {

            var remainingChars = args.minimum - args.input.length;

            var message = '请输入' + remainingChars + '个或更多文字';

            return message;

        },

        searching: function () {

            return '搜索中…';

            }   

    }

 

 $('#select2').select2({

     language: localLang,

})

 

获取选中的名

 var cardTypeW = $("#cardType option:checked").text();

获取选中的值

写法1

var cardTypeW = $("#cardType option:checked").val();

写法2

var cardTypeW = $("#cardType").find("option:selected").val();

相关文章
相关标签/搜索