Google Custom Search API Autocomplete?

前端 未结 2 1087
陌清茗
陌清茗 2021-02-08 18:05

We\'re using the google custom search API (paid-for server-side API) to power our search results.

I\'d like to add an autocomplete feature to the search - however, does

相关标签:
2条回答
  • 2021-02-08 18:15

    At the time of writing (June 2013), there is a somewhat easier way of getting autocompletion while still getting the results as XML:

    • Use the Google Custom Search Element Control (https://developers.google.com/custom-search/docs/element).
    • Just use the Search bar control, which you can style as you like.

    <gcse:searchbox-only enableAutoComplete="true" resultsUrl="#"></gcse:searchbox-only>

    • The "trick" is that you can specify "resultsUrl" which means you can direct the actual search results to a page you generate via the XML API, without having to implement the search box UX yourself.
    0 讨论(0)
  • 2021-02-08 18:32

    Got this working something like this - hope this helps someone else :)

    $(function () {
      $('input.search')
        .focus(function () { this.select(); })
        .mouseup(function (e) { e.preventDefault(); })
        .autocomplete({
          position: {
            my: "left top",
            at: "left bottom",
            offset: "0, 5",
            collision: "none"
          },
          source: function (request, response) {
            $.ajax({
              url: "http://clients1.google.com/complete/search?q=" + request.term + "&hl=en&client=partner&source=gcsc&partnerid={GOOGLESEARCHID}&ds=cse&nocache=" + Math.random().toString(),
              dataType: "jsonp",
              success: function (data) {
                response($.map(data[1], function (item) {
                  return {
                    label: item[0],
                    value: item[0]
                  };
                }));
              }
            });
          },
          autoFill: true,
          minChars: 0,
          select: function (event, ui) {
            $(this).closest('input').val(ui.item.value);
            $(this).closest('form').trigger('submit');
          }
        });
    });
    
    0 讨论(0)
提交回复
热议问题