Hotwire Discussion

Pagination with Stimulus

I’m trying to migrate to Stimulus and I thought a good place to start would be to move all my UJS style partials that are using jQuery to Stimulus. It would be great to use one Stimulus controller for all the remote data partial rendering I do using link_to.

Thanks to a previous post, I’ve been successful with building a Stimulus controller and rendering a partial with using a data target. What I can’t figure out is how to get my pagy pagination partial or nav to render with the link_to rendered partial.

show.html.erb

<div data-controller="renderer">
  <%= link_to visits_physician_path(@physician), data: { remote: true, action: 'ajax:success->renderer#render' } do %>
    Visits
  <% end %>
  <div data-renderer-target="display"></div>


  <div id="js-physician-pagination" class="pagination-button">
   // Where I need to render the pagination nav. 
   // Either using render nav or <%== pagy_bootstrap_nav(@pagy) %>
  </div>
</div>

controller

  def visits
    authorize(@physician)

    @pagy, @visits = pagy(Visit.where(physician: @physician.id)
                               .order(check_in: :desc)
                               .includes(:office, :user),
                          items: 5, , link_extra: 'data-remote="true"')

    respond_to do |format|
      format.html { render(partial: 'physicians/show/visit', collection: @visits) }
    end

    authorize(@visits)
  end

stimulus controller

import {Controller} from 'stimulus'

export default class extends Controller {
    static targets = ['display']

    render(evt) {
        this.displayTarget.innerHTML = evt.detail[0].body.innerHTML
    }
}

visits.js.erb (what was previously being used)

$("#js-physician-container").html("<%= j render(partial: 'physicians/show/visit', collection: @visits) %>");
$("#js-physician-pagination").html("<%== j(pagy_bootstrap_nav(@pagy)) %>");

Any help on this would be great.

Brad