Giter VIP home page Giter VIP logo

simple_table_for's Introduction

Gem Version Build Status Code Climate Test Coverage

Simple Table For helper

Generate HTML tables in a simple and clean way.

Installation

gem 'simple_table_for'

Usage

<%= table_for @users, [:name, :email, 'Registration date', 'Comments count', '-', '-'] do |user| %>
  <%= field user.name %>
  <%= field user.email %>
  <%= field l(user.created_at, format: '%d/%m/%Y') %>
  <%= field user.comments.count %>
  <%= field link_to('View', user) %>
  <%= field link_to('View', edit_users_path(user)) %>
<% end %>

The above will generate a table like:

First name E-mail Registration date Comments count - -
John [email protected] 01/01/2015 15 View Edit
Mark [email protected] 02/02/2015 34 View Edit

The second parameter is a array of headers. For a Symbol header, the helper will get the localizated name in config/locales folder. Example: for a @users collection and :name header it will search for '/activerecord/attributes/user/name'. For a String header, the helper will just print it as is.

You can optionally give adicional options, like id or classes to tables and fields:

<%= table_for @users, [:name, '-'], id: 'users-table', class: 'table' do |user| %>
  <%= field user.name, class: 'user-name' %>
  <%= field link_to('View', user), class: 'user-link' %>
<% end %>

table_for is aliased as simple_table_for, and field is aliased as column:

<%= simple_table_for @products, [:product_name, :price, 'Number of orders'] do |product| %>
  <%= column product.product_name %>
  <%= column product.price %>
  <%= column product.orders.count %>
<% end %>

Setting default options

You can also set a default class for tables:

# application.rb
class Application < Rails:Application
  # for a Bootstrap table
  config.simple_table_for.defaults = {
    class: 'table table-condensed table-striped table-bordered'
  }
end

This project uses MIT-LICENSE.

Contributing

  1. Fork it ( https://github.com/andreynering/simple_table_for/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

simple_table_for's People

Contributors

andreynering avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  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.