It took me a while to figure out how to put together a hobo datatable with 
server side processing. It's based on railscasts #340. Just want to share 
it here.

1. Install hobo datatables per 
instruction http://hobocentral.net/api_plugins/hobo_data_tables

2. My model looks like this

class Product < ActiveRecord::Base

  hobo_model # Don't put anything above this

  fields do
    name       :string
    body       :text
    timestamps
  end
....

3. add below to views/products/index.dryml

<index-page>
<collection: replace>
<data-table bJqueryUI fields="this,body,updated_at" 
data-rapid='{"data-table":{"options":{"sPaginationType":"full_numbers","bProcessing":true,"bServerSide":true,"sAjaxSource":""},"event":{}}}'
 
/>
</collection:>
    <bottom-page-nav: replace /> 
</index-page>

** Not 100% sure whether there is a way to specify options without 
overriding data-rapid, but this works.
** <bottom-page-nav: replace /> is used to remove the bottom-page-nav

4. modify the controller to 

class ProductsController < ApplicationController

  hobo_model_controller

  auto_actions :all
  
  def index
    hobo_index do 
      respond_to do |format|
        format.html
        format.json { render json: ProductsDatatable.new(view_context) }
      end
    end
  end
end

5. Add a products_datatable.rb to app/datatables/ to create the json 
response

class ProductsDatatable
  delegate :params, :h, :link_to, to: :@view

  def initialize(view)
    @view = view
  end

  def as_json(options = {})
    {
      sEcho: params[:sEcho].to_i,
      iTotalRecords: Product.count,
      iTotalDisplayRecords: products.total_entries,
      aaData: data
    }
  end

private

  def data
    products.map do |product|
      [
        link_to(product.name, product),
        h(product.body),
        h(product.updated_at.strftime("%B %e, %Y")),
      ]
    end
  end

  def products
    @products ||= fetch_products
  end

  def fetch_products
    products = Product.order("#{sort_column} #{sort_direction}")
    products = products.page(page).per_page(per_page)
    if params[:sSearch].present?
      products = products.where("name like :search or body like :search", 
search: "%#{params[:sSearch]}%")
    end
    products
  end

  def page
    params[:iDisplayStart].to_i/per_page + 1
  end

  def per_page
    params[:iDisplayLength].to_i > 0 ? params[:iDisplayLength].to_i : 10
  end

  def sort_column
    columns = %w[name body updated_at]
    columns[params[:iSortCol_0].to_i]
  end

  def sort_direction
    params[:sSortDir_0] == "desc" ? "desc" : "asc"
  end
end

That's it. Let me know if you have any questions. Enjoy.

-- 
You received this message because you are subscribed to the Google Groups "Hobo 
Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/hobousers.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to