473,385 Members | 1,317 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Filter HTML table by Div Value with Javascript?

Hello,

I am trying to filter an HTML table by div value using javascript.

Here is my table in HTML:
Expand|Select|Wrap|Line Numbers
  1. <table>
  2. <tr> 
  3.   <td><div align="center">1</div></td> 
  4.   <td><div align="left" value="198.1000"><a href="frontend.php">Shields</a></div></td> 
  5.   <td><div align="left">111.0000</div></td> 
  6. </tr> 
  7. <tr> 
  8.   <td><div align="center">2</div></td> 
  9.   <td><div align="left" value="183.2000"><a href="frontend.php">Millwood</a></div></td> 
  10.   <td><div align="left">108.0000</div></td> 
  11. </tr> 
  12. <tr> 
  13.   <td><div align="center">3</div></td> 
  14.   <td><div align="left" value="189.1000"><a href="frontend.php">Lopez</a></div></td> 
  15.   <td><div align="left">106.0000</div></td> 
  16. </tr> 
  17. </table>
  18.  
I would like to know how to only list rows in my table that have a greater div value than 185.

Therefore, when executing the javascript, I would expect that only row 1 and 3 would show, but row 2 would not because its div value is 183.2.

If possible, I would like the user to select from a drop down list values for which greater div values would show.

For example, there could be options for 184, 185, 186, etc. and if they select 185 it will show all rows in which div values are greater than or equal to 185. The same with 186, etc.

If you could provide any insight I would be very appreciative. Thanks!
Sep 28 '10 #1

✓ answered by RamananKalirajan

You can do it. But doing it with div is little bit complicated. Set the value of the div to the tr as it's id like below.

Ex:
Expand|Select|Wrap|Line Numbers
  1. <table id="myTable"> 
  2. <tr id="198.1000">  
  3.   <td><div align="center">1</div></td>  
  4.   <td><div align="left" value="198.1000"><a href="frontend.php">Shields</a></div></td>  
  5.   <td><div align="left">111.0000</div></td>  
  6. </tr>  
  7. <tr id="183.2000">  
  8.   <td><div align="center">2</div></td>  
  9.   <td><div align="left" value="183.2000"><a href="frontend.php">Millwood</a></div></td>  
  10.   <td><div align="left">108.0000</div></td>  
  11. </tr>  
  12. <tr id="189.1000">  
  13.   <td><div align="center">3</div></td>  
  14.   <td><div align="left" value="189.1000"><a href="frontend.php">Lopez</a></div></td>  
  15.   <td><div align="left">106.0000</div></td>  
  16. </tr>  
  17. </table> 
As per your requirement provide a select/combo box to the user from which they can select a value. Onchange of the value trigger a js function and fetch the table object. Check the id of the tr with the value selected in combo box. If the value is equal or greater let the rows be shown else hide it.

Sample:

Expand|Select|Wrap|Line Numbers
  1. function showValue(){
  2.   var selVal = document.getElementById('mySelect').options[document.getElementById('mySelect').selectedIndex].value;
  3.  
  4. var tblObj = document.getElementById('myTable');
  5. var rowLen = tblObj.rows.length;
  6. for(var i=0;i<rowLen;i++)
  7. {
  8.   var idd = tblObj.rows[i].id;
  9.   if(idd>=selVal){
  10.      tblObj.rows[i].style.display='block';
  11.   }else{
  12.      tblObj.rows[i].style.display='none';
  13.   }
  14.  }
Guess this will solve ur problem

Thanks and Regards
Ramanan Kalirajan

16 6242
RamananKalirajan
608 512MB
You can do it. But doing it with div is little bit complicated. Set the value of the div to the tr as it's id like below.

Ex:
Expand|Select|Wrap|Line Numbers
  1. <table id="myTable"> 
  2. <tr id="198.1000">  
  3.   <td><div align="center">1</div></td>  
  4.   <td><div align="left" value="198.1000"><a href="frontend.php">Shields</a></div></td>  
  5.   <td><div align="left">111.0000</div></td>  
  6. </tr>  
  7. <tr id="183.2000">  
  8.   <td><div align="center">2</div></td>  
  9.   <td><div align="left" value="183.2000"><a href="frontend.php">Millwood</a></div></td>  
  10.   <td><div align="left">108.0000</div></td>  
  11. </tr>  
  12. <tr id="189.1000">  
  13.   <td><div align="center">3</div></td>  
  14.   <td><div align="left" value="189.1000"><a href="frontend.php">Lopez</a></div></td>  
  15.   <td><div align="left">106.0000</div></td>  
  16. </tr>  
  17. </table> 
As per your requirement provide a select/combo box to the user from which they can select a value. Onchange of the value trigger a js function and fetch the table object. Check the id of the tr with the value selected in combo box. If the value is equal or greater let the rows be shown else hide it.

Sample:

Expand|Select|Wrap|Line Numbers
  1. function showValue(){
  2.   var selVal = document.getElementById('mySelect').options[document.getElementById('mySelect').selectedIndex].value;
  3.  
  4. var tblObj = document.getElementById('myTable');
  5. var rowLen = tblObj.rows.length;
  6. for(var i=0;i<rowLen;i++)
  7. {
  8.   var idd = tblObj.rows[i].id;
  9.   if(idd>=selVal){
  10.      tblObj.rows[i].style.display='block';
  11.   }else{
  12.      tblObj.rows[i].style.display='none';
  13.   }
  14.  }
Guess this will solve ur problem

Thanks and Regards
Ramanan Kalirajan
Sep 29 '10 #2
Dormilich
8,658 Expert Mod 8TB
some annotations:
- there is no need to use <div> at all
- the value attribute is valid only for input/option elements
- align can be replaced by CSS
@ RamananKalirajan
- line #2: you get the same with document.getElementById('mySelect').value (.value is a property of every form control element)
Sep 29 '10 #3
Thanks very much for your feedback and assistance on this matter. I seem to be encountering some errors with it. For instance, even when I set my minimum value to 200, table rows like this one show:
Expand|Select|Wrap|Line Numbers
  1. <tr id="6.0000"> 
  2.   <td><div align="center">11</div></td> 
  3.   <td><div align="left" value="6.0000"> 
  4. <a href="frontend.php">Charles</a></div></td> 
  5.   <td><div align="left">0.5000</div></td> 
  6. </tr> 
  7.  
I would expect that given that I only want rows that have a higher <td id> greater than 200, something like this wouldn't show...

Would it make a difference if my table is dynamically generated?
Sep 29 '10 #4
RamananKalirajan
608 512MB
Nope there wont be any difference in dynamic things

This is a sample i developed from what you have given, it works fine for me.
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. function showValue(){ 
  5.   var selVal = document.getElementById('mySelect').options[document.getElementById('mySelect').selectedIndex].value; 
  6.  
  7. var tblObj = document.getElementById('myTable'); 
  8. var rowLen = tblObj.rows.length; 
  9. for(var i=0;i<rowLen;i++) 
  10.   var idd = tblObj.rows[i].id; 
  11.   if(idd>=selVal){ 
  12.      tblObj.rows[i].style.display='block'; 
  13.   }else{ 
  14.      tblObj.rows[i].style.display='none'; 
  15.   } 
  16.  } 
  17. }  
  18. </script>
  19. </head>
  20. <body>
  21. <select id="mySelect" onchange="showValue()">
  22.   <option value="100">100</option>
  23.  <option value="150">150</option>
  24. <option value="180">180</option>
  25. </select>
  26. <table id="myTable">  
  27. <tr id="198.1000">   
  28.   <td><div align="center">1</div></td>   
  29.   <td><div align="left" value="198.1000"><a href="frontend.php">Shields</a></div></td>   
  30.   <td><div align="left">111.0000</div></td>   
  31. </tr>   
  32. <tr id="183.2000">   
  33.   <td><div align="center">2</div></td>   
  34.   <td><div align="left" value="183.2000"><a href="frontend.php">Millwood</a></div></td>   
  35.   <td><div align="left">108.0000</div></td>   
  36. </tr>   
  37. <tr id="189.1000">   
  38.   <td><div align="center">3</div></td>   
  39.   <td><div align="left" value="189.1000"><a href="frontend.php">Lopez</a></div></td>   
  40.   <td><div align="left">106.0000</div></td>   
  41. </tr>
  42. <tr id="100.1000">   
  43.   <td><div align="center">4</div></td>   
  44.   <td><div align="left" value="100.1000"><a href="frontend.php">Lopez</a></div></td>   
  45.   <td><div align="left">106.0000</div></td>   
  46. </tr>    
  47. <tr id="120.1000">   
  48.   <td><div align="center">5</div></td>   
  49.   <td><div align="left" value="120.1000"><a href="frontend.php">Lopez</a></div></td>   
  50.   <td><div align="left">106.0000</div></td>   
  51. </tr> 
  52. <tr id="160.1000">   
  53.   <td><div align="center">6</div></td>   
  54.   <td><div align="left" value="160.1000"><a href="frontend.php">Lopez</a></div></td>   
  55.   <td><div align="left">106.0000</div></td>   
  56. </tr> 
  57. <tr id="170.1000">   
  58.   <td><div align="center">7</div></td>   
  59.   <td><div align="left" value="170.1000"><a href="frontend.php">Lopez</a></div></td>   
  60.   <td><div align="left">106.0000</div></td>   
  61. </tr> 
  62. </table>  
  63. </body>
  64. </html>
Please post your code...
Sep 30 '10 #5
My code is contingent upon several functions and variables, but here is the general code, maybe you can find why there might be an issue with the sorting:

Expand|Select|Wrap|Line Numbers
  1.  
  2. <script type="text/javascript">
  3. function showValue(){
  4. var selVal = document.getElementById('mySelect').options[document.getElementById('mySelect').selectedIndex].value;
  5. var tblObj = document.getElementById('myTable');
  6. var rowLen = tblObj.rows.length;
  7.  
  8. for(var i=0 ; i<rowLen ; i++)
  9. {
  10.  var idd = tblObj.rows[i].id;
  11.  if( idd > selVal ){
  12.     tblObj.rows[i].style.display='block';
  13.  } else {
  14.     tblObj.rows[i].style.display='none';
  15.   }
  16.  }
  17. }
  18. </script>
  19. <select id="mySelect" onchange="showValue()">
  20. <option><? $batting_statistic = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 54, 55, 56, 57, 58);
  21. if(in_array($statistic_id, $batting_statistic)) echo "-- Min. At Bats --"; else echo "-- Min. Innings Pitched --"; ?></option>
  22. <option value="5">5</option>
  23. <option value="50">50</option>
  24. <option value="100">100</option>
  25. <option value="150">150</option>
  26. <option value="200">200</option>
  27. <option value="350">350</option>
  28. </select>
  29. <table width="50%" border="0" id="myTable">
  30. <tr>
  31.  <td width="20%"><div align="center" class="style3">Rank</div></td>
  32.   <td width="35%"><div align="left" class="style3">Player</div></td>
  33.   <td width="45%"><div align="left" class="style3"><?= $statistic; ?></div></td>
  34. </tr>
  35. <? $i = 0;
  36.    foreach( $array as $player_id => $value ) {
  37.    $player_name = getPlayerNameFromId ( $player_id );
  38.    $team_name = getTeamNameFromId ( $player_id );
  39.    $innings_pitched = getIpFromId ( $player_id, $ip );
  40.    $at_bats = getABFromId ( $player_id, $ab );
  41.    $position_name = getPosNameFromId ( $player_id );
  42.    $i++; ?>
  43. <tr id="<? $innings_pitched; if(in_array($statistic_id, $batting_statistic)) echo $at_bats; else echo $innings_pitched; ?>">
  44.   <td><div align="center"><?= $i; ?></div></td>
  45.   <td><div align="left" value="<? $innings_pitched; if(in_array($statistic_id, $batting_statistic)) echo $at_bats; else echo $innings_pitched; ?>"><a href="frontend.php?p=<?= $player_id; ?>&s=<?= $statistic_id; ?>&y=<?= $year; ?>"><?= $player_name; ?>, <?= $team_name; ?>, <?= $position_name; ?></a></div></td>
  46.   <td><div align="left"><?= $value; ?></div></td>
  47. </tr>
  48. <? } ?>
  49. </div>
  50. </table></body>
  51. </html>
  52.  
Here is a sample output page: http://tyrone.pae.me/frontend.php?p=347&s=47&y=2010

Please let me know if you find anything that could explain for why it isn't working...
Sep 30 '10 #6
RamananKalirajan
608 512MB
Expand|Select|Wrap|Line Numbers
  1. var idd = tblObj.rows[i].id; 
After this statement insert an alert and check whether the value idd is coming with any space or its containing the perfect value and all.

Thanks and Regards
Ramanan Kalirajan
Sep 30 '10 #7
Hi Ramanan,

I am very poor at javascript and don't know how to do what you suggested. Could you show me what you mean?
Sep 30 '10 #8
RamananKalirajan
608 512MB
Expand|Select|Wrap|Line Numbers
  1. var idd = tblObj.rows[i].id;  
Replace this line with

Expand|Select|Wrap|Line Numbers
  1. var idd = tblObj.rows[i].id;  
  2. alert("***"+idd+"***");
Thanks and Regards
Ramanan Kalirajan
Sep 30 '10 #9
Hi Ramanan,

I've attached some images here of what shows up after applying the alert. Is this to be expected?



Sep 30 '10 #10
RamananKalirajan
608 512MB
Hi,
Why in the first image there was no value? For all the option in the select you have specified value rite?

Thanks and Regards
Ramanan Kalirajan
Sep 30 '10 #11
Hi,

The reason that there was no value in the first image is because I do not specify a <tr id> value for the very first <tr> I have in the table, because the very first <tr> are placeholders for column headers. Additionally, the first <tr> is not in my foreach statement so the php code would not even make sense there.

Here is the very first <tr> with no id value:
Expand|Select|Wrap|Line Numbers
  1.  <table width="50%" border="0" id="myTable">
  2.  <tr>
  3.   <td width="20%"><div align="center" class="style3">Rank</div></td>
  4.    <td width="35%"><div align="left" class="style3">Player</div></td>
  5.    <td width="45%"><div align="left" class="style3"><?= $statistic; ?></div></td>
  6.  </tr>
  7.  
Sep 30 '10 #12
If anyone has any further suggestions or ideas on why this isn't working, please submit to the thread. Thank you.
Oct 1 '10 #13
The problem appears to be the type of value it was looking for. It was resolved by doing:

Expand|Select|Wrap|Line Numbers
  1. parseFloat(idd) > parseFloat(selVal)
  2.  
Oct 3 '10 #14
RamananKalirajan
608 512MB
Glad that you have found it. For the past few days I was strucked with my personal works. So not able to reply you soon.

Thanks and Regards
Ramanan Kalirajan
Oct 4 '10 #15
If I wanted to default the filter on page load to a particular value in my drop down table, how would that be accomplished. The function is 'onchange' at the moment, so simply indicating "yes" for a selected value does not accomplish it. Does anyone have any ideas?
Oct 12 '10 #16
RamananKalirajan
608 512MB
Hi you can do it yaar. It's simple. Few changes in your script function will do that. On page load call the function with the default value.

Ex:
Expand|Select|Wrap|Line Numbers
  1. <body onload = "showValue('100')" >
and change the script function to have a parameter in it. Check for the parameter value. If it is not null use that value, if it is null use the select box value.

Ex:

Expand|Select|Wrap|Line Numbers
  1. function showValue(defVal){
  2.   var selVal;
  3.   if( defVal!=null && defVal!="" )
  4.   {
  5.      selVal = defVal;
  6.      defVal="";
  7.   }
  8.   else
  9.     selVal = document.getElementById('mySelect').value;
  10.  
  11.  
  12. //Your Js Code...  
  13. }

Thanks and Regards
Ramanan Kalirajan
Oct 13 '10 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Jeff J. | last post by:
The basic concept is a music playlist that I'm trying to write as a table inside a div. I have Song and Playlist 'classes'. The Song class has a Write() method, as does the Playlist. The...
4
by: Gareth Gale | last post by:
I'm trying to implement a way of allowing a user to sort a HTML table via Javascript on the client. I've seen lots of samples where single column sorting (asc or desc) is shown, but I'd like nested...
1
by: anonieko | last post by:
This example applies to javascript, table, cells, rows > > How do you access rows and columns of a HTML table? > > > <script language="javascript"> alert('start');
1
by: chansiboy | last post by:
Hi, I've developed a jsp page, where in i had a table getting list of data. I want the table header to be fixed, and i should allow scrolling only for the data in the table. Can anyone please...
4
by: suvarna1026 | last post by:
Hi,I am doing project in ASP.NET(by VB.NEt).I have created an HTML Table.Now I want to apply sorting to the table in javascript.How can I do this?Any help will be appreciated.Thanking you!
11
by: sanju | last post by:
Dear All, I have html table and this table contains 10 Rows and 2 column, I want every time this HTML page is called by the user to view the rows Randomly. How can I do this from JavaScript? ...
18
by: tarunkhatri | last post by:
HI can anyone help me with a function of javascript ... that calculates the sum of all colums at the very last row and also sum of all rows at the very last coloum.... It wiuld be like a 2-d array...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.