javascript - Hide table row based on content of table cell - Stack Overflow

admin2025-04-17  0

I want to make some jQuery that shows some table rows and hides others based on the content of the first table cell in each row.

When I click a list item I want jQuery to check if the first letter of the item matches the first letter in any table cell in my markup, if so the parent table row should be shown and other rows should be hidden.

This is my markup:

<ul>
<li>A</li>
<li>B</li>
<li>G</li>
</ul>

<table>

<tr>
<td>Alpha1</td>
<td>Some content</td>
</tr>

<tr>
<td>Alpha2</td>
<td>Some content</td>
</tr>

<tr>
<td>Alpha3</td>
<td>Some content</td>
</tr>

<tr>
<td>Beta1</td>
<td>Some content</td>
</tr>
<tr>
<td>Beta2</td>
<td>Some content</td>
</tr>
<tr>
<td>Beta3</td>
<td>Some content</td>
</tr>


<tr>
<td>Gamma1</td>
<td>Some content</td>
</tr>

<tr>
<td>Gamma2</td>
<td>Some content</td>
</tr>

<tr>
<td>Gamma3</td>
<td>Some content</td>
</tr>
</table>

So if I press "A" this is what is rendered in the browser:

<ul>
<li>A</li>
<li>B</li>
<li>G</li>
</ul>

<table>

<tr>
<td>Alpha1</td>
<td>Some content</td>
</tr>

<tr>
<td>Alpha2</td>
<td>Some content</td>
</tr>

<tr>
<td>Alpha3</td>
<td>Some content</td>
</tr>

</table>

I'm really new to jQuery so any hint on how to go about a problem like this would be appreciated :)

I want to make some jQuery that shows some table rows and hides others based on the content of the first table cell in each row.

When I click a list item I want jQuery to check if the first letter of the item matches the first letter in any table cell in my markup, if so the parent table row should be shown and other rows should be hidden.

This is my markup:

<ul>
<li>A</li>
<li>B</li>
<li>G</li>
</ul>

<table>

<tr>
<td>Alpha1</td>
<td>Some content</td>
</tr>

<tr>
<td>Alpha2</td>
<td>Some content</td>
</tr>

<tr>
<td>Alpha3</td>
<td>Some content</td>
</tr>

<tr>
<td>Beta1</td>
<td>Some content</td>
</tr>
<tr>
<td>Beta2</td>
<td>Some content</td>
</tr>
<tr>
<td>Beta3</td>
<td>Some content</td>
</tr>


<tr>
<td>Gamma1</td>
<td>Some content</td>
</tr>

<tr>
<td>Gamma2</td>
<td>Some content</td>
</tr>

<tr>
<td>Gamma3</td>
<td>Some content</td>
</tr>
</table>

So if I press "A" this is what is rendered in the browser:

<ul>
<li>A</li>
<li>B</li>
<li>G</li>
</ul>

<table>

<tr>
<td>Alpha1</td>
<td>Some content</td>
</tr>

<tr>
<td>Alpha2</td>
<td>Some content</td>
</tr>

<tr>
<td>Alpha3</td>
<td>Some content</td>
</tr>

</table>

I'm really new to jQuery so any hint on how to go about a problem like this would be appreciated :)

Share asked Feb 20, 2011 at 22:21 timkltimkl 3,33912 gold badges59 silver badges71 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

Sotris is almost correct in his answer. I believe that what you want is:

$('li').click(function() {
    var letter = $(this).text();
    $("td").hide();
    $("tr").each(function(){
        if ($("td:first:contains('" + letter + "')", this).length != 0) {
            $('td', this).show();
        }
    })
});

If you are really interested only in paring the first letter of the first "TD" in a row, and not any letter (":contains") then change the line that says:

if ($("td:first:contains('" + letter + "')", this).length != 0) {

by:

if ($("td:first", this).text().substr(0,1) == letter) {

Alternatively you could use a regular expression, e.g. replace:

var letter = $(this).text();

By:

var re = new RegExp('^' + $(this).text());

and the line that says:

if ($("td:first:contains('" + letter + "')", this).length != 0) {

by:

if ($("td:first", this).text().match(re)) {

edit: I hadn't checked that it doesn't show and the second td so I changed a little my code:

 $('li').click(function() {
var letter = $(this).text();
    $("table td").hide();
    $("table td:contains("+letter+"), table td:contains("+letter+") + td").show();
});

Demo: http://jsfiddle/rdBx9/1

or

$('li').click(function() {
var letter = $(this).text();
    $("table td").hide();
    $("table td:contains("+letter+")").show().next().show();
});

Demo: http://jsfiddle/rdBx9/2

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744885356a272501.html

最新回复(0)