Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loaded only one page and stop v.3 #362

Closed
XIOLog opened this issue Aug 22, 2019 · 14 comments
Closed

Loaded only one page and stop v.3 #362

XIOLog opened this issue Aug 22, 2019 · 14 comments

Comments

@XIOLog
Copy link

XIOLog commented Aug 22, 2019

Hello.

I use the script v. 3 and Opencart.

I have pagination with products when the page loads normally. Your script works fine and everything looks good.

I also have several categories anchor tags with category_id in "href".

problem

My html code:

<div class="categories">
    <div class="categories__wrap">
        <?php foreach ($categories as $category) : ?>
            <a 
                href="<?php echo $category['category_url']; ?>" 
                class="category__link"
                data-categoryId="<?php echo $category['category_id']; ?>"
             >
                 <?php echo $category['name']; ?>
             </a>
        <?php endforeach; ?>
     </div>
</div>
<div class="products">
    <?php foreach ($products as $product) : ?>
    <div class="product">
    	<div class="product__header"> <img src="<?php echo $product['thumb']; ?>" alt="<?php echo $product['name']; ?>" class="product__image"> </div>
    	<div class="product__content">
    	    <h3 class="product__title">
                <a href="<?php echo $product['href']; ?>" class="product__link"><?php echo $product['name']; ?></a>
            </h3>
    		<div class="product__prices">
    		    <?php if (!$product['special']) : ?>
    		        <div class="product__price">
    			    <?php echo $product['price']; ?> <small class="product__price--small">/ шт.</small>
    			</div>
    		    <?php else : ?>
    			<div class="product__price">
    			    <?php echo $product['price']; ?> <small class="product__price--small">/ шт.</small>
			</div>
    			<div class="product__price product__price--old">
    			    <?php echo $product['special']; ?>
    			</div>
    		    <?php endif; ?>
    		</div>
    		<div class="product__info">
    		    <div class="product__quantity">
    			<?php echo $product['purchased']; ?>
    		    </div>
    		    <div class="product__rating">
    			<?php if ($product['rating']) : ?>
    			    <?php for ($i = 0; $i < 5; $i++) : ?>
    				<?php if ($i < $product['rating']) : ?>
    				    <div class="star star--gold"></div>
    				<?php else : ?>
    				    <div class="star"></div>
    				<?php endif; ?>
    			    <?php endfor; ?>
    			<?php endif; ?>
    		    </div>
    		</div>
    	</div>
    	<div class="product__footer">
    	    <div class="product__seller">Магазин:
    		<a href="<?php echo $product['seller_url']; ?>" class="seller__link">
    		    <?php echo $product['seller_name']; ?>
    		</a>
    	    </div>
    	    <button 
                class="product__button" 
                onclick="cart.add('<?php echo $product['product_id']; ?>', '<?php echo $product['minimum']; ?>');">
                 > 
            </button>
    	</div>
    </div>
    <?php endforeach; ?>
</div>

When I click on the button, the data is sent and I get the products of the desired category. Then I insert this data into the div:

const products = $('.products');

$.get(this.href, {category_id: $(this).attr('data-categoryId')}).done(function(data) {
    const product = $(data).find('.products');
    products.replaceWith(product);
});

All looking good.

But when I scroll down the page bellow, the items I have uploaded are deleted. In their place are loaded products from the second page. And the script stops.

My code is:

let ias = new InfiniteAjaxScroll('.products', {
    item: '.product',
    next: '.next__link',
    pagination: '.pagination',
    logger: true,
    spinner: {
        element: '.loader', // element
        delay: 600, // delay in milliseconds
        show: function(element) {
            $('<img>', {
                src: 'image/loader.gif',
                class: 'loader__image',
            }).appendTo(element);
        },
        hide: function(element) {
            $(element).find('img').remove();
        },
    },
});

Where did I go wrong?

@XIOLog XIOLog changed the title Loaded only one page and stop Loaded only one page and stop v.3 Aug 22, 2019
@fieg
Copy link
Member

fieg commented Aug 23, 2019

Did you also update the pagination element? That’s what IAS is using to load the next page.

@XIOLog
Copy link
Author

XIOLog commented Aug 23, 2019

Yes, I did. I want to send a new url with category_id to the pagination element .

@XIOLog
Copy link
Author

XIOLog commented Aug 23, 2019

And when I clicked on the category button the page counter must be 0

@XIOLog
Copy link
Author

XIOLog commented Aug 23, 2019

I`m confused :-)

@XIOLog
Copy link
Author

XIOLog commented Aug 23, 2019

I generate pagination element in the Opencart controller and send it to the view.

$pagination = new Pagination();
$pagination->total = $productCount;
$pagination->page = $page;
$pagination->limit = $limit;
$pagination->url = $this->url->link('common/home', $url . '&page={page}');
$data['pagination'] = $pagination->render();

@XIOLog
Copy link
Author

XIOLog commented Aug 23, 2019

And got products from category via JQuery $.get

@fieg
Copy link
Member

fieg commented Aug 23, 2019

const products = $('.products');

$.get(this.href, {category_id: $(this).attr('data-categoryId')}).done(function(data) {
    const product = $(data).find('.products');
    products.replaceWith(product);
});

I only see you replacing the products, not the pagination element. If you change the cateogory, the next page url should also be changed, right?

@XIOLog
Copy link
Author

XIOLog commented Aug 23, 2019

Yes, it is. Category_id must be added to the url via &.

@XIOLog
Copy link
Author

XIOLog commented Aug 23, 2019

I take a category_id from data-categoryId in the category anchor tag:

<a href="<?php echo $category['category_url']; ?>"
    class="category__link"
    data-categoryId="<?php echo $category['category_id']; ?>"
>
<?php echo $category['name']; ?>
</a>

And pass it to the $.get

@XIOLog
Copy link
Author

XIOLog commented Aug 23, 2019

And products block has two states:

  • when the category button is pressed and I got category_id
  • when the category button is not pressed and I didn`t get category_id.

In both cases, the script should work.

@XIOLog
Copy link
Author

XIOLog commented Aug 23, 2019

Here is such a task :-)

@XIOLog
Copy link
Author

XIOLog commented Aug 26, 2019

Do you have any idea?

@fieg
Copy link
Member

fieg commented Aug 26, 2019

No sorry, just that you need to update the pagination after the $.get, just like you do with .products.

@XIOLog
Copy link
Author

XIOLog commented Aug 26, 2019

Ok. Thank you for all.

@fieg fieg closed this as completed Sep 4, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants