Using the Find Functions for HTML Database Forms - The find() Function
(Page 4 of 4 )
The find() function uses all the above functions to do its work. I will give you the function first before I explain how it operates.
function find()
{
var titleAndItem = prompt("Enter the Title and Item separated by one Space (example shown)", "Name Smith");
if ((titleAndItem != null)&&(titleAndItem != ""))
{
//validate Find form entry
var arr = getTwoWords(titleAndItem);
if (arr[1] == "")
alert('You did not type correctly.')
else
{//call the appropriate search function.
// first check if the column for the title is sorted.
var titleInx = getColumnIndex(arr[0]); //the cell index of the recordset
if (titleInx == 6)
alert('You did not type the title correctly.') //title does not exist
else
{//title exist
var foundRowIndex;
if (titleBoolArr[i][1] == "true")
foundRowIndex = binarySearch(arr[1], titleInx);
else
foundRowIndex = ordinarySearch(arr[1], titleInx);
//if foundRowIndex is total number of rows then item was not found
if (foundRowIndex == numberOfRows)
alert('Item was not found')
else
{//item was found - so display the items on the form controls
if (foundRowIndex == 0) //the first row was seen
showFirstRow();
else if (foundRowIndex == (numberOfRows - 1)) //the last row was seen
showLastRow();
else
{//a row between the first and the last was seen
index = foundRowIndex; //the new index
for (j=0;j<6;j++)
{
//form the input text control ID
CID = "EI" + j;
//form the recordset ID
RID = "TD" + index + j;
document.getElementById(CID).value = document.getElementById(RID).innerHTML;
//show the data row number
document.getElementById('RowPosition').value = index + 1;
}
//enable all navigation buttons
enableButtons('SFA1','SP1','SN1','SL1');
}
}
}
}
}
}
When the Find button on the form is clinked, the JavaScript find() function is called. The first thing it does is issue a prompt dialog box. I have explained above what the dialog box takes as input. The line for the code is:
var titleAndItem = prompt("Enter the Title and Item separated by one Space (example shown)", "Name Smith");
The first argument for the prompt function is an instruction to the user on what he has to do. The second is the default value that goes into the input text control. In this case it serves as an example of what the user should type. The prompt() function returns a string made up of the title of a column and a cell value of the column. The returned string is held by the variable, titleAndItem.
The find() function only continues if titleAndItem is not null and is not an empty string. When it continues, it first of all validates the string (titleAndItem) returned by the prompt() function. Well, it does not do a lot, it simply checks to see if the titleAndItem is made up of two words. If it isn't, the error message "You did not type correctly" is issued. Otherwise the find() function uses the two words. The cell value is called the key.
The find() function then goes on to get the column index for the title by calling the getColumnIndex() function. This function takes the title of the column as an argument. If it sees the index, it returns it, which is a number between 0 and 5. If it returns 6, the total number of visible columns, it means the index was not found. In this case the find() function issues the error massage "You did not type the title correctly."
It then goes on to check whether the column for the title is sorted. If the column is sorted, it calls the binarySearch() function, passing the cell value (key) as the first argument and passing the column index as the second argument. If the column is not sorted, it calls the ordinarySearch() function, passing the same arguments as for the binary search. If either of the functions called returns the maximum number of rows in the recordset, it means that the key was not found. In this case the function issues the error message "Item was not found."
If the item was found, that is the index returned is in the range from 0 to (numberOfRows - 1), the find function has three final options:
- If the index is the first, that is, 0, the showFirstRow() function is called to display the first record.
- If the index is the last, that is, (numberOfRows - 1), the showLastRow() function is called to display the last row.
- If the index is between the first and last, the function displays the corresponding record and enables all the navigation buttons.
We shall continue in the next part of the series.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |