$('#zip').select2({ ajax: { url: '/posters.search.php', dataType: 'json', delay: 250, data: function (params) { console.log(params); return { q: params.term, // search term page: params.page }; }, processResults: function (data, params) { // 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, except to indicate that infinite // scrolling can be used params.page = params.page || 1; // console.log(data+' '+params); return { results: data.items, pagination: { more: (params.page * 30) < data.total_count } }; }, cache: true }, multiple: true, allowClear: true, placeholder: 'You can search for zip codes', language: { inputTooShort: function(args) { // args.minimum is the minimum required length // args.input is the user-typed text return ' Search by typing a zip code'; }, inputTooLong: function(args) { // args.maximum is the maximum allowed length // args.input is the user-typed text return 'Du har indtastet for meget'; }, errorLoading: function() { return 'fejl ved hentning af resultater'; }, loadingMore: function() { return 'Henter flere resultater'; }, noResults: function() { return 'Der blev ikke fundet nogen resultater'; }, searching: function() { return 'Søger, vent venligst...'; }, maximumSelected: function(args) { // args.maximum is the maximum number of items the user may select return 'fejl ved hentning af resultater'; } }, escapeMarkup: function (markup) { return markup; }, // let our custom formatter work minimumInputLength: 2, templateResult: formatRepo, // omitted for brevity, see the source of this page templateSelection: formatRepoSelection // omitted for brevity, see the source of this page }); function formatRepo (repo) { if (repo.loading) return repo.text; // console.log(repo); var markup = '
' + // '
'+ '
' + '
' + repo.text + '
'; if (repo.description) { markup += '
' + repo.description + '
'; } // markup += '
' + // '
' + repo.forks_count + ' Forks
' + // '
' + repo.stargazers_count + ' Stars
' + // '
' + repo.watchers_count + ' Watchers
' + // '
' + '
'; return markup; } function formatRepoSelection (repo) { return repo.text; } $($('#zip').data('select2').$container).addClass('search-container');