Select2插件不填写该字段

时间:2015-09-11 14:08:49

标签: javascript jquery jquery-select2

我正在尝试使用Select2插件,但我有疑问

的JavaScript

$(".js-example-data-array").select2({
        ajax: {
            method: "POST",
            url: "Actions/ColaboradorSvc.ashx",
            dataType: 'json',
            delay: 250,
            data: function (params) {                
                return {
                    idPeriodoGestao: idPeriodoGestao,
                    query: params.term, 
                    start: 0, 
                    limit: 10
                };
            },
            processResults: function (data, page) {
                debugger;
                // 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
                return {
                    results: data
                };
            },
            cache: true
        },
        escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
        minimumInputLength: 1        
    });

更新

我更改了processResults事件,现在它适用于自动完成

        processResults: function (data, page) {
            var array = [];
            for (var i = 0; i < data.length; i++) {

                var modelo = {
                    id: 0,
                    text: '' + data[i].Nome + ''
                };

                array.push(modelo);
            }

            // 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
            return {
                results: array
            };
        },

从我的服务返回JSON

[{
        "Id": 81,
        "IdArea": 270,      
        "Nome": "Adelice Colaço",       
        "FotoPath": null,
        "WorkflowAcoes": true,
        "Ativo": true       
    },
    {
        "Id": 346,
        "IdArea": 211,      
        "Nome": "Adelma Lira da Silva Borges",      
        "FotoPath": null,
        "WorkflowAcoes": true,
        "Ativo": true
    },
    {
        "Id": 276,
        "IdArea": 247,      
        "Nome": "Adilene Fernandes Da Silva",       
        "FotoPath": null,
        "WorkflowAcoes": true,
        "Ativo": true
    },
    {
        "Id": 277,
        "IdArea": 247,      
        "Nome": "Adriana Dias Silva Porto",     
        "FotoPath": null,
        "WorkflowAcoes": true,
        "Ativo": true
    },
    {
        "Id": 82,
        "IdArea": 250,      
        "Nome": "Agnes Jakobovitsch Costa",     
        "FotoPath": null,
        "WorkflowAcoes": true,
        "Ativo": true
    }]

渲染

Bellow,我原本期待在“Nome”属性中使用JSON中的firt寄存器自动完成Adelice Colaço但不起作用

enter image description here

0 个答案:

没有答案