Project

data_table

0.03
No release in over 3 years
Low commit activity in last 3 years
Simple data preparation from AR/Mongoid to the jQuery DataTables plugin
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.0
~> 2.0
~> 2.11

Runtime

>= 3.0
 Project Readme

Data Table¶ ↑

Makes it easy to ship data to a jQuery DataTable from ActiveRecord or Mongoid.

Quick example:¶ ↑

in your javascript:

$(".providers-data-table").dataTable({"bJQueryUI"       : true,
                                      "bProcessing"     : true,
                                      "bAutoWidth"      : false,
                                      "sPaginationType" : "full_numbers",
                                      "aoColumns"       : [{"sType" : "html"}, null, null, null, null],
                                      "aaSorting"       : [[0, 'asc'], [1, 'asc']],
                                      "bServerSide"     : true,
                                      "sAjaxSource"     : "/providers.json" }).fnSetFilteringDelay();

Note: the fnSetFilteringDelay() call isn’t required but highly recommended: datatables.net/plug-ins/api#fnSetFilteringDelay

in your controller:

class ProvidersController < ApplicationController

  def index
    respond_to do |wants|
      wants.html
      wants.json do
        render(:json => Provider.for_data_table(self, %w(name fein categories.name county state), %w(name fein)) do |provider|
          ["<%= link_to(provider, provider) %>", provider.fein, provider.category.name, provider.county, provider.state]
        end)
      end
    end
  end

end

in your view (assuming HAML):

%table.providers-data-table
  %thead
    %tr
      %th Name
      %th FEIN
      %th Category
      %th County
      %th State

  %tbody

and in your Gemfile

# for ActiveRecord
gem "will_paginate"

# for Mongoid
gem "kaminari"

If using Mongoid add this to your model

include Mongoid::DataTable

Advanced Features¶ ↑

Date fields¶ ↑

In order to handle date fields properly, enclose the field name in an array along with a hash like so:

Provider.for_data_table(self, %w(name fein opened_on), ["name", "fein", ["opened_on", {:date => true}]]) { ... }

Split fields¶ ↑

To handle split fields, that is to handle searching for “x-y”, add a hash with a split key:

Provider.for_data_table(self, %w(name fein suffix), ["name", ["fein", "suffix", {:split => "-"}]]) { ... }

Copyright © 2010-2011 Jason Dew. See LICENSE for details.