使用select2 ajax时,Select2不允许选择值

时间:2014-12-10 06:47:32

标签: javascript angularjs angularjs-select2

我正在尝试使用select2-AJAX在angularJS中使用select2作为远程数据,当我在http://ivaynberg.github.io/select2/上给出示例时,它可以正常工作,但是当我使用自己的代码时,它不允许我选择价值。

$scope.select2Options = {
allowClear: true,
minimumInputLength: 3,

ajax: { // instead of writing the function to execute the request we use Select2's convenient helper
    url: rootURL.tsURL + "valueSets/dy346:fhir.observation-codes/concepts/_fhir",
    dataType: 'json',
    quietMillis: 250,

    id: function(item) {
        console.log(item);
        return data.item['CodeableConcept.coding']['Coding.code'];
    },

    transport: function(params) {
        params.beforeSend = function(request) {
            request.setRequestHeader("Authorization", userService.tsConfig.headers.Authorization);
        };
        return $.ajax(params);
    },
    data: function(term, page) {
        return {
            criteria: term,
            matchType: "StartsWith",
            limit: "8",
            offset: "0"
        };
    },
    cache: true,

    results: function(data, page) { // 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

        var org = normalizeJSONLD.findAllObjectsByType(data['@graph'], "fhir:CodeableConcept");
        var object = normalizeJSONLD.normalizeLD(data['@graph'], org);

        return {
            results: object
        };
    }
},

formatResult: function(item) {
    console.log(item);
    return item['CodeableConcept.coding']['Coding.code'] + ": " + item['CodeableConcept.coding']['Coding.display'];
},
formatSelection: function(item) {
    return item['CodeableConcept.coding']['Coding.code'];
}

};

在Chrome开发工具中,select2

  • 有一个“select2-result-unselectable”类,它不允许我选择值。

  • 1 个答案:

    答案 0 :(得分:2)

    你只是在你的ajax调用中放置id函数,而它应该作为键放在select2Options上下文中......

    $scope.select2Options = {
    allowClear: true,
    minimumInputLength: 3,
    
    ajax: { 
        url: rootURL.tsURL + "valueSets/dy346:fhir.observation-codes/concepts/_fhir",
        dataType: 'json',
        quietMillis: 250,
    
    transport: function(params) {
        params.beforeSend = function(request) {
            request.setRequestHeader("Authorization", userService.tsConfig.headers.Authorization);
        };
        return $.ajax(params);
    },
    data: function(term, page) {
        return {
            criteria: term,
            matchType: "StartsWith",
            limit: "8",
            offset: "0"
        };
    },
    cache: true,
    
    results: function(data, page) { // 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
    
        var org = normalizeJSONLD.findAllObjectsByType(data['@graph'], "fhir:CodeableConcept");
        var object = normalizeJSONLD.normalizeLD(data['@graph'], org);
    
        return {
                results: object
            };
        }
    },
    formatResult: function(item) {
        console.log(item);
        return item['CodeableConcept.coding']['Coding.code'] + ": " + item['CodeableConcept.coding']['Coding.display'];
    },
    formatSelection: function(item) {
        return item['CodeableConcept.coding']['Coding.code'];
    },
    
    // id should be defined over here...
        id: function(item) {
                console.log(item);
                return data.item['CodeableConcept.coding']['Coding.code'];
            }