2021年5月7日星期五

How to change SPA page number onclick? JavaScript button

I have next button how can I change the page number when I click on the button?

function pagination_build(){          const pagination_buttons = document.createElement('div');          const next_button = document.createElement('button');          next_button.innerHTML += 'Next'          pagination_buttons.appendChild(next_button);          pagination_buttons.classList.add('pagination_buttons');          document.querySelector('#show-posts').appendChild(pagination_buttons);          next_button.addEventListener("click", () => {                            load_posts();          });  }    function load_posts(){    document.querySelector('#page-view').style.display = 'none';    document.querySelector('#load-profile').style.display = 'none';    document.querySelector('#posts-view').style.display = 'block';    document.querySelector('#show-posts').style.display = 'block';    document.querySelector('#post-form').onsubmit = function() {          compose_post();      }      fetch('/posts/all_posts') // url with that API      .then(response => response.json())      .then(all_posts => {          // Loop and show all the posts.          console.log(all_posts.post_count)          if(all_posts.post_count > 3){              pagination_build();          }          all_posts.posts.forEach(function(post) { // loop to loop over each object              build_post(post)          });      });  document.querySelector('#show-posts').innerHTML = ""  }  

views.py

def show_posts(request):      all_posts = NewPost.objects.all()      all_posts = all_posts.order_by("-date_added").all()      all_posts_count = all_posts.count()      paginator = Paginator(all_posts, 3)      page_number = request.GET.get('page', 1)      page_obj = paginator.get_page(page_number)      return JsonResponse({          "post_count": all_posts.count(),          "posts": [website_post.serialize() for website_post in page_obj]       }, safe=False)  

My backend handles which posts to load depends on the current page number by (request.get) but my button is in JavaScript and I need to change the page number on click

https://stackoverflow.com/questions/67443138/how-to-change-spa-page-number-onclick-javascript-button May 08, 2021 at 08:57AM

没有评论:

发表评论