473,382 Members | 1,389 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,382 software developers and data experts.

Filtered table

How can I hide an entire row of a table if one of the cells in that row is
equal to a value drop down box value.

My goal is to have a filtered table so that each cell in each row determines
if it should be hidden by drop down boxes. Thanks for your help.
Jul 23 '05 #1
2 3213
Seth wrote on 18 apr 2004 in comp.lang.javascript:
How can I hide an entire row of a table if one of the cells in that
row is equal to a value drop down box value.

My goal is to have a filtered table so that each cell in each row
determines if it should be hidden by drop down boxes. Thanks for your
help.

<table border=1>
<tr id=tr1><td id=td1>qwerty</td><td>more</td></tr>
<tr id=tr2><td id=td2>qwerty</td><td>more</td></tr>
<tr id=tr3><td id=td3>blah</td><td>more</td></tr>
</table>
<br>

Do not show:
<select onChange=doHide(this); >
<option selected>none</option>
<option>blah</option>
<option>qwerty</option>
</select>

<script>
function doHide(that){
testvalue = that.options[that.selectedIndex].innerHTML
for(i=1;i<4;i++){
trps = document.getElementById("tr"+i).style
tdp = document.getElementById("td"+i)
trps.display=(tdp.innerHTML==testvalue)?"none":""
}
}
</script>

IE6 tested
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 23 '05 #2
"Seth" <se**@doesnotexist.com> writes:
How can I hide an entire row of a table if one of the cells in that row is
equal to a value drop down box value.
To hide a row, set its visibility to hidden (which leaves its space
blank) or its display to none (which completely removes the row).

---
<table id="table">
<tr><td>foo</td><td>bar<td></tr>
<tr><td>foo</td><td>baz<td></tr>
<tr><td>doo</td><td>bar<td></tr>
<tr><td>doo</td><td>baz<td></tr>
</table>

<lable for="col1sel">Column 1:
<select id="col1sel"
onchange="filterTable('table',0,
this.options[this.selectedIndex].value);">
<option selected="selected">None</option>
<option value="foo">Filter out "foo"</option>
<option value="doo">Filter out "doo"</option>
</select>
</label>

<script type="text/javascript">
function filterTable(table,column,value) {
var tableRef = document.getElementById(table);
var rows = tableRef.rows;
if (value) { // for partial matching
var valueRE = new RegExp(value);
}
for(var i=0,n=rows.length;i<n;i++) {
var row = rows.item(i);
if (!value) {
// set default, block in IE, table-row in standards
row.style.display = "";
} else {
var cell = row.cells.item(column);
var cellText = cell.firstChild.nodeValue;
if (valueRE.test(cellText)) {
row.style.display = "none";
} else {
row.style.display = "";
}
}
}
}
</script>
---
This assumes that the table cells contains only text, not HTML, because that
makes it easier to extract the text content.
It also doesn't generaliz very well to filtering on more than one column,
because it sets the display property directly (so unhiding on cell in
a column will make its row visible, even though it might have been hidden
by another column too).
My goal is to have a filtered table so that each cell in each row determines
if it should be hidden by drop down boxes. Thanks for your help.


For this you need to remember the hidden state for each row, so if a row is
hidden by more than one cell, it has to be unhidden by all of them.
---
<table id="table">
<tr><td>foo</td><td>bar<td></tr>
<tr><td>foo</td><td>baz<td></tr>
<tr><td>doo</td><td>bar<td></tr>
<tr><td>doo</td><td>baz<td></tr>
</table>

<lable for="col1sel">Column 1:
<select id="col1sel"
onchange="filterTable('table',0,
this.options[this.selectedIndex].value);">
<option selected="selected">None</option>
<option value="foo">Filter out "foo"</option>
<option value="doo">Filter out "doo"</option>
</select>
</label>

<lable for="col2sel">Column 2:
<select id="col2sel"
onchange="filterTable('table',1,
this.options[this.selectedIndex].value);">
<option selected="selected">None</option>
<option value="bar">Filter out "bar"</option>
<option value="baz">Filter out "baz"</option>
</select>
</label>

<script type="text/javascript">
// format : map(tableId->array(object(hidNum:int,hidCol:array(bool))))
var tableFilterData = {};
function filterTable(tableId,column,text) {
var tableRef = document.getElementById(tableId);
var filterData = tableFilterData[tableId];
if (!filterData) { // create if first time
filterData = tableFilterData[tableId] = [];
}
var rows = tableRef.rows;
if (text) {
var textRE = new RegExp(text);
}
for (var i=0,n=rows.length;i<n;i++) {
var row = rows.item(i);
var rowData = filterData[i];
if (!rowData) {
rowData = filterData[i] = {hidNum:0,hidCol:[]};
}
var nowHide = false;
if (text) {
var cell = rows.item(i).cells.item(column);
var cellText = cell.firstChild.nodeValue;
newHide = textRE.test(cellText);
}
if (newHide != !!rowData.hidCol[column]) {
rowData.hidCol[column] = newHide;
if (newHide) {
rowData.hidNum ++;
} else {
rowData.hidNum --;
}
row.style.display = ((rowData.hidNum == 0) ? "" : "none");
}
}
}
</script>
---
This script works with multiple tables and columns, but is not stable
against changes in the table structure. If that is needed, you can
try putting the rowData object on the row element itself, i.e., change
var rowData = filterData[i];
if (!rowData) {
rowData = filterData[i] = {hidNum:0,hidCol:[]};
}
to
var rowData = row.filterData;
if (!rowData) {
rowData = row.filterData = {hidNum:0,hidCol:[]};
}
It requires you to be able to add properties to the rowElement, which
is most likely possible, but not required by the standard (it's a host
object, so it can behave almost arbitrarily).

Good luck.
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: johnb41 | last post by:
I need to create a Dataset and datatable from an XML file. The only way I know how to make a Dataset and Datatable, is by using an Access database as my datastore: You know, the usual thing in...
6
by: GSteven | last post by:
(as formerly posted to microsoft.public.access.forms with no result) I've created a continuous form which is based on a straightforward table (ex - customers - 100 records). On the form there is...
7
by: Randy | last post by:
Folks: We have a web-based app that's _really_ slowing down because multiple clients are writing their own private data into a single, central database. I guess the previous programmer did...
1
by: sparks | last post by:
I have a main table with teacher names and students I can put this in a subform and filter by teacher name so I have a list of her students in a sub form. the problem I have is this is created in...
1
by: s26f84 | last post by:
Hey Guys Alot of articles and alot of stuff still no answer. Please help me on this one. I need to get data out of a filterred selected row !!! ... this is how my code looks like.... On...
3
by: melnhed | last post by:
---Report the current filtered records from a Form--- Hello All, I've seen this topic discussed before, but the solution described then doesn't work in my particular case. My Config: ...
4
by: Ironr4ge | last post by:
Hi everyone, I am trying to open the form "Languages" with a diffrent record source to the "Contacts" form where I conducted the search or filter... . I was wondering whether there was a vba...
0
by: ramseyscripts | last post by:
I have Table A that is displayed through a form. Table A can be filtered through the form to create a subset of Table A records. How can I create a new table, Table B, with only the records in the...
6
by: Nettle | last post by:
Purpose: I am creating a mailing distribution list database. Users should be able to filter/search contacts and add them to distribution lists they have created. My problem? I can't add multiple,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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: 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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.