Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Select2 Rails Quick Tip - AJAX with Search

Hey Jason, Thanks for the quick tip on Select2, can you explain how to get the Select2 drop down with AJAX to filter the search results just like it does with the Simple Example.

I downloaded Select2 v 4.0 here is what I have so far:

    $(".js-example-basic-ajax").select2({
      ajax: {
        url: "/clients.json",
        dataType: 'json',
        delay: 250,
        data: function (params) {
          return {
            q: params.term, // search term
            page: params.page
          };
        },
        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(client, i) {
                    return { id: client.id, text: client.name }
                } ) };
        },
        cache: true
      },
      escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
      theme: "bootstrap",
        placeholder: "Please select a client"
    });