jQuery - Magic Grid Filters using jQuery
Update: I've uploaded a demo page showing grid filters in action. It can be found here.
Quite often there's a grid or two (or hundred) in a project. One of the neat thing about jQuery is how simple it is to do some really amazing things, such as filtering grid content based on user input.
Take the following example: the above grid has several Employees listed, but the user only wants to see Michael Bolton's information. With jQuery, it is possible to remove grid rows that don't match the user's selection, with just a few lines of code. Here's how.
First, we create a view page listing the employees. The key here is to decorate the table rows and cells with CSS classes so our jQuery function will search only where we want it to.
Code Snippet
- <table>
- <thead>
- <tr>
- <th>Employee ID</th>
- <th>Name</th>
- <th>Age</th>
- <th>Department</th>
- <th>Status</th>
- </tr>
- </thead>
- <tbody>
- @{
- for (int i = 0; i < Model.Employees.Count; i++)
- {
- <tr class="searchable">
- @Html.EditorFor(x => x.Employees[i])
- </tr>
- }
- }
- </tbody>
- </table>
Notice the <tr> element in the table body gets the "searchable" class, while the <tr> element in the table header does not. This ensures that only the content rows of the table will be searched / filtered, and the header row will not.
The Editor Template for the Employees object has the second piece of the puzzle:
Code Snippet
- @model MVC3GridFiltersjQuery.Models.Employee
- <td>
- <label>@Model.EmployeeId</label>
- </td>
- <td>
- @Html.TextBoxFor(x => x.Name, new { @class = "search-target" })
- </td>
- <td>
- @Html.TextBoxFor(x => x.Age, new { @class = "search-target" })
- </td>
- <td>
- @Html.DropDownListFor(x => x.DepartmentId,
- new SelectList(ViewBag.Departments, "DepartmentId", "Description", Model.DepartmentId),
- new { @class = "search-target" })
- </td>
- <td>
- <label class="search-target">@Model.Status</label>
- </td>
Each value in each cell that we want to be part of the filtering is marked with the class "search-target". This ensures that only the values we wish to be targeted by the search are included in the filtering. Note that we purposefully did not mark the EmployeeId label as a search-target.
Finally, the jQuery. This is hooked to the keyup event on our Grid Filter textbox. As the user types in that box, the function is run, and the following happens:
- Rows that have values matching the filter are shown
- The first cell that contains the value matched by the filter is highlighted
- All other rows are hidden
Code Snippet
- <script type="text/javascript">
- $(function () {
- $("#SearchTerm").keyup(function () {
- //Remove previous highlighting
- $(".found").removeClass('found');
- //get the search term
- var terms = $(this).val().toLowerCase();
- if (!terms) {
- //Show all rows if there are no terms
- $(".searchable").show();
- } else {
- //Find all of the rows to search, hide those that don't match the terms, and show the rest
- $(".searchable").hide().filter(function () {
- var isFound = false;
- //Only check cells that are marked as search-target
- $(this).find(".search-target").each(function () {
- //If this is a select list, use the selected option's text()
- if ($(this[0]).is('option')) {
- searchText = $(this).find('option:selected').text();
- }
- //If this is a textbox, use the val()
- else if ($(this).is("input")) {
- searchText = $(this).val();
- }
- //For all other controls, use the text()
- else {
- searchText = $(this).text();
- }
- //Check if the control's text contains the terms
- isFound = (searchText && searchText.toLowerCase().indexOf(terms) > -1);
- if (isFound == true) {
- //Highlight the cell that matched the filter
- $(this).addClass('found');
- return false;
- }
- else {
- return true;
- }
- });
- return isFound;
- }).show();
- }
- });
- });
- </script>
That's it. One quick jQuery function to allow filtering on any grid in your project.
Comments
Post a Comment