|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
Latest version is v1.0e Demo
BackgroundA while ago, I was working on a typical web application that allows a user to browse and manage customer data stored in a database, among other things. The normal thing to do would be implement such logic on the server-side. However, a specific requirement was that the user must be able to quickly search and find one or more records from a table using one or more search criteria. To make a round trip to the server and back is considered too expensive. Therefore a client-side solution is required. This solution uses a combination of JavaScript and DHTML. The script was created with flexibility, reusability and simplicity in mind. I.e. The developer (meaning you) is only required to include the script, setup a few parameters, and make the JavaScript calls in the appropriate event handlers. FeaturesThis script offers the following set of features:
UsageIn order to use this script, you will first have to include it in your HTML as follows: <SCRIPT LANGUAGE="JavaScript" SRC="tablefilter.js" TYPE='text/javascript'>
</SCRIPT>
And the recommended place to include this code is in the Setting up the tableFirst, give the table element a handle using the attribute An example of how this will look in HTML is shown here: <table id="dataTable">
<tbody>
<tr>
<td TF_colKey="ckbox"><INPUT type="checkbox"></td>
<td TF_colKey="name">Joe</td>
<td TF_colKey="group">Alpha</td>
<td TF_colKey="salary">400</td>
<td TF_colKey="zone">North,South</td>
<td TF_colKey="status">Off-Site</td>
</tr>
<tr>
<td TF_colKey="name">Celest</td>
<td TF_colKey="group">Beta</td>
<td TF_colKey="salary">5000</td>
<td TF_colKey="zone">North</td>
<td TF_colKey="status">OK</td>
</tr>
</tbody>
</table>
Now that's the easy part done! Setting up the search formThe search criteria input mechanism is implemented using an HTML <form name="filter" onsubmit="TF_filterTable(dataTable, filter);return false"
onreset="_TF_showAll(dataTable)">
Override You may also want to allow the user to easily reset the search form and show all the rows by having Using a text input boxUse the following code to implement a text input search field: <input type="text" TF_colKey="name" TF_searchType="full"
onkeyup="TF_filterTable(dataTable, filter)">
The attribute The attribute To activate the search as soon as the user presses a key, override Using a selection list input boxUse the following code to implement a selection list input search field: <select TF_colKey="status" onChange="TF_filterTable(dataTable, filter)">
<option TF_not_used value="">-</option>
<option value="OK">OK</option>
<option value="Off-Site">Off-Site</option>
<option value="On Leave">On Leave</option>
</select>
Again, the attribute As with all other search inputs, the optional attribute Note that the To activate the search as soon as a selection is made, override If a multiple selection list is used (by specifying the Filtering the content of the table based on checkbox stateBy popular demand, I've added a new feature that allows the filtering of rows based on the state of checkbox. To use this feature, you can specify the search box in one of two ways; either a checkbox or a dropdown list. In the above demo, I've chosen to use the dropdown list for ease of implementation. Do note that as three states are required to express the search criteria (i.e. checked, unchecked and off), plus the fact that a HTML checkbox cannot be used in tri-state mode, you will need to implement another control (e.g. checkbox) to enable or disable the search checkbox to fully express the search criteria, which IMHO, looks rather clumsy. In any case, the code fully supports this use case and I've done some preliminary testing so it should work as advertised. The code to display the dropdown list search criteria is as follows: <SELECT onchange="TF_filterTable(dataTable, filter)"
TF_colKey="ckbox" TF_searchType="checkbox">
<OPTION value="" selected TF_not_used>-</OPTION>
<OPTION value="true">checked</OPTION>
<OPTION value="false">unchecked</OPTION>
</SELECT>
Again, the attribute Note that the use of For those who are interested, the code to display the checkbox search criteria is as follows: <INPUT type="checkbox" TF_colKey="ckbox"
onclick="TF_filterTable(dataTable, filter)">
Building complex query inputsOne of the challenges I faced in the project was to build a single search criterion using multiple input fields. E.g. table value is "100 USD" and the inputs are constructed using one text box for the numeric part, and one selection list for the units portion (See demo above for a live example). At first thought, I had wanted to incorporate this feature directly in the script. But I've yet to find an elegant way to do it; the implementation wasn't robust enough to handle all the combinations plus using it becomes really complicated. Perhaps in a future version. In the meantime however, here's an approach you can use: Define an intermediate input field that is hidden from view (Use either a hidden input field or set the display style to none). Write a custom function to perform the necessary formatting (e.g. concatenating the values) from the inputs that make up the complex query and place the result in the hidden field. Then in the event handler of the search inputs, make a call to the formatting function before calling the table filtering function. And that's it! Here's the code snippet that does the complex query in the demo above: <input type="text" name="salText"
onblur="TF_concat_and_set(salText, salSelect, salHidden);
TF_filterTable(dataTable, filter);">
<select name="salSelect"
onChange="TF_concat_and_set(salText, salSelect, salHidden);
TF_filterTable(dataTable, filter);">
<option value="" TF_not_used>-</option>
<option value=" USD">USD</option>
<option value=" SGD">SGD</option>
<option value=" YEN">YEN</option>
</select>
<input type="hidden" name="salHidden" TF_colKey="salary"
TF_searchType="substring">
Notice that the custom attribute Turning the search form on/offA utility function <input type="checkbox" checked
onclick="TF_enableFilter(dataTable, filter, this)">
Browser CompatibilityThis script and the demo were developed and tested on the IE platform only, since the project did not require Netscape compatibility. It shouldn't be too difficult to make it work on Netscape and I'll probably get round to it when I have the time. I would appreciate any help in this area as well. ConclusionWell, that's it folks. It has been good fun developing this script and writing this article. Hope you will enjoy it too! Oh, and I most certainly welcome any feedback, bug reports or suggestions. Version History
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||