Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. (function($) {
  2. $(function() {
  3. var options = $.extend({
  4. 'fieldName': '#s',
  5. 'maxRows': 10,
  6. 'minLength': 4
  7. }, SearchAutocomplete);
  8.  
  9. options.fieldName = $('<div />').html(options.fieldName).text();
  10.  
  11. $(options.fieldName).autocomplete({
  12. source: function( request, response ) {
  13. $.ajax({
  14. url: options.ajaxurl,
  15. dataType: "json",
  16. data: {
  17. action: 'autocompleteCallback',
  18. term: this.term
  19. },
  20. success: function( data ) {
  21. console.log('success');
  22. response( $.map( data.results, function( item ) {
  23. return {
  24. label: item.title,
  25. value: item.title,
  26. url: item.url
  27. }
  28. }));
  29. },
  30. error: function(jqXHR, textStatus, errorThrown) {
  31. console.log(jqXHR, textStatus, errorThrown);
  32. }
  33. });
  34. },
  35. delay: options.delay,
  36. minLength: options.minLength,
  37. autoFocus: ( options.autoFocus === 'true' ),
  38. search: function(event, ui) {
  39. $(event.currentTarget).addClass('sa_searching');
  40. },
  41. create: function(item) {
  42. console.log('creating');
  43. $(this).data('ui-autocomplete')._renderItem = function (ul, item) {
  44. return $('<li>')
  45. .append('<a>' + item.label + '<br>' + item.value + '</a>')
  46. .appendTo(ul);
  47. };
  48. },
  49. select: function( event, ui ) {
  50. if ( ui.item.url !== '#' ) {
  51. location = ui.item.url;
  52. } else {
  53. return true;
  54. }
  55. },
  56. open: function(event, ui) {
  57. var acData = $(this).data('uiAutocomplete');
  58. acData
  59. .menu
  60. .element
  61. .find('a')
  62. .each(function () {
  63. var $self = $( this ),
  64. keywords = $.trim( acData.term ).split( ' ' ).join('|');
  65. $self.html($self.text().replace(new RegExp("(" + keywords + ")", "gi"), '<span class="sa-found-text">$1</span>'));
  66. });
  67. $(event.target).removeClass('sa_searching');
  68. },
  69. close: function() {
  70. }
  71. })
  72. });
  73. })(jQuery);
  74.  
  75. success: function( data ) {
  76. console.log('success');
  77. response( $.map( data.results, function( item ) {
  78. return {
  79. label: item.title,
  80. value: item.title,
  81. url: item.url
  82. }
  83. }));
  84. },
  85.  
  86. create: function(item) {
  87. console.log('creating');
  88. $(this).data('ui-autocomplete')._renderItem = function (ul, item) {
  89. return $('<li>')
  90. .append('<a>' + item.label + '<br>' + item.value + '</a>')
  91. .appendTo(ul);
  92. };
  93. },
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement