How do I make a dynamic pagination run the same function it was rendered?

Asked

Viewed 110 times

2

I’m dynamically rendering a view with the pagination defined and to perform this action I use the following function:

$('#form').submit(function (e) {
    e.preventDefault();

    const request = $.ajax({
        url: '/module/controller/action',
        method: 'POST',
        data: $(this).serialize()
    });

    request.done(function (data) {
        $('#dynamic_elements').html(data);
    });
});

I would like paging to perform the same function by sending the page number through the method GET for the same controller which is used for rendering view via POST.

Follow the example of pagination:

<ul class="pagination">
    <li class="active"><a href="">1</a></li>
    <li><a href="">2</a></li>
    <li><a href="">3</a></li>
</ul>
  • Do you load the page by submitting a form? What form would that be and what is its relation to paging?

  • @Andersoncarloswoss, is loaded the view after submitting the form. This form provides parameters that are used to filter the query in the database. Since there can be from 0 to more than 100K of records, my controller creates the page.

1 answer

2


Create a Event Handler for links that are not within the <li> with class .active, firing a Trigger for the event .submit form, passing a parameter with the button number:

$(".pagination a").on("click", function(e){
   e.preventDefault();
   if(!$(this).parent().hasClass("active")) $('#form').trigger("submit", $(this).text().trim());
});

The if above will prevent the link that is inside the li with class .active Fire Trigger. Maybe the active page doesn’t matter if it reloads, but if you want it to be enabled to click as well, just remove the if:

$(".pagination a").on("click", function(e){
   e.preventDefault();
   $('#form').trigger("submit", $(this).text().trim());
});

Note that Trigger sends as parameter for event .submit the text of the link, then you need to put in the event function .submit a second variable to receive this information:

$('#form').submit(function (e, v) {
                               ↑

Now just concatenate in the controller URL the parameter of the page that will be sent via GET:

url: '/module/controller/action'+ (v ? '?pagina='+ v : ''),

The ternary operator will check whether the variable v has some value and concatenate in the controller URL the parameter ?pagina= plus the value in v (in place of pagina you use the name you think best).

In the controller you will receive the value in pagina with $_GET['pagina'];. The form itself you will receive normal with $_POST.


If pagination links are being created on the page after loading the page, change the Event Handler for the form below (delegated form):

$(document).on("click", ".pagination a", function(e){
   e.preventDefault();
   $('#form').trigger("submit", $(this).text().trim());
});
  • 1

    Excellent answer, it worked perfectly! Thank you for taking the time to help us!

  • Sam, let me ask you something. In this case, these Paggings are rendered in a panel bootstrap. In case in my form I select more than one user, are rendered a panel for each user selected. How to limit page request only to panel what was clicked on? The solution is very complex?

  • When there is more than one panel more than one request is made?

  • Only the request I posted. In the view there is a loop where I create the panels for each selected user. The ideal would be a request for each one?

  • Dude, I couldn’t see the question. When you click on the page update only the panel where the page number was clicked?

  • Exact, as more than one panel can be rendered. Only the rendering is done through a single function (posted above). If, for example, three panels are generated and I want to change the page only of the second panel will need another function or I can use the same?

  • See that the result of Ajax goes to the element $('#dynamic_elements'). If you have, for example, 3 panel, you will have 3 $('#dynamic_elements')? If so, it is incorrect because it cannot repeat id’s, if I understand.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.