Reputation: 801
I'm using Select2 4.0 as the code update has broken some existing code:
Data is loaded through an AJAX array structured as followed
['id':1,'text':'hello-world','artefact_type'=>'CONTACT'], ['id':2,'text':'test','artefact_type'=>'ORGANISATION'],
The plugin is configured as followed:
...
ajax: [
'url' => $url,
'dataType': 'json',
'data' :'function(params) { return {q:params.term}; }',
'processResults':'function(out) {console.log(out.results); return {results:out.results}; }',
],
templateResult: 'function(data) {
return data.text+" - "+data.artefact_type;
}'
...
The AJAX is loading properly, as the template results displays both the text and artefact_type.
Now I try to implement the change function
function (e) {
element = $("#quick-search");
alert(element.val()+'++'+ e.val());
artefact_type = ??;
if (data != "") {
var url = "";
if (artefact_type == "CONTACT") {
url = "/xrm/contact/detail?id=" + element.val();
}
if (artefact_type == "ORGANIZATION") {
url = "/xrm/organization/detail?id=" + element.val();
}
if (artefact_type == "COMMITTEE") {
url = "/xrm/committee/detail?id=" + element.val();
}
$(location).attr('href', url);
}
else
{
$("#test").html("Please select a sector operator...");
}
}
Previously I could do element.selected.artefact_type to achieve this. How do I get the artefact_type corresponding to the selected value? I am additionally not sure if data-attributes are actually being created.
Please help! Thank you
Upvotes: 1
Views: 2842
Reputation: 801
Solved it by using $("#quick-search").select2('data')[0];
Same question was asked the day after:
Adding "data-" attributes with select2
Upvotes: 2