Guest User

AJAX file

a guest
Mar 5th, 2023
619
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 1.43 KB | None | 0 0
  1. // AJAX file
  2. $(document).ready(function() {
  3.  
  4.   // ajax запрос на load more
  5.   let currentPage = 1;
  6.   $('#load-more').on('click', function() {
  7.     currentPage++; // Do currentPage + 1, because we want to load the next page
  8.     $(this).removeData('catslug'); // remove value of data-slug from cache
  9.  
  10.     $.ajax({
  11.       type: 'POST',
  12.       url: '/wp-admin/admin-ajax.php',
  13.       dataType: 'json',
  14.       data: {
  15.         action: 'load_more_projects',
  16.         paged: currentPage,
  17.         category: $(this).data('catslug'),
  18.       },
  19.       success: function (res) {
  20.         if(currentPage >= res.max) {
  21.           $('#load-more').hide();
  22.         }
  23.         $('.post-filter').append(res.html);
  24.       }
  25.     });
  26.   });
  27.  
  28.   // ajax запрос на фильтр
  29.   $('.cat-list_item').on('click', function() {
  30.     $('.cat-list_item').removeClass('active');
  31.     $(this).addClass('active');
  32.     $('#load-more').attr('data-catslug', $(this).data('slug')); //write category data to load-more button
  33.     $('#load-more').show(); //Show load more button if hided
  34.    
  35.     $.ajax({
  36.       type: 'POST',
  37.       url: '/wp-admin/admin-ajax.php',
  38.       dataType: 'json',
  39.       data: {
  40.         action: 'filter_projects',
  41.         category: $(this).data('slug'),
  42.       },
  43.       success: function(res) {
  44.         currentPage = 1; //set the current page to 1 after filter button click
  45.         $('.post-filter').html(res.html);
  46.  
  47.       }
  48.     })
  49. });
Advertisement
Add Comment
Please, Sign In to add comment