No commit activity in last 3 years
No release in over 3 years
Simple data preparation from 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.3
>= 0
~> 0.10
~> 0.10.0
~> 2.1
>= 0
~> 2.8
~> 0.9.0.rc

Runtime

~> 0.13.0
>= 3.0.0
 Project Readme

Mongoid: Data Table

Build Status

Dependency Status

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

Quick example:

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: http://datatables.net/plug-ins/api#fnSetFilteringDelay

Model

class Provider

  include Mongoid::Documenta
  include Mongoid::DataTable

  ## fields ##
  field :name
  field :fein
  field :country
  field :state

  ## associations ##
  referenced_in :category

  ## data_table ##
  data_table_options.merge!({
    :fields => %w(name fein category country state),
    :searchable => %w(name fein),
    :dataset => lambda do |provider|
      {
        0 => "<%= link_to(provider.name, provider) %>",
        1 => provider.fein,
        2 => provider.category.name,
        3 => provider.country,
        4 => provider.state,
        :DT_RowId => provider._id
      }
    end
  })

end

Controller (InheritedResources::Base)

Recommended: https://github.com/josevalim/inherited_resources

class PrividersController < InheritedResources::Base

  respond_to :json, :only => :index

  protected

  def collection
    @providers ||= end_of_association_chain.to_data_table(self)
  end

end

Controller (ActionController::Base)

class ProvidersController < ApplicationController

  def index
    respond_to do |format|
      format.html
      format.json do
        render :json => Provider.to_data_table(self)
      end
    end
  end

end

View (HAML)

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

  %tbody

Patches welcome, enjoy!

Copyright

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