bogdan / datagrid

Gem to create tables grids with sortable columns and filters
MIT License
1.02k stars 115 forks source link

Full HTML rendering context for empty state #315

Closed gap777 closed 1 year ago

gap777 commented 1 year ago

By default, it seems that the table empty state must be a simple translated string:

/ _body.html.slim
- else
    tr
      td class="noresults" colspan="#{grid.html_columns.length}"  
        = I18n.t('datagrid.no_results')

We'd like to use richer HTML for the empty state, composed of images, links, etc, and generated by each table.

/ _body.html.slim
- else
    tr
      td class="noresults" colspan="#{grid.html_columns.length}"  
        = grid.no_results_html

However, the no_results_html isn't being called from a view context (so we don't have access to helpers like link_to).

How can we do this?

bogdan commented 1 year ago

Pass self into the no_results_html:

grid.no_results_html(self)
gap777 commented 1 year ago

For posterity, I'll note that this appears to be something like a view context, and on it are all the helper methods I would expect and hope for:

def no_results_html(view_context)
    view_context.link_to ....
end
bogdan commented 1 year ago

Yes, it is. I just found there is another way:

class Grid
  include  Rails.application.routes.url_helper 
  def no_results_html
      helpers = ApplicationController.helpers
      helpers.content_tag(:div, "No Results") + helpers.link_to("Back", root_path) 
  end
end

Up to you....

https://stackoverflow.com/questions/4112050/how-to-use-all-view-and-helper-methods-inside-of-rails-console-for-rails-2-x-and https://stackoverflow.com/questions/2846247/rails-check-output-of-path-helper-from-console