0

How can I use ajax to create a "Load more" button to load more list items?

I have a list similarly to the one used in this question: jQuery load first 3 elements, click "load more" to display next 5 elements

This is how mine looks like:

product-list.liquid (custom Shopify code)

<ul id="product-container">

{% for product in collections.all.products = 1 %}

<li>

<a href="{{ product.url }}" class="product">
<img src="{{ product.featured_image.src }}" alt="{{ product.featured_image.alt }}" class="image-border-style" width="500" height="500">
</a>

</li>

{% endfor %}

</ul>

<div id="loadMore">Load more</div>
<div id="showLess">Show less</div>

I use the "{% for product in ... %}" function in order to create list items without having to manually set up all the items and their properties. I had to abandon manual set up to include special Shopify code that changes item properties automatically.

In theme.liquid, I have this javascript:

<script>
$(document).ready(function () {
    // Load the first 5 list items from another HTML file
    //$('#product-container').load('externalList.html li:lt(5)');
    $('#product-container li:lt(5)').show();
    $('#showLess').hide();
    var items =  50;
    var shown =  5;
    $('#loadMore').click(function () {
        $('#showLess').show();
        shown = $('#product-container li:visible').size()+5;
        if(shown< items) {$('#product-container li:lt('+shown+')').show();}
        if(shown= items) {$('#loadMore').hide();}

    });
    $('#showLess').click(function () {
        $('#product-container li').not(':lt(5)').hide();
        $('#loadMore').show();
        $('#showLess').hide();
    });
});
</script>

What I want to do

My current javascript only shows/hides list items, but I want to load them similarly to Facebook newsfeed and Google images - not automatically, but with a "load more" button.

For example, to load the first 5 to 10 list items, and then load 5 to 10 more items after pressing a "Load more" button.

Purpose

The purpose of the function would be to increase initial loading speed of the website by loading only a specific number of list items, and load more on demand.


How can I use ajax to create a "Load more" button to load more list items?

Community
  • 1
  • 1
Erlend K.H.
  • 448
  • 6
  • 21
  • 1
    You can do it with `ajax` call. Search for it. This is an asynchronous call. – vaso123 Apr 28 '15 at 10:23
  • Thanks! Do you know about a code reference that can get me started on the right track? – Erlend K.H. Apr 28 '15 at 10:38
  • Here is the official API documentation: http://api.jquery.com/jquery.ajax/ And some examples: http://www.w3schools.com/ajax/ajax_examples.asp It will take some times, while you understand it. DON'T FORGET, it is asynchronous!!! – vaso123 Apr 28 '15 at 10:51
  • Thank you again, I am reading w3school's tutorial now. – Erlend K.H. Apr 28 '15 at 11:04
  • I am currently reading this line: xmlhttp.open("GET","xmlhttp_info.txt",true); (open(method,url,async)) and the website says: "(URL) The file can be any kind of file, like .txt and .xml, or server scripting files like .asp and .php." But I'm not sure how I can turn a list (ul, li) into a corresponding URL file. My list contains pictures, so putting those into .txt and .xml may not work? – Erlend K.H. Apr 28 '15 at 11:51
  • @ErlendK.H. You could do anything you want, you could create JSON or other data files with the data and urls for the images in it. Or more simply, create a .php file with the html content you want to load, and then simply load it async into your dom – Stefan Wittwer Apr 28 '15 at 12:05
  • And I've made a mistake. I send you these examples. There are more simple the jQuery `$.ajax` calls. Sorry about that: http://www.w3schools.com/jquery/ajax_ajax.asp – vaso123 Apr 28 '15 at 13:20
  • @StefanWittwer I think I have installed php on IIS 7 now (screenshot) [link](https://cdn.shopify.com/s/files/1/0804/2449/files/php_on_iss7.jpg?6057224924087266698) , but I don't know how I can enable it for my shopify website. Currently, php code just gets greyed out like this: [link](https://cdn.shopify.com/s/files/1/0804/2449/files/php_code_gray.jpg?6057224924087266698) . – Erlend K.H. Apr 29 '15 at 17:05
  • Edit: It seems like php code isn't supported on shopify's liquid code, so I guess I'm back to square one. @lolka_bolka Do I need a web host, or a web server on my computer to use ajax calls? I tried the codes from the w3school's tutorials, but they didn't work on my site, but I'm pretty sure that ajax is supported for shopify websites. – Erlend K.H. Apr 29 '15 at 19:03
  • Ajax is a javascript, so you do not need anything extra on your webserver. Ajax is sending a request to an url, and you can use the response data. – vaso123 Apr 30 '15 at 08:35

0 Answers0