jquery
TableSorter: Filter Results Based on Search Query
So, you want to use the TableSorter jQuery plugin and you want to be able to search the table and filter the results. Attached is a companion plugin we’ve written, called tablesorterFilter, which will extend tablesorter to provide real-time filtering of rows which match a search query!
Usage
<script type="text/javascript"> jQuery(document).ready(function() { $("#myTable") .tablesorter({debug: false, widgets: ['zebra'], sortList: [[0,0]]}) .tablesorterFilter({filterContainer: $("#filter-box"), filterClearContainer: $("#filter-clear-button"), filterColumns: [0], filterCaseSensitive: false}); }); </script>
Search: <input name="filter" id="filter-box" value="" maxlength="30" size="30" type="text">
<input id="filter-clear-button" type="submit" value="Clear"/>
<table id="myTable">
<thead>
<tr>
<th>Last Name</th>
<th>First Name</th>
<th>Email</th>
<th>Web Site</th>
</tr>
</thead>
<tbody>
<tr>
<td>Smith</td>
<td>John</td>
<td>jsmith@gmail.com</td>
<td>http://www.jsmith.com</td>
</tr>
<tr>
<td>Doe</td>
<td>Jason</td>
<td>jdoe@hotmail.com</td>
<td>http://www.jdoe.com</td>
</tr>
</tbody>
</table>Configuration
tablesorterFilter takes up to six parameters:
- filterContainer – The DOM id of the input box where the user will type the search string.
- filterClearContainer – (optional) The DOM id of the button, image, or whatever which will clear the search string and reset the table to it’s original, unfiltered state.
- filterColumns – An array of columns, starting at 0, which will be searched.
- filterCaseSensitive – (optional) Boolean stating whether the search string is case sensitive. The default is false.
- filterWaitTime – (optional) Time after last key press to start filtering. The default is 500 (milliseconds).
- filterFunction – (optional) Custom function to filter by column text. The default is the plugin function has_words.
Requirements
jQuery version 1.2.1 or higher and a slightly modified jquery.tablesorter.js version 2.0.3.
The modification to the original tablesorter plugin is the addition of a few lines which will cache all of the rows so they can be searched. You can download the modified jquery.tablesorter.js code below.
Download
jquery.tablesorter.js 2.0.3 (modified for tablesorterFilter)
Updated blog post on 1-April-2009 to reflect two additional configuration parameters.
Inline jQuery datePicker and Rails
This post describes how to use the jQuery datePicker plugin (foundĀ here) to display an inline calendar and post the selected date back to your Rails app. I’m simply going to put a hidden text_field on the page and use a bit of javascript to populate the field with the selected date.
Grab the datePicker plugin from http://plugins.jquery.com/project/datepicker, and include it in your Rails app.
Get an inline datePicker set up by following the author’s instructions at http://www.kelvinluck.com/assets/jquery/datePicker/v2/demo/inlineDatePicker.html. Quite simply, you only need to do two things.
One: Add this to your .html.erb page:
<div id="date-picker"></div>
Two: To the same .html.erb page, add this bit of jQuery to turn the the div into an inline datePicker:
<script type="text/javascript">
$('#date-picker').datePicker({inline:true});
</script>
At this point you should see the datePicker on your page. It should look something like:

Now, let’s put a text_field in your form to keep the date. Here’s what I’ve got:
<% form_for(@event) do |f| %> <fieldset> <label for="occurred_on">Date event occurred:</label> <%= f.text_field 'occurred_on' %> <%= f.submit "Save" %> </fieldset> <% end %>
We need to catch the user’s selected date and populate our text_field. To do this, we need to modify the bit of jQuery we used to set up the inline datePicker. We need to bind the action of selecting a date to a javascript function which will populate the text_field. Return to your .html.erb file and replace the jQuery with:
<script type="text/javascript">
$('#date-picker').datePicker({inline:true})
.bind(
'dateSelected',
function(e, selectedDate, $td)
{
$('#event_occurred_on').val(selectedDate.asString());
}
);
</script>
Finally, since you probably don’t want your users to see the text_field, just change it to a hidden_field.
About Justin
Search
Recent Posts
- Not Having a Plan B Makes Plan A More Successful
- Easy Rails API Authentication Using restful-authentication
- God Init.d Script for CentOS
- Private, Authenticated RSS Feeds in Rails
- A Private Web Beta in Seconds with Prefinery
- Backup Your WordPress Blog to Amazon S3 using Ruby
- How to Set an Expires Header in Apache
- How to Configure Apache to Gzip Your Components


