Created
January 26, 2016 23:23
-
-
Save martijn94/9d72c1af9bf14337afde to your computer and use it in GitHub Desktop.
Select2 ajax wp api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function formatResult (company) { | |
if (company.loading) return company.text; | |
var markup = "<div class='select2-result-company clearfix'>" + | |
"<div class='select2-result-company__meta'>" + | |
"<div class='select2-result-company__title'>" + company.company_name + "</div>" + | |
"<div class='select2-result-company__number'>" + company.company_number + "</div>" + | |
"</div></div>"; | |
return markup; | |
} | |
function formatSelection (company) { | |
return company.company_name || company.text; | |
} | |
jQuery('.coc-autocomplete select').select2({ | |
ajax: { | |
url: "http://yoursite.com/wp-json/somename/search", | |
dataType: 'json', | |
delay: 500, | |
data: function (params) { | |
return { | |
keyword: params.term, | |
}; | |
}, | |
processResults: function (data, params) { | |
return { | |
results: data.items, | |
}; | |
}, | |
cache: true | |
}, | |
escapeMarkup: function (markup) { return markup; }, // let our custom formatter work | |
minimumInputLength: 3, | |
templateResult: formatResult, // omitted for brevity, see the source of this page | |
templateSelection: formatSelection // omitted for brevity, see the source of this page | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment