Skip to content

Instantly share code, notes, and snippets.

@harshvb7
Last active July 29, 2016 11:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harshvb7/52e198aa0c91f8920ca346bc151cc2c4 to your computer and use it in GitHub Desktop.
Save harshvb7/52e198aa0c91f8920ca346bc151cc2c4 to your computer and use it in GitHub Desktop.
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
<script>
var company_field = jQuery('#acf-field-company_name');
var is_admin = jQuery('#post_author_override')[0];
if(is_admin != undefined){
company_field.select2({
ajax: {
url: 'http://localhost:8000/get_user_companies/',
processResults: function (data) {
var arr = []
jQuery.map(data.companies, function(val, i){
arr.push({id:val.slug, text: val.name})
});
return {
results: arr
};
},
xhrFields: {
withCredentials: true
},
method: 'POST',
dataType: 'json',
}
});
}
else{
jQuery.ajax({
url: 'http://localhost:8000/get_user_companies/',
method: 'POST',
dataType: 'json',
xhrFields: {
withCredentials: true
}
})
.done(function (result) {
data = result.companies
jQuery.map(data, function (val, i) {
company_field.append('<option value='+ val.slug +'>'+ val.name +'</option>');
});
});
company_field.select2();
}
var post_id = jQuery('#post_ID').val();
if (post_id){
jQuery.ajax({
url: 'http://localhost:8000/get_post_companies/',
method: 'POST',
data: {'post_id': post_id},
dataType: 'json',
xhrFields: {
withCredentials: true
}
})
.done(function (result) {
data = result.companies
jQuery.map(data, function (val, i) {
company_field.append('<option value='+ val.slug +' selected>'+ val.name +'</option>');
});
});
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment