% columns = columns.map{ |column| column.symbolize_keys! } %>
<% if !cookies["#{table_name}_table"].blank? %>
<% json = JSON.parse(cookies["#{table_name}_table"]) %>
<% if !json['columns'].blank? %>
<% columns.each_with_index do |column, key| %>
<% column[:visible] = json['columns'].include?(column[:data].to_s) ? true : false %>
<% end %>
<% end %>
<% if !json['view'].blank? %>
<% default_view = json['view'] %>
<% end %>
<% sort = json['sort'].blank? ? sort : json['sort'] %>
<% initial_size = json['table_size'].blank? ? initial_size : json['table_size'] %>
<% ordering = json['ordering'].blank? ? [] : json['ordering'] %>
<% end %>
<% if !ordering.blank? %>
<% columns = columns.sort_by{ |v| ordering.index(v[:data]).blank? ? 9999 : ordering.index(v[:data])} %>
<% end %>
<% mapping = columns.each_with_index.map {|v,i| [v[:data],i]}.to_h %>
<% sort = sort.map{|col,dir| [mapping[col].blank? ? 1 : mapping[col] ,dir]} %>
<% debug = !defined?(debug) ? false : debug %>
<% content_for :scripts do %>
columns = <%= raw(columns.to_json) %>;
$.each(columns, function(key, column) {
$.each(column, function(key, value) {
if(key=='render'){columns[0][key] = new Function('data', 'type', 'row', value);};
});
});
var <%= table_name %> = $('#<%= "#{table_name}_content" %>').find('table.datatable').dataTable({
"sDom": "t<'row'<'grid_view col-lg-12'>><'table_footer'i<'<%="\##{table_name}_select_container" %>.select_container'>p>",
"pagingType": "full_numbers",
"language" : {
"paginate" : {
"previous" : "",
"next" : ""
}
},
"bProcessing": true,
"bServerSide": true,
"colReorder": true,
"select": false, //{ style: 'multi'},
"columns": [].concat(columns),
"aaSorting": <%= raw(sort.to_json) %>,
"sAjaxSource": $('#<%= "#{table_name}_content" %>').find('table.datatable').data('source'),
"iDisplayLength": <%= initial_size %> ,
"fnServerParams": function ( aoData ) {
aoData.push(
{ "name": "table_view_mode", "value": $('#<%= "#{table_name}_content" %>').find('input[type=radio][name="<%= table_name %>[view_mode]"]:checked').val()},
{ "name": "sSearch", "value": $('#<%= "#{table_name}_content" %>').find('input[name="table[search]"]').val()}
);
<% if !aoData.blank? %>
<% aoData.each do |data| %>
aoData.push(<%= raw(data.to_json) %>);
<% end %>
<% end %>
<% if defined?(table_filters) %>
<% table_filters.each do |table_filter| %>
<% if table_filter["type"] == "checkbox" %>
aoData.push({ "name": "<%= table_filter["name"].parameterize('_') %>", "value": $('#<%= "#{table_name}_content" %>').find('input[name="<%= table_filter["name"].parameterize('_') %>[]"]:checked').map(function(_, el) { return $(el).val()}).get() });
<% elsif table_filter["type"] == "boolean" %>
aoData.push({ "name": "<%= table_filter["name"].parameterize('_') %>", "value": $('#<%= "#{table_name}_content" %>').find('input[name="<%= table_filter["name"].parameterize('_') %>"]:checked').val() });
<% elsif table_filter["type"] == "select" %>
aoData.push({ "name": "<%= table_filter["name"].parameterize('_') %>", "value": $('#<%= "#{table_name}_content" %>').find('select[name="<%= table_filter["name"].parameterize('_') %>"]').val() });
<% end %>
<% end %>
<% end %>
},
"fnDrawCallback": function( oSettings ) {
$('#<%= "#{table_name}_content" %>').find('.table [rel="status_popover"], .table[data-rel="status_popover"]').popover({ placement: "left", html: 'true', container: '.table'});
$('#<%= "#{table_name}_content" %>').find('.table [data-toggle="tooltip"]').tooltip();
view_mode = $('#<%= "#{table_name}_content" %>').find('input[type=radio][name="<%= table_name %>[view_mode]"]:checked').val();
$('#<%= "#{table_name}_content" %>').find('.grid_view').addClass('hidden');
$('#<%= "#{table_name}_content" %>').find('table.datatable').addClass("hidden");
if(view_mode == 'grid'){
$('#<%= "#{table_name}_content .grid_view" %>').html('');
$.each(oSettings.aoData, function(index, result) {
$('#<%= "#{table_name}_content .grid_view" %>').append(result._aData.grid_view);
});
if (oSettings.aoData.length == 0) {
$(".login_grid").append("" + oSettings.oLanguage.sEmptyTable + "
")
}
$('#<%= "#{table_name}_content" %>').find('.grid_view').removeClass('hidden')
}else if(view_mode == 'table'){
$('#<%= "#{table_name}_content" %>').find('table.datatable').removeClass("hidden");
}
<%= table_name %>.find('input[type="checkbox"].select_row').on('click', function(e){
if($('#<%= "#{table_name}_select_all" %>').is(':checked')){ $('#<%= "#{table_name}_select_all" %>').prop("checked", false);}
});
if( <%= table_name %>.find('tr[processing="true"]').length > 0 && <%= table_name %>.is(":visible")){
setTimeout(function(){
<%= table_name %>.api().draw(false);
}, 1000);
}
if(<%= table_name %>.find('tr[processing="true"]').length > 0 && $('#<%= table_name %>_wrapper .grid_view').is(":visible")){
setTimeout(function(){
<%= table_name %>.api().draw(false);
}, 1000);
}
},
"fnServerData": function ( sSource, aoData, fnCallback, oSettings ) {
data = aoData;
if ( data && $.isArray(data) ) {
var tmp = {};var rbracket = /(.*?)\[\]$/;
$.each( data, function (key, val) {
var match = val.name.match(rbracket);
if ( match ) {var name = match[0];if ( ! tmp[ name ] ) {tmp[ name ] = [];}tmp[ name ].push( val.value );}else {tmp[val.name] = val.value;}
});
data = tmp;
}
<% if debug %>console.log(fnCallback); console.log(oSettings);console.log(data);<% end %>
oSettings.jqXHR = $.ajax({"dataType": 'json', "url": sSource, "data": data, "success": fnCallback,"error": function (textStatus, errorThrown) {
if(textStatus.status == 400 || textStatus.status == 302 ){
window.location.replace("/");
}else if(textStatus.status == 500 ){
alert('<%= "Table Server Error: #{table_name} Contact Support" %>');
}else{
window.location.replace("/");
};
}});
}
}).on( 'draw.dt', function () {
<% if debug %> console.log('Drawing Trigger <%= table_name %>'); <% end %>
<%= table_name + '_cookie' %>();
}).on( 'processing.dt', function ( e, settings, processing ) {
if(processing){
$('#<%= "#{table_name}_content" %>').find('.table_refresh i').addClass('fa-spin');
}else{
$('#<%= "#{table_name}_content" %>').find('.table_refresh i').removeClass('fa-spin');
}
});
function <%= table_name + '_cookie' %>(){
<% if debug %> console.log('Save Cookie <%= table_name %>'); <% end %>
columns = $('#<%= "#{table_name}_content" %>').find('input[name="<%= table_name %>[table_column_vis][]"]:checked').map(function(_, el) { return $(el).data('key')}).toArray();
view = $('#<%= "#{table_name}_content" %>').find('input[type=radio][name="<%= table_name %>[view_mode]"]:checked').val();
sort = <%= table_name %>.fnSettings().aaSorting;
table_size = <%= table_name %>.fnSettings()._iDisplayLength;
sort = sort.map(function(value,key) {
column = <%= table_name %>.api().column(value[0]).dataSrc();
return [column,value[1]]
})
var orderedItems = [];
$.each(<%= table_name %>.api().colReorder.order(), function(value) {
data_key = <%= table_name %>.api().column(value).dataSrc();
if (data_key != null && data_key.length > 0 ){orderedItems.push(data_key);}
});
if (Cookies.get('<%= "#{table_name}_table" %>') != null){
data = $.parseJSON( Cookies.get('<%= "#{table_name}_table" %>'));
data['columns'] = columns;
data['view'] = view;
data['sort'] = sort;
data['table_size'] = table_size;
data['ordering'] = orderedItems;
}else{
data = {'columns' : columns, 'view' : view, 'sort' : sort, 'table_size' : table_size, 'orderedItems' : orderedItems};
}
Cookies.set('<%= "#{table_name}_table" %>', data);
}
function <%= table_name + '_column_vis' %>(sorting){
$('#<%= "#{table_name}_content" %>').find('input[name="<%= table_name %>[table_column_vis][]"]').on("change", function(e) {
index = <%= table_name %>.api().colReorder.transpose(parseInt($(this).val()),'toCurrent');
column = <%= table_name %>.api().column(index);
column.visible($(this).is(':checked'));
<%= table_name + '_cookie' %>();
});
if(sorting){
$('#<%= "#{table_name}_content" %>').find('.table_visibility').sortable({axis: 'y'}).on("sortstop", function( event, ui ) {
<% if debug %> console.log('Sort Stop Trigger <%= table_name %>'); <% end %>
items = $('#<%= "#{table_name}_content" %>').find('.table_visibility li input');
var orderedItems = [];
$.each(items, function(index, value) {
index = <%= table_name %>.api().colReorder.transpose(parseInt($(value).val()),'toCurrent');
column = <%= table_name %>.api().column(index);
if (column != null && column.length > 0 ){orderedItems.push(column.index());}
});
<%= table_name %>.api().colReorder.order(orderedItems);
});
$('#<%= "#{table_name}_content" %>').find('.table_visibility').disableSelection();
}
}
<%= table_name + '_column_vis' %>(true);
//Datable Select
function <%= table_name + '_row_select' %>(select, data){
var count = <%= table_name %>.api().rows({ selected: true }).count();
if(count > 0){
$('#<%= "#{table_name}_content" %>').find('.group_actions').removeClass('hidden');
}else {
$('#<%= "#{table_name}_content" %>').find('.group_actions').addClass('hidden');
}
if(select){
<% if debug %>
console.log('Selected');
console.log(data);
<% end %>
}else{
<% if debug %>
console.log('Deselected');
console.log(data);
<% end %>
}
}
<%= table_name %>.api().on('select', function ( e, dt, type, indexes ) {
<%= table_name + '_row_select' %>(true, <%= table_name %>.api().rows( indexes ).data());
});
<%= table_name %>.api().on('deselect', function ( e, dt, type, indexes ) {
<%= table_name + '_row_select' %>(false, <%= table_name %>.api().rows( indexes ).data());
});
//Datatable header reorder
<%= table_name %>.api().on('column-reorder', function ( e, settings, details ) {
<% if debug %> console.log('Ordering Trigger <%= table_name %>'); <% end %>
var orderedItems = [];
$.each(<%= table_name %>.api().colReorder.order(), function(value) {
data_key = <%= table_name %>.api().column(value).dataSrc();
item = $('#<%= "#{table_name}_content" %>').find('.table_visibility li input[data-key="' + data_key + '"]').parents('li');
if (item != null && item.length > 0 ){orderedItems.push(item);}
});
<% if debug %> console.log('Ordering Trigger Rebuild DropDown <%= table_name %>'); <% end %>
$('#<%= "#{table_name}_content" %>').find('.table_visibility').empty().html(orderedItems);
<%= table_name + '_column_vis' %>(false);
<%= table_name + '_cookie' %>();
});
$('#<%= "#{table_name}_select_all" %>').on('click', function(e){
if($(this).is(':checked')){<%= table_name %>.find('input[type="checkbox"].select_row').prop("checked", true);}else{<%= table_name %>.find('input[type="checkbox"].select_row').prop("checked", false);}
});
$('#<%= "#{table_name}_content" %>').find('.table_refresh').click(function(e){<%= table_name %>.api().draw(false)});
// Add the row # select to the footer
const <%= "#{table_name}_select" %> = $('