Javascript-форум (https://javascript.ru/forum/)
-   Общие вопросы Javascript (https://javascript.ru/forum/misc/)
-   -   $.ajax в autocomplete (https://javascript.ru/forum/misc/36732-%24-ajax-v-autocomplete.html)

Bezlepkin 25.03.2013 19:21

$.ajax в autocomplete
 
Привет! Подскажите как осуществить $.ajax в autocomplete
$(input).autocomplete({
    source: function(request, response) {
      $.ajax({
        url: Drupal.settings.geo.path,
        dataType: 'json',
        data: {
          act: 'get_other_cities',
          country: $(selects['country']).val(),
          value: request.term,
        },
        success: function(data) { <- вот тут не знаю как
          response({
            return{
            }
          }); <- и до сюда
        }
      });
    },
    autoFocus: true,
  });


Как осуществить success?

danik.js 25.03.2013 19:50

Цитата:

Сообщение от Bezlepkin
Как осуществить success?

www.successbird.com/

Bezlepkin 25.03.2013 20:46

:)
Я имел ввиду как вывести данные!

Bezlepkin 25.03.2013 22:21

Сделал так:

$(input).autocomplete({
    source: function(request, response) {
      $.ajax({
        url: Drupal.settings.geo.path,
        dataType: 'json',
        data: {
          act: 'get_other_cities',
          country: $(selects['country']).val(),
          value: request.term,
        },
        success: function(data) {
          response($.map(data.cities, function(item) {
          
         var elementList = '<span class="city">' + item[1] + '</span>';
         if (item[2] != '') {
           elementList += item[2] + ',<br>';
         }
         if (item[3] != '') {
           elementList += item[3];
         } 
         return {
            label: elementList,
            value: item[1]
         }
         }));
        }
      });
    },
    autoFocus: true,
  });


Возвращается все, но html теги выводятся строкой (не обробатываются)

Hoshinokoe 26.03.2013 11:55

Bezlepkin,

Используй .html() вместо .text() и будет счастье.

Неплохо было бы показать функцию response, а так остается только гадать.

Bezlepkin 26.03.2013 16:59

А где вы увидели .text()?
А разьве это не функция response?

response($.map(data.cities, function(item) {
         var elementList = '<span class="city">' + item[1] + '</span>';
         if (item[2] != '') {
           elementList += item[2] + ',<br>';
         }
         if (item[3] != '') {
           elementList += item[3];
         }
         return {
            label: elementList,
            value: item[1]
         }
         }));

Bezlepkin 26.03.2013 17:00

Я еще видел такую фишку:
.data(и тут как то выводится html и все параметры для autocomplete)

Bezlepkin 27.03.2013 10:50

Сделал так, все работает.
А можно ли поменять классы для ul и li в выпадающем списке autocomplete?

$(input).autocomplete({
    source: function(request, response) {
      $.ajax({
        url: Drupal.settings.geo.path,
        dataType: 'json',
        data: {
          act: 'get_other_cities',
          country: $(selects['country']).val(),
          value: request.term,
        },
        success: function(data) {
          response($.map(data.cities, function(item) {
            return {
              cityID:   item[0],
              label:    item[1],
              value:    item[1],
              city:     item[1],
              state:    item[2],
              district: item[3]
              
            }

          }));
        }
      });
    },
    autoFocus: true,
  })
  .data('autocomplete')._renderItem = function( ul, item ) {
    return $( '<li onmousemove="Select.itemMouseMove(5, 17, this)" val="' + item.cityID + '"></li>' )
      .data( "item.autocomplete", item )
      .append('<a href=" + item.url + ">' + '<span class="city">' + item.city + '</span>' + item.state + "<br>" + item.district + "</a>")
      .appendTo( ul );
    };


Часовой пояс GMT +3, время: 09:10.