Coder Social home page Coder Social logo

konung / datatables Goto Github PK

View Code? Open in Web Editor NEW

This project forked from caseproof/datatables

1.0 2.0 0.0 101 KB

Datatables is a Rails 3 plugin that enables the easy creation of dynamic datatable views on top of any ActiveRecord model

Home Page: http://blairwilliams.com/ruby-on-rails/datatables

License: MIT License

datatables's Introduction

Datatables

Datatables is a Rails 3 plugin that enables the easy creation of dynamic datatable views on top of any ActiveRecord model. Datatables provides a simple helper that can be utilized in the view to automatically display a dynamic view on top of a model. Datatables handles the entire front end and backend support to do this.

Installation

To install datatables to your rails project, follow these simple steps:

1. Make sure your project is using the ‘jquery-rails’ plugin.

2. Add the following line to your Gemfile:

gem 'datatables', :git => 'git://github.com/Caseproof/datatables.git'

3. Run bundle install

4. Run rails generate datatables:install

5. Add the following lines to your layout file after javascript_include_tag :defaults

<%= stylesheet_link_tag "datatable_page" %>
<%= stylesheet_link_tag "datatable_table" %>
<%= javascript_include_tag 'jquery.dataTables.min' %>

6. Add the Datatable helper mixin to your ApplicationHelper (app/helpers/application_helper.rb) like so:

module ApplicationHelper
  include Datatables::Helpers
end

Example

With datatables it’s easy to add rich datatables to your views that correspond with your active record models.

There is a lovely helper that you can use to render your datatable that takes in a whole host of options. Unfortunately, there’s still quite a bit of work to do with these options to handle every scenario but here’s what it support so far:

Database table:

The following examples will use the following database table:

mysql> desc companies;
+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| id          | int(11)      | NO   | PRI | NULL    | auto_increment |
| name        | varchar(255) | YES  | MUL | NULL    |                |
| slug        | varchar(255) | YES  | MUL | NULL    |                |
| domain      | varchar(255) | YES  | MUL | NULL    |                |
| category_id | int(11)      | NO   | MUL | NULL    |                |
| created_at  | datetime     | YES  |     | NULL    |                |
| updated_at  | datetime     | YES  |     | NULL    |                |
+-------------+--------------+------+-----+---------+----------------+

Standard datatable

The first argument to the datatable helper is the name of the model or an array of model & scope. The second argument is a hash of column names & options.

<%= datatable('company', { :name => { :type => 'string', 
                                      :label => 'Name',
                                      :width => '20%' },
                           :slug => { :type => 'string',
                                      :label => 'Slug',
                                      :width => '15%' },
                           :domain => { :type => 'string',
                                        :label => 'Domain',
                                        :width => '15%' }
                         }) %>

Datatable with a link to edit

Note that the edit link path utilizes the :id field in the database table to automatically insert the correct id on a row by row basis:

<%= datatable('company', { :id => { :type => 'hidden' },
                           :name => { :type => 'link', 
                                          :label => 'Name', 
                                          :link => edit_admin_company_path(:id),
                                          :replace => 'id',
                                          :width => '50%' },
                           :slug => { :type => 'string',
                                      :label => 'Slug',
                                      :width => '25%' },
                           :domain => { :type => 'string',
                                        :label => 'Domain',
                                        :width => '25%' }
                         }) %>

Scoped datatable (now we’re using the ‘dot_org_domains’ that can be found in the Company model):

<%= datatable(['company','dot_org_domains'], { :id => { :type => 'hidden' },
                                               :name => { :type => 'link', 
                                                              :label => 'Name', 
                                                              :link => edit_admin_company_path(:id),
                                                              :replace => 'id',
                                                              :width => '50%' },
                                               :slug => { :type => 'string',
                                                          :label => 'Slug',
                                                          :width => '25%' },
                                               :domain => { :type => 'string',
                                                            :label => 'Domain',
                                                            :width => '25%' }
                                             }) %>

Association columns

<%= datatable(['company','dot_org_domains'], { :id => { :type => 'hidden' },
                                               :name => { :type => 'link', 
                                                              :label => 'Name', 
                                                              :link => edit_admin_company_path(:id),
                                                              :replace => 'id',
                                                              :width => '40%' },
                                               :slug => { :type => 'string',
                                                          :label => 'Slug',
                                                          :width => '20%' },
                                               :domain => { :type => 'string',
                                                            :label => 'Domain',
                                                            :width => '20%' },
                                               :category_name => { :type => 'string',
                                                                   :label => 'Category',
                                                                   :column => 'categories.name',
                                                                   :width => '20%',
                                                                   :class => 'center' }

                                             }) %>

Why use this plugin?

It is the easiest way to integrate dynamic datatables using the jQuery datatables plugin into your rails app.

Version history

  • Version 1.0.0

    • First Release

Contribute

If you’ve got some ideas for datatables let me know and, even better, if you’ve made some enhancements to the code send me a pull request!

Support

Bug report? Faulty/incomplete documentation? Feature request? Please post an issue on ‘github.com/Caseproof/metafy/issues’. If its urgent, please contact me from my website at ‘blairwilliams.com/contact

Copyright © 2004-2011 Caseproof, LLC, released under the MIT license

datatables's People

Contributors

supercleanse avatar

Stargazers

Nick Gorbikoff avatar

Watchers

Nick Gorbikoff avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.