bogdan/datagrid

Full HTML rendering context for empty state

Closed this issue · 3 comments

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?

Pass self into the no_results_html:

grid.no_results_html(self)

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

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