In Index view I have table where I'd like to populate ID dynamically. So far I tried this:
id="table_<%= @controller_name %>"
Method in controller:
def get_controller_name
@controller_name = self.class.name.split("::").last
end
Then I want to access particular Table in my Coffeescript. I've done it like this:
$ ->
myvar = '<%= raw @controller_name.to_json %>'
myvarAsObj = JSON.parse(myvar)
$('#' + 'table_' + myvarAsObj).DataTable
however it doesn't seem to be working.
I see in Page Source I got Table ID like this:
id="table_MyController"
How do I access my table ID in Coffeescript correctly, please? Thank you!
Update
Table in Index:
<table data-controller-name="<%= @controller_name %>" cellpadding="0" cellspacing="0"
border="0" class="table table-striped table-bordered table-hover" width="100%"
data-source="<%= campaign_campaigns_index_path(format: :json) %>">
Coffeescript:
$ ->
$('table[data-controller-name]').each ->
$(this).DataTable
ajax: $('table[data-controller-name]').each ->
$(this).data('source')
Page Source:
<table data-controller-name="CampaignsController" cellpadding="0" cellspacing="0" border="0"
class="table table-striped table-bordered table-hover" width="100%"
data-source="/en/campaigns.json">