How do I set the first value shown in the select2 (before the user searches) and so that I can configure the templating extra fields (state and city)?
$('.postcode2').select2({
ajax: {
url: function (params) {
return "{{ url('/') }}" + "/api/postcodes/" + params.term;
},
processResults: function (data) {
return {
results: data,
};
},
cache: true,
dataType: 'json',
delay: 100,
},
minimumInputLength: 2,
escapeMarkup: function(result) {
return result;
},
templateSelection: function(result) {
return result['text'] + ' (' + result['city'] + ', ' + result['state'] + ')';
},
templateResult: function (result) {
if (result.loading) return 'Searching...';
return '<strong>' + result['text'] + '</strong>' +
'<h6>' + result['city'] + ', ' + result['state'] + '</h6>';
},
});