2017-08-03 59 views
1

我希望得到我的Shopify內我的搜索欄自動完成設置阿賈克斯/ jQuery的自動完成這個商城 - 液體使用和Ajax在搜索框(Shopify)

我發現這個教程,並實現它,因爲它說,然而它不起作用,網站上的任何搜索欄上都沒有自動完成 - 我認爲它可能比較陳舊,並且在最小/ Shopify搜索功能的更新/更改之前寫入。

https://help.shopify.com/themes/customization/store/enable-autocomplete-for-search-boxes

我可以用Chrome瀏覽器開發工具,遵循它通過,而現在看來似乎被卡住的地方增加了搜索結果列表$('<ul class="search-results"></ul>').appendTo($(this)).hide();,追蹤網頁的HTML時,這不會出現。這意味着當它稍後嘗試查找此列表var resultsList = form.find('.search-results');時,它不會找到它,因此無法填充項目。

我正在運行Minimal主題。 該網站是testing site在頂部灰色標題搜索欄,並且還通/搜索

通過Shopify內置演示此自動完成的測試位點位於[HTTPS]://search-autocomplete.myshopify.com/ 。在頁面加載時,<ul>追加已經存在。

編輯:

做了一些更多的挖掘,我在開發者工具碰到這個錯誤絆倒 -

Uncaught ReferenceError: jQuery is not defined at (index):7031

哪個,你猜對了,下面的jQuery代碼的第一行。 $(function() {任何想法爲什麼jQuery是未定義的?該腳本包含在</body>之前的索引文件的底部,因此jquery.min.js應該在那之前加載,網站上的其他jQuery工作正常。在測試現場

表單代碼

<form action="/search" method="get" class="site-header__search small--hide" role="search"> 
     {% comment %}<input type="hidden" name="type" value="product">{% endcomment %} 
     <div class="site-header__search-inner"> 
      <label for="SiteNavSearch" class="visually-hidden">{{ 'general.search.placeholder' | t }}</label> 
      <input type="search" name="q" id="SiteNavSearch" placeholder="{{ 'general.search.placeholder' | t }}" aria-label="{{ 'general.search.placeholder' | t }}" class="site-header__search-input"> 
     </div> 

     <button type="submit" class="text-link site-header__link site-header__search-submit"> 
      {% include 'icon-search' %} 
      <span class="icon__fallback-text">{{ 'general.search.submit' | t }}</span> 
     </button> 
     </form> 

search.json

{% layout none %} 
{% capture results %} 
    {% for item in search.results %} 
{% assign product = item %} 
{ 
    "title" : {{ product.title | json }}, 
    "url"  : {{ product.url | within: product.collections.last | json }}, 
    "thumbnail": {{ product.featured_image.src | product_img_url: 'thumb' | json }} 
} 
{% unless forloop.last %},{% endunless %} 
{% endfor %} 
{% endcapture %} 
{ 
"results_count": {{ search.results_count }}, 
"results": [{{ results }}] 
} 

搜索autocomplete.liquid

<script> 
$(function() { 
    // Current Ajax request. 
    var currentAjaxRequest = null; 

    // Grabbing all search forms on the page, and adding a .search-results list to each. 
    var searchForms = 
$('form[action="/search"]').css('position','relative').each(function() { 

// Grabbing text input. 
    var input = $(this).find('input[name="q"]'); 

    // Adding a list for showing search results. 
    var offSet = input.position().top + input.innerHeight(); 
    $('<ul class="search-results"></ul>').css({ 'position': 'absolute', 'left': '0px', 'top': offSet }).appendTo($(this)).hide();  

// Listening to keyup and change on the text field within these search forms. 
    input.attr('autocomplete', 'off').bind('keyup change', function() { 

// What's the search term? 
    var term = $(this).val(); 

// What's the search form? 
    var form = $(this).closest('form'); 

// What's the search URL? 
    var searchURL = '/search?type=product&q=' + term; 

// What's the search results list? 
    var resultsList = form.find('.search-results'); 

    // If that's a new term and it contains at least 3 characters. 
    if (term.length > 3 && term != $(this).attr('data-old-term')) { 

    // Saving old query. 
    $(this).attr('data-old-term', term); 

    // Killing any Ajax request that's currently being processed. 
    if (currentAjaxRequest != null) currentAjaxRequest.abort(); 

    // Pulling results. 
    currentAjaxRequest = $.getJSON(searchURL + '&view=json', function(data) { 
     // Reset results. 
     resultsList.empty(); 

    // If we have no results. 
     if(data.results_count == 0) { 

    // resultsList.html('<li><span class="title">No results.</span></li>'); 
     // resultsList.fadeIn(200); 
     resultsList.hide(); 
     } else { 

     // If we have results. 
     $.each(data.results, function(index, item) { 
      var link = $('<a></a>').attr('href', item.url); 
      link.append('<span class="thumbnail"><img src="' + item.thumbnail + '" /></span>'); 
      link.append('<span class="title">' + item.title + '</span>'); 
      link.wrap('<li></li>'); 
      resultsList.append(link.parent()); 
     }); 

     // The Ajax request will return at the most 10 results. 
     // If there are more than 10, let's link to the search results page. 
     if(data.results_count > 10) { 
      resultsList.append('<li><span class="title"><a href="' + searchURL + '">See all results (' + data.results_count + ')</a></span></li>'); 
     } 
     resultsList.fadeIn(200); 
     }   
    }); 
    } 
}); 
}); 

// Clicking outside makes the results disappear. 
$('body').bind('click', function(){ 
    $('.search-results').hide(); 
}); 
}); 
</script> 

回答

0

這裏爲子孫後代和完整性。

我終於找到了工作,並做了一些調整。

要解決jQuery的未定義錯誤我替換第一行: window.onload = (function() {

出於某種原因,結果列表是越來越顯示:從某處塊,但我找不到它,所以使用jQuery我將其更改爲使其顯示的塊。同樣在這個代碼行中,我修改了term.length以在2上啓動ajax請求,否則,如果您鍵入cat/dog,則需要輸入另一個字母或空格來開始搜索。

// If that's a new term and it contains at least 2 characters. if (term.length > 2 && term != $(this).attr('data-old-term')) { $('<ul class="search-results"></ul>').css({ 'display': 'block'})

搜索結果現在出現,並且正確的!只需要對位置進行一些CSS調整即可。