How do use ajax url with a url like this, foo/:foo_id/boo

31 views
Skip to first unread message

kare...@gmail.com

unread,
May 28, 2016, 9:20:08 PM5/28/16
to select2
I'm trying to chain some selects and the second one needs the selected value from the first select so that when it calls the ajax request it returns records specific to that first select.

But when I build the ajax, the value being embedded from the first select is undefined. Any thoughts?

$(".select2-foos").select2({
minimumInputLength: 3
, allowClear: true
, ajax: {
url: "/api/v1/foos"
, dataType: 'json'
, type: 'GET'
, delay: 250
, data: function(params) {
return {
search: params.term
, page: 10
};
}
, processResults: function(data, page) {
// 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
return { results: $.map(data, function(d, i) {
return { id: d.foo_id, text: d.foo_name }
})
};
}
, cache: true
}
, escapeMarkup: function(markup) { return markup; } // let our custom formatter work
, theme: "bootstrap"
, placeholder: "Please select a foo"
});

$(".select2-boos").select2({
minimumInputLength: 3
, allowClear: true
, ajax: {
url: function(params) {
return '/api/v1/foos/' + $('select2-foos').val() + '/boos';
}
, dataType: 'json'
, type: 'GET'
, delay: 250
, data: function(params) {
return {
search: params.term // search term
, page: 10
};
}
, processResults: function(data, page) {
// 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
return { results: $.map(data, function(d, i) {
return { id: d.boo_id, text: d.boo_name }
})
};
}
, cache: true
}
, escapeMarkup: function(markup) { return markup; } // let our custom formatter work
, theme: "bootstrap"
, placeholder: "Please select a boos"
});

Kevin Brown

unread,
Jun 6, 2016, 5:16:58 PM6/6/16
to select2
The following question on Stack Overflow may help you achieve what you are looking for


--
You received this message because you are subscribed to the Google Groups "select2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to select2+u...@googlegroups.com.
To post to this group, send email to sel...@googlegroups.com.
Visit this group at https://groups.google.com/group/select2.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages