lib/ajax-datatables-rails/alt-api/datatable.rb
# Use this in the `columns` option in the JS initialization of Datatable.
# Call this method in the view.
# There are times that when conditionally showing columns, the column
# conditionals need access to the datatable instance.
# Example:
#   <table data-datatable-columns="<%= UsersDatatable.js_columns %>">
#     <th>Name</th>
#     <th>Email</th>
#   </table>
#   <script>
#     var table = $('#my-datatable');
#     table.DataTable({
#       columns: JSON.parse(table.data('datatable-columns'))
#     })
#   </script>
Use this in the `columns` option in the JS initialization of Datatable.
Call this method in the view.
There are times that when conditionally showing columns, the column
conditionals need access to the datatable instance.
Example:

Name
Email


Use this in the ` columns ` option in the JS initialization of Datatable . Call this method in the view . There are times that when conditionally showing columns the column conditionals need access to the datatable instance . Example : Name Email
def js_columns(only: [], exclude: [])
          cols = if only.present?
                   columns.select { |c| only.include?(c.attr_name) }
                 elsif exclude.present?
                   columns.reject { |c| exclude.include?(c.attr_name) }
                 else
                   columns
                 end
          cols.reject! { |c| c.attr_name.to_s.starts_with?('search_only') }
          cols.map(&:as_json).compact.to_json
        end
def js_columns ( only : [ ] , exclude : [ ] ) cols = if only . present? columns . select { | c | only . include? ( c . attr_name ) } elsif exclude . present? columns . reject { | c | exclude . include? ( c . attr_name ) } else columns end cols . reject! { | c | c . attr_name . to_s . starts_with? ( 'search_only' ) } cols . map ( & :as_json ) . compact . to_json end
Use this in the `columns` option in the JS initialization of Datatable.
Use this in the ` columns ` option in the JS initialization of Datatable .
only
exclude