derekcroft / zable

HTML table generation with searching and sorting made dead simple

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Bulk action interface

joekur opened this issue · comments

Hey so I'm thinking of calling this new feature "bulk actions" since I think that's the end goal of being able to select rows, but let me know if you have a better name :)

I'm going to describe how I have things set up in my other app so we can discuss how we want things to work after I extract this functionality.

Front-end

Right now I have a jQuery function that let's you initialize the checkbox behavior like so: $('table').zable(). This may be something we want run automatically on, for instance, all tables with a "zable" class. All the behavior itself is stored within a js object that can be accessed with jQuery's data() function: $('table').data('zable'). Now I figured on the front end you may want a simple function available to get the ids of selected rows:

$('table').getSelectedRowIds()
or
$('table').data('zable').getSelectedRowIds()

And finally also a function to submit the selected ids with some action to apply to all of them to the server.

$('table').data('zable').submitAction('send_reminder_email')

The previous would use a hidden form generated by the zable helper, populating the array of ids as well as the action name.

Furthermore I have two events that can be listened to on the table ($('table').on(event, callback)).

  • rows_selected - On transition between no rows selected and at least one selected
  • no_rows_selected - On opposite transition

These are useful for only showing the bulk action interface when you have rows selected, for example.

Model

On the model I created a simple class method "bulk_actions" that sets up methods on the class to accept an array of ids and then process their respective instance method on each model.

class User < ActiveRecord::Base
  bulk_actions :activate, :deactivate, :send_reminder_email

  def activate; end
  def deactivate; end
  def send_reminder_email; end
end

User.activate([1,7,3])

However I'm not sure how useful this really is compared to doing like

User.find(id: [1,7,3]).each(&:activate)

... but I was having fun with metaprogramming :P

Controller

In the controller now you're just using the ids and action name to run the action on each model

class UsersController
  def bulk_action
    User.send(params[:bulk_action], params[:ids].split(','))
    # User.find(id: params[:ids].split(',')).each(&params[:action].to_sym)
    flash[:success] = "Users updated"
    redirect_to users_path
  end
end

I don't think there's much point in implementing this in the gem, do you?

Maybe you could give some input as to what makes sense to include in zable and what is unnecessary? Also wondering what you think of the interfaces!