<div tal:define="
style field.widget.style;
oid oid|field.oid;
css_class css_class|field.widget.css_class;
optgroup_class optgroup_class|field.widget.optgroup_class;
multiple multiple|field.widget.multiple;
content_type field.widget.content_type;
key field.widget.key;
this_object_id field.widget.this_object_id;
this_object_in_list field.widget.this_object_in_list;"
tal:omit-tag="">
<style>
.form-control .select2-choice {
border: 0;
border-radius: 2px;
}
.form-control .select2-choice .select2-arrow {
border-radius: 0 2px 2px 0;
}
.form-control.select2-container {
height: auto !important;
padding: 0px;
}
.form-control.select2-container.select2-dropdown-open {
border-color: #5897FB;
border-radius: 3px 3px 0 0;
}
.form-control .select2-container.select2-dropdown-open .select2-choices {
border-radius: 3px 3px 0 0;
}
.form-control.select2-container .select2-choices {
border: 0 !important;
border-radius: 3px;
}
</style>
<input type="hidden" name="__start__" value="${name}:sequence"
tal:condition="multiple" />
<select tal:attributes="
name name;
id oid;
class string: form-control ${css_class};
data-placeholder field.widget.placeholder|None;
multiple multiple;
style style;">
</select>
<script type="text/javascript">
deform.addCallback(
'${field.oid}',
function(oid) {
//this string needs to be in quotes as $ in the template means something different in jquery
var $initial_value = '${cstruct}';
console.log($initial_value);
var $select = $('#' + oid);
$select.select2({
placeholder: "${str(field.widget.placeholder).replace('"','\\"')|""}",
allowClear: true,
ajax: {
url: "/admin/getter",
dataType: 'json',
delay: 250,
data: function (params) {
return {
key:"${key}",
content_type:"${content_type}",
this_object_id:"${this_object_id}",
this_object_in_list:"${this_object_in_list}",
query: params.term,
};
},
processResults: function (data, params) {
revised_data = []
for (i = 0; i < data.length; i++) {
result = {id:data[i]['id'], text:data[i]['text']}
revised_data.push(result)
}
console.log(data);
return {
results: revised_data,
pagination: {
}
};
},
cache: true
},
minimumInputLength: 2,
});
var $option = $('<option selected>Loading...</option>').val($initial_value); //this never displays as we update it immediately with the actual value
$select.append($option).trigger('change'); // append the option and update Select2
$.ajax({ // make the request for the selected data object
data:{initial_id:$initial_value},
url: '/admin/get_initial',
dataType: 'json'
}).then(function (data) {
// Here we should have the data object
$option.text(data[0].text).val(data[0].id); // update the text that is displayed (and maybe even the value)
$option.removeData(); // remove any caching data that might be associated
$select.trigger('change'); // notify JavaScript components of possible changes
});
}
);
</script>
<input type="hidden" name="__end__" value="${name}:sequence"
tal:condition="multiple" />
</div>