Does will pagination work with forms which have method=“POST”?

前端 未结 2 673
挽巷
挽巷 2021-01-28 04:40

I switched my advanced order form to POST from GET as the URI request became too large for browsers to handle. Everything works fine, with the exception of will pagaination. It

2条回答
  •  清酒与你
    2021-01-28 05:08

    The other answer posted is wrong. will_paginate was NOT built to work with post requests.

    Your choices to 'make' will_paginate work with post request include:

    writing some javascript to:

    • preventDefault on click of the will_paginate-generated link

    $(".pagination li a").click(function(e){ e.preventDefault(); ....

    • find the target page via the clicked link.

    (below code is assuming all will_paginate links have a query string...which they do. But note that this particular line of code won't work if you are passing in more params in the controller via the params option for will_paginate..which you shouldn't be in the first place because we are trying to achieve a post request.)

    var tp_id = $(this).attr("href").split('?page=')[1]

    • generate a hidden input with the correct name attribute depending on the clicked will_paginate link.

    $('form').append("< input type='hidden' name='page' value='" + tp_id +"' >")

    • finally post to the controller action of the search form.

    $('form').submit()

    .done. I hope this puts you on the right path.

提交回复
热议问题