Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Table filter counter remains static and doesn't count!? 1

Status
Not open for further replies.

Katie6

Programmer
Jun 12, 2007
58
GB
Hi there,

I'm using some JavaScript which adds a filter to my table. It works well - as the user types in the search box, the table rows appear and disappear dynamically to show only the rows which contain text that matches what the user types in.

I would like to add a counter next to the search box which shows how many rows are displayed. However, my attempts have failed miserably - the number is static and doesn't change dynamically.

Is anyone able to explain where I have gone wrong? Here my the latest complete demo code:

Code:
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Row 1</title>

<script language="javascript" type="text/javascript">
function rowCount()
{
  	var numRows = 1;
  	var displayNum = 0;
  	var table = document.getElementById('tableID');
  	
		for (var numRows = 1; numRows < table.rows.length; numRows++){
			if (document.getElementById('tableId').rows.length.style!='display:none'){
				displayNum++;
			}
			else{
				displayNum;
			}
		}  	
  	document.glossary.displayNumBox.value = displayNum //toFixed displays numbers to 2 decimal places
}

</script>

<script type="text/javascript">
   	function filter (phrase, _id){
		var words = phrase.value.toLowerCase().split(" ");
		var table = document.getElementById(_id);
		var ele;
		for (var r = 1; r < table.rows.length; r++){
			
			ele = table.rows[r].innerHTML.replace(/<[^>]+>/g,"");
		    var displayStyle = 'none';
		    var displayNum = 0;    
		    for (var i = 0; i < words.length; i++) {
			    if (ele.toLowerCase().indexOf(words[i])>=0){
					displayStyle = '';
				}				
			    else {
				displayStyle = 'none';
				break;
			    }
		    }
			table.rows[r].style.display = displayStyle;
		}
	}
  </script>
  
</head>

<body>
<form name="glossary" style="float: left; padding-right: 20px;">
	<b>Search:</b> <input name="filt" onKeyUp="filter(this, 'tableID', '1'); rowCount();" onkeyup="rowCount();" type="text">|
	<input type="text" id="displayNumBox" name="displayNumBox" value="0"><br />
</form> <br><br>
<table id="tableId">
<tr style="display:none"><td>Row 1</td></tr>
<tr><td>Row 2</td></tr>
<tr><td>Row 3</td></tr>
</table>

</body>

</html>

Many thanks,

Katie
 
I wonder if it is this line:
Code:
...
if (document.getElementById('tableId').rows.length.style!='display:none'){
...
Maybe you are looking to use:
Code:
...
if (table.rows[numRows].style!='display:none'){
...
You were only ever testing the last row from what I can gather.

You might want to rework the function a little more:
Code:
function rowCount() {
	var numRows = 1;
	var displayNum = 0;
	var table = document.getElementById('tableID');
	for (var numRows = 1; numRows < table.rows.length; numRows++) {
		if (table.rows[numRows].style != 'display:none') {
			displayNum++;
		}
	}      
	document.getElementById('displayNumBox').value = displayNum;
}

It's just a variation on what you already have, I guess.

Let us know how you go!

Cheers,
Jeff

[tt]Jeff's Blog [!]@[/!] CodeRambler
[/tt]

Make sure your web page and css validates properly against the doctype you have chosen - before you attempt to debug a problem!

FAQ216-6094
 
Hi Jeff,

Thanks for your help. I can see why what you've written would make sense, but it doesn't seem to work for me. I copied the re-worked rowCount function above and pasted it over my existing one, but the count doesn't work as I would expect.

When I type 'Row 1' in the search box, all other rows become hidden and so I expect the counter to read '1'. But it says '2'.

Now, I think this is because the filter function does not actually write style="display:none" in the html code but hides rows in some other mysterious way that I don't understand! So my rowCount function cannot see that the rows are hidden. (The only reason why the counter picks up that Row 1 is hidden is because I've written style="display:none" manually).

Is there a way to target the rows hidden by the filter() function?...perhaps by linking the two functions together somehow?

Thanks again Jeff,

Katie
 
Ah... try this change...
Code:
...
function rowCount() {
    var numRows = 1;
    var displayNum = 0;
    var table = document.getElementById('tableID');
    for (var numRows = 1; numRows < table.rows.length; numRows++) {
        if (table.rows[numRows].style[!].display[/!] != 'none') {
            displayNum++;
        }
    }      
    document.getElementById('displayNumBox').value = displayNum;
}
...

Cheers,
Jeff

[tt]Jeff's Blog [!]@[/!] CodeRambler
[/tt]

Make sure your web page and css validates properly against the doctype you have chosen - before you attempt to debug a problem!

FAQ216-6094
 
Thank's ever so much Jeff! That works fantastically!

Katie :D
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top