Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a few additional datatables options #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,19 @@ Activate using <%= datatable() %>, passing in the columns, how to filter them (s
:no_records_message - Message to display if no records are found, whether on load or after searching
:auto_width - Automatically adjust the width of the columns. Defaults to true.
:row_callback - a function to run on each row in the table. Inserted in to "'fnRowCallback': function( nRow, aData, iDisplayIndex ) { }". See [documentation for fnRowCallback](http://www.datatables.net/usage/callbacks) for more information.
:jquery_ui - boolean, set true if you want to enable jQueryUI for this datatable
:dom - string, configuration for layout of additional table controls (see datatables documentation for sDom)

#### Column Options

:class - string, the class to assign to the table cell. Default is none.
:type - string, the type of content in the column, for non-Ajax tables. 'html' will strip all HTML and sort on the inner value, as a string. Default is string.
:sortable - boolean, allow this column to be sorted on. Default is true.
:searchable - boolean, allow this column to be searched, for non-Ajax tables. Default is true.

:datasort - integer, use the indicated (0-indexed) column for sorting on this column
:visible - boolean, set false to hide this column
:sorting - array, controls the default sorting direction, and even alter the behaviour of the sort handler (i.e. only allow ascending sorting etc) using this parameter.

#### AJAX Options

When you're working with large datasets it's not reasonable to load everything on page load. Use an :ajax_source to load just the records that are being displayed, do custom searching (DB, Solr, etc).
Expand Down
12 changes: 9 additions & 3 deletions lib/rails_datatables.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ def datatable(columns, opts={})

ajax_source = opts[:ajax_source] || nil
server_side = opts[:ajax_source].present?

jquery_ui = opts[:jquery_ui] || nil
dom = opts[:dom] || nil

additional_data_string = ""
additional_data.each_pair do |name,value|
additional_data_string = additional_data_string + ", " if !additional_data_string.blank? && value
Expand All @@ -40,8 +42,10 @@ def datatable(columns, opts={})
"bStateSave": #{persist_state},
"bFilter": #{search},
"bAutoWidth": #{auto_width},
#{"'sDom': [#{dom}]," if dom}
#{"'aaSorting': [#{sort_by}]," if sort_by}
#{"'sAjaxSource': '#{ajax_source}'," if ajax_source}
#{"'bJQueryUI': #{jquery_ui}," if jquery_ui}
"aoColumns": [
#{formatted_columns(columns)}
],
Expand All @@ -68,11 +72,13 @@ def formatted_columns(columns)
else
searchable = c[:searchable].to_s.present? ? c[:searchable].to_s : "true"
sortable = c[:sortable].to_s.present? ? c[:sortable].to_s : "true"

"{
'sType': '#{c[:type] || "string"}',
'bSortable':#{sortable},
'bSearchable':#{searchable}
'bSearchable':#{searchable},
#{",'asSorting':#{c[:sorting].inspect}" if c[:sorting]}
#{",'bVisible':'#{c[:visible]}'" if c[:visible]}
#{",'iDataSort':#{c[:datasort]}" if c[:datasort]}
#{",'sClass':'#{c[:class]}'" if c[:class]}
}"
end
Expand Down