# Snaptable
A gem that generate HTML tables from your models in order to display them in your admin views. It supports pagination, sorting and searching. It is also possible to customize the tables.
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'snaptable'
```
And then execute:
$ bundle
Or install it yourself as:
$ gem install snaptable
Include the assets:
```css
# application.css
/*
*= require_self
*= require_tree .
*= require snaptable
*/
```
```js
# application.js
//= require_tree .
//= require snaptable
```
## Usage
### Basic table
In your controller, instantiate a new `Table` with minimum two arguments: the controller itself and the model to use.
```ruby
def index
@table = Table.new(self, Article)
end
```
Then, in order to enable sorting, call the method `respond` on the table.
```ruby
def index
@table = Table.new(self, Article)
@table.respond
end
```
Finally, in your view, generate the table where you wish.
```html
<%= @table.present %>
```
The elements in the table are clickable. Click on an element and use the links above the table to edit or destroy it. If you double-click, you are directly redirect to the edit page. Furthermore, the columns are sortable. Click on a label to sort the data by a column.
### Options
You can customize the table when you instantiate it. Pass you own collection in the third argument.
```ruby
@articles = Article.last(3)
Table.new(self, Article, @articles)
```
Pass the options in the fourth argument. Here is a list:
* buttons [true]: enable the buttons above the table to add, edit or destroy an element.
* search [false]: enable searching. Add a search field above the table.
```ruby
Table.new(self, Article, nil, { search: true, buttons: false })
```
You can also configure the table in the view. The `present` method takes a single named argument to let you add a custom buttons bar. Pass the name of a partial to the parameter `buttons` and the content will be added above the table.
```erb
<%= @table.present(buttons: "my_custom_partial") %>
```
To help you build your custom buttons header, you are provided two helper methods for each of the possible actions (add, show, edit, delete):
* `#{action}_button` returns the HTML code to display the button for the desired action, e.g `add_button`.
* `#{action}_button?` returns if the user is allowed to see the desired button, e.g. `delete_button?`.
```erb