February 12, 2010

Rails link_to with prompt instead of confirm

Today I've been asked to create a link to delete record. In Rails it's quite simple to do it, even there is an example on the documentation. For example,

link_to "Delete Me", {:action => "delete_me", :id => @dm.id}, :confirm => "Are you sure?", :method => :delete

This means to pop-up confirm box with message "Are you sure?" after click the link.

But sometimes, we want the user to enter some text first just to make sure the confirmation or to prevent deletion because of wrong selection by user.

So, instead of confirm I use the prompt box that have a text box. But rails don't have the helper/parameter to do it.

Then I make two links, one is for handle the prompt and user input and the other one to handle the request. So the code is become,

link_to_function "Delete Me", "function_that_handle_user_input"

link_to "Delete Me", {:action => "delete_me", :id => @dm.id}, :method => :delete, :style => "display:none"

Yes, the second link is a hidden link. And the prompt box is inside the function that should handle the user input, example

if(prompt('Enter YES to continue') == 'YES'){
  $(this).next().onclick();
}


For the javascript function, you could write based on your need or just use your imagination.

That's it for now and thanks. Just write a comment if there is another solution or if you want to ask me.

Table sorter javascript plugin

I was assigned a task to provide table sort by client-side. So I find table sorter for javascript and I found several plugin that meet my needs.
This is not a judgement which plugin is the best or worse, I know that plugin is not created for all circumstances. The best plugin is the one that most fit to your circumstances, style, and conditions.
I've try 3 table sorter plugin that most simple and suitable for my circumstances, this time I will explain them

1. TabOrderer (1 JS file. Mini: +/- 9KB, normal: +/- 22KB)
    Dependency: prototype
    +. This plugin have search, filter, and pagination feature.
    +. I think it's simple, easy to use and quite fast.
     -.  It's only accept data in JSON format, need to customize the table first.

2. TableFilter (3 JS files. Mini: +/- 25KB, normal +/- 67KB)
    Dependency: jquery, jquery.cookies, jquery.truemouseout, json, daemachTools
    +. This plugin have filter and pagination feature.
    +. I think it's simple and easy to use.
    +. Can change per-page setting on the fly.
    +. Can sort multiple columns.
    +. It accept data from ordinary table.
     -.  Lot's of dependency files, which make it bigger size.

3. TinyTable (1 JS file. Mini & normal: < 3KB)
    Dependency: prototype
    +. Tiny file size.
    +. This plugin have pagination feature.
    +. I think it's simple and easy to use.
     -.  Must add pagination control by ourself.


These three are the most fit to my conditions.
Note: My conditions is to sort table from search result, can be small to big data, should have pagination feature and no need to edit the row.
There are still a lot of plugins that I've not tried, if you want you can see it here,
ThePixelArt
Tympanus