GithubHelp home page GithubHelp logo

isabella232 / slickgrid-rails Goto Github PK

View Code? Open in Web Editor NEW

This project forked from liquidm/slickgrid-rails

0.0 0.0 0.0 242 KB

SlickGrid integration for Rails 3.1+

License: MIT License

Ruby 97.74% Shell 2.26%

slickgrid-rails's Introduction

SlickGrid::Rails

slickgrid-rails is a simple gem to add SlickGrid vendor files to the Rails asset pipeline. It also provides a simple SlickGrid::Table class to render JSON output suitable for the Grid/DataView.

Installation

Add this line to your application's Gemfile:

gem 'slickgrid-rails'

And then execute:

$ bundle

Or install it yourself as:

$ gem install slickgrid-rails

Usage

To use the basic grid functionality you just need to include the slick.js file in your Sprocket manifest:

//= require slick

The above example will include all required jQuery dependencies and core.js/grid.js from SlickGrid. To use one of the more advanced example modules simply add them to your manifest:

//= require slick/dataview
//= require slick/editors
//= require slick/formatters

Rails Table Helper

A simple table helper is included to generate SlickGrid compatible JSON output:

require "slickgrid/table"

class UsersTable < SlickGrid::Table
  column :name
  column :email
  column :last_login
end

To render a list of all users add the following to your controller:

class UsersController < ApplicationController
  def index
    respond_with(@users) do |format|
      format.json { render :json => UsersTable.new(@users).as_json }
    end
  end
end

The SlickGrid::Table class will render columns for each model instance by simply calling the column methods on the instance. If you want to customize the output, specify a custom generator function:

class UsersTable < SlickGrid::Table
  column :name, generator: ->(obj) { "#{obj.first_name} #{obj.last_name}" }
  column :email
  column :last_login
end

Rails Model Support

This gem also replaces the example RemoteModel (which can only load data from digg.com) to support loading and updating standard Rails REST models. First, include the class into your manifest:

//= require slick/remotemodel

Then update your table definition to include a path column for each instance:

class UsersTable < SlickGrid::Table
  ...
  column :path, generator: ->(obj) { user_path(obj) }
end

Finally wire up all the grid events (example taken from the requirejs-controllers gem):

initGrid: (@selector, @columns, @options) ->
  @dataView = new Slick.Data.DataView()
  @grid = new Slick.Grid(@selector, @dataView, @columns, @options)
  @model = model = new Slick.Data.RemoteModel()

initLoader: ->
  @model.onDataLoading.subscribe (e, args) =>
    @showIndicator()

  @model.onDataLoaded.subscribe (e, args) =>
    @hideIndicator()

  @model.onDataLoadedSuccess.subscribe (e, args) =>
    @dataView.beginUpdate()
    @dataView.setItems(args.data)
    @dataView.endUpdate()
    @grid.invalidateAllRows()
    @grid.render()

  @model.onDataLoadedError.subscribe (e, args) =>
    alert("failed to load data from server")

initWriter: ->
  @model.onDataWriting.subscribe (e, args) =>
    @showIndicator("Writing")

  @model.onDataWritten.subscribe (e, args) =>
    @hideIndicator()

  @model.onDataWrittenSuccess.subscribe (e, args) =>
    @dataView.updateItem(args.item.id, args.data)
    @grid.updateRow(args.row)

  @model.onDataWrittenError.subscribe (e, args) =>
    alert("failed to write data on server")

  @grid.onCellChange.subscribe (e, args) =>
    @model.writeData(args)

Update SlickGrid

To upgrade SlickGrid version just run

rake slickgrid:update

It will clone the current SlickGrid master and will copy all javascript and stylesheet files into this repository. Don't forget to update the gemspec version!

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

slickgrid-rails's People

Contributors

hollow avatar ryanza avatar woahdae 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.