Well, I'm sure something is not so simple here, but I'm not 100% what it is.
Therefore, I am trying to use the Select2 AJAX method as a way to search for users in a database and select a result. The call itself returns with the results, however it will not allow me to select an answer from the list. It also does not allow you to βselectβ it when you hover or the up / down arrow. So, without further ado, here is my code:
index.html
<html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="select2/select2.css" media="screen" /> <script src="select2/select2.js"></script> <script src="select.js"></script> </head> <body> <input type="text" style="width: 500px" class="select2"> </body> </html>
select.js
jQuery(function() { var formatSelection = function(bond) { console.log(bond) return bond.name } var formatResult = function(bond) { return '<div class="select2-user-result">' + bond.name + '</div>' } var initSelection = function(elem, cb) { console.log(elem) return elem } $('.select2').select2({ placeholder: "Policy Name", minimumInputLength: 3, multiple: false, quietMillis: 100, ajax: { url: "http://localhost:3000/search", dataType: 'json', type: 'POST', data: function(term, page) { return { search: term, page: page || 1 } }, results: function(bond, page) { return {results: bond.results, more: (bond.results && bond.results.length == 10 ? true: false)} } }, formatResult: formatResult, formatSelection: formatSelection, initSelection: initSelection }) })
JSON response
{ error: null, results: [ { name: 'Some Name', _id: 'Some Id' }, { name: 'Some Name', _id: 'Some Id' } ] }
Everything seems to be in order, but I canβt actually select the answer and enter it in the field. Is there a problem in my code?
javascript jquery ajax jquery-select2 jquery-select2-3
Dropped.on.Caprica Feb 11 '13 at 20:02 2013-02-11 20:02
source share