简体   繁体   English

由Select2触发的插件

[英]Select2 plugin fired by

newbie here first, my english is not good enough to describe the problem that i'm facing right now, so consider to see my code below 首先是新手,我的英语水平不足以描述我现在面临的问题,因此请考虑在下面查看我的代码

$('#selectOriginAirport, #selectDestinationAirport').select2({
            placeholder: 'Select Airport',
            ajax: {
                url: '{{url('get-airports')}}',
                dataType: 'json',
                delay: 250,
                data: function(params){
                    return { keyword: params.term };
                },
                processResults: function(datas, params) {
                    return {
                        results: $.map(datas.data, function(item) {
                            return {
                                text: item.cityName + ' - '+item.airportName + ' ('+item.airportCode+')',
                                id: item.airportCode+'|'+item.cityName,
                                lat: item.airportLatitude,
                                lon: item.airportLongitude
                            }
                        })
                    };
                },
                cache: true
            },
            escapeMarkup: function (markup) { 
                // console.log('markup >>> ' + markup);
                return markup; 
            },
            minimumInputLength: 3,
            templateResult: function(data) {
                // console.log('data >>> ' + data);
                if(data.loading) {
                    return data.text;
                }
                var markup = '<p>'+data.text+'</p>';
                return markup;
            },
            templateSelection: function(data) {
                console.log(data);
                if($(this).is('#selectOriginAirport')){
                    console.log('pepaya');
                    $("[name='flightOriginLat']").val(data.lat);
                    $("[name='flightOriginLon']").val(data.lon);
                }
                if($(this).is('#selectDestinationAirport')){
                    console.log('kates');
                    $("[name='flightDestinationLat']").val(data.lat);
                    $("[name='flightDestinationLon']").val(data.lon);
                // }
                return data.airportName || data.text;
            }
        });

first take a look that i fire select2 by #selectOriginAirport and selectDestinationAirport 首先看一下我通过#selectOriginAirport和selectDestinationAirport触发select2

the problem is i need to make a conditional on the templateSelection function but its not work, the result is none of that 2 logical is executed 问题是我需要在templateSelection函数上设置一个条件,但是它不起作用,结果是没有两个逻辑被执行

thats the problem i need to solve i wish you get what i mean Thanks in advance 那就是我需要解决的问题,我希望你能明白我的意思。

I checked the source code for select2 and it looks like select2 does pass the container as the second parameter in the templateSelection option. 我检查了select2的源代码,看起来select2 确实将容器作为templateSelection选项中的第二个参数传递了。 Here's the relevant snippet from the select2.js 这是select2.js中的相关片段

SingleSelection.prototype.display = function (data, container) {
  var template = this.options.get('templateSelection');
  var escapeMarkup = this.options.get('escapeMarkup');

  return escapeMarkup(template(data, container));
};

Using that and JSFiddle's /echo/json as a sample AJAX, I've created a working snippet: http://jsfiddle.net/shashank2104/ozy16L8s/2/ 使用该代码和JSFiddle的/echo/json作为示例AJAX,我创建了一个有效的代码段: http : //jsfiddle.net/shashank2104/ozy16L8s/2/

Relevant code: 相关代码:

templateSelection: function(selection,inst) {
   if(inst && inst.length) {
       return inst.attr('id') === 'select2-user-email-address-container' ? selection.email : selection.id;
   }
}

Based on the container ID, the appropriate attribute can be chosen. 根据容器ID,可以选择适当的属性。 Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM