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

I want to make some jQuery that shows some table rows and hides others based on the content of the firs

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744885355a4599085.html

相关推荐

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

    I want to make some jQuery that shows some table rows and hides others based on the content of the firs

    1天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信