autocomplete.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /**
  2. * @version 7.1
  3. */
  4. if ($('#basic-search')) {
  5. $('#basic-search')
  6. .autocomplete({
  7. source: function (request, response) {
  8. $.post(
  9. paths.typeahead,
  10. {
  11. term: request.term
  12. },
  13. function (data) {
  14. //map the data into a response that will be understood by the autocomplete widget
  15. response($.map(data, function (item) {
  16. return {
  17. value: item.value,
  18. label: item.label,
  19. category_id: item.category_id,
  20. category_name: item.category_name,
  21. listing_id: item.listing_id,
  22. no_link: item.no_link
  23. }
  24. }));
  25. },
  26. 'json'
  27. );
  28. },
  29. minLength: 3,
  30. //when you have selected something
  31. select: function (event, ui) {
  32. var srcForm = $(this).closest('form');
  33. if (ui.item.category_id > 0) {
  34. srcForm.find("[name='parent_id']")
  35. .append('<option value="' + ui.item.category_id + '" selected="selected">' + ui.item.category_name + '</option>');
  36. }
  37. srcForm.find("[name='keywords']").val(ui.item.value);
  38. // IMPORTANT: if the id of the submit button is "submit", then the below call wont work!!
  39. srcForm.submit();
  40. //close the drop down
  41. this.close;
  42. },
  43. //show the drop down
  44. open: function () {
  45. $(this).removeClass("ui-corner-all").addClass("ui-corner-top");
  46. },
  47. //close the drop down
  48. close: function () {
  49. $(this).removeClass("ui-corner-top").addClass("ui-corner-all");
  50. }
  51. })
  52. .data("ui-autocomplete")._renderItem = function (ul, item) {
  53. var selector = $("<li></li>")
  54. .data("item.ui-autocomplete", item);
  55. if (item.no_link == true) {
  56. selector.append(item.label)
  57. }
  58. else {
  59. selector.append('<a>' + item.label + '</a>');
  60. }
  61. selector.appendTo(ul);
  62. return selector;
  63. };
  64. }