最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Select2 option templateResult not being called - Stack Overflow

programmeradmin4浏览0评论

I would like to use templateResult option to format my results using select2 v4. I have the following code:

$(".search").select2({
    minimumInputLength: 3,
    ajax: {
        url: url,
        dataType: 'json',
        delay: 250,
        processResults: function (data) {
            return {
                results: data.items
            };
        },
        templateResult: function (data) {
            console.log('templateResult');
            return '<span><img src="//example/img.png" /> ' + data.text + '</span>';
        },
    }
});

However, templateResult doesn't appear to be getting called as nothing is outputted to the console. Even if I change the return to 'TEST', the default results are still displayed. The code works the same whether I include the templateResult or not.

I would like to use templateResult option to format my results using select2 v4. I have the following code:

$(".search").select2({
    minimumInputLength: 3,
    ajax: {
        url: url,
        dataType: 'json',
        delay: 250,
        processResults: function (data) {
            return {
                results: data.items
            };
        },
        templateResult: function (data) {
            console.log('templateResult');
            return '<span><img src="//example./img.png" /> ' + data.text + '</span>';
        },
    }
});

However, templateResult doesn't appear to be getting called as nothing is outputted to the console. Even if I change the return to 'TEST', the default results are still displayed. The code works the same whether I include the templateResult or not.

Share Improve this question edited Feb 27, 2019 at 14:35 kyle 6891 gold badge7 silver badges17 bronze badges asked Feb 27, 2019 at 13:29 xylarxylar 7,67317 gold badges59 silver badges105 bronze badges 3
  • 1 Odd - it seems to work fine here: jsfiddle/RoryMcCrossan/waxt54cv. – Rory McCrossan Commented Feb 27, 2019 at 13:55
  • Thanks - that's without the ajax call but it helped me spot the issue. The templateResult option was inside the ajax callback. – xylar Commented Feb 27, 2019 at 14:09
  • Ahh yes! I missed that myself. Glad you got it working – Rory McCrossan Commented Feb 27, 2019 at 14:11
Add a ment  | 

2 Answers 2

Reset to default 16

I had the templateResult inside ajax scope. Fix is below:

$(".search").select2({
    minimumInputLength: 3,
    ajax: {
        url: url,
        dataType: 'json',
        delay: 250,
        processResults: function (data) {
            return {
                results: data.items
            };
        },
    },
    templateResult: function (data) {
        console.log('templateResult');
        return '<span><img src="//example./img.png" /> ' + data.text + '</span>';
    }
});

Check your processResults - results: data.items, I switched to data.results

发布评论

评论列表(0)

  1. 暂无评论