Reputation: 411
I need a little help to put me in the right direction. This is what I am trying to achieve.
I have a table of data e.g.
<table><thead>
<tr><th>Categories</th><th>Week 1</th><th>Week 2</th><th>Week 3</th></tr>
</thead>
<tbody>
<tr><td>Category 1</td>
<td>Sam: 5 <br /> Roger 10</td>
<td>Sam: 0 <br /> Roger 5</td>
<td>Susan: 25 <br /> Aimee 15</td>
</tr>
<tr><td>Category 2</td>
<td>Sam: 5 <br /> John: 15</td>
<td>Sam: 0 <br /> Roger: 15</td>
<td>Susan: 25 <br /> Aimee: 15</td>
</tr>
</tbody></table>
These are the users who have number assigned in the table above.
<ul>
<li><a href="#">John</a></li>
<li><a href="#">Roger</a></li>
<li><a href="#">Aimee</a></li>
<li><a href="#">Sam</a></li>
<li><a href="#">Susan</a></li>
</ul>
I was hoping to create a filter using JQuery so that if I wanted to see John only in the table above I would click against that name in the list above. I have complete control over the markup so I can wrap user names in the table with required classes (for selecting) or add any additional code etc.
How do I achieve that? I have limited experience using Jquery so any help would be appreciated. Thanks
Upvotes: 2
Views: 67
Reputation: 1577
modified HTML:
<ul class="names">
<li><a href="#">John</a></li>
<li><a href="#">Roger</a></li>
<li><a href="#">Aimee</a></li>
<li><a href="#">Sam</a></li>
<li><a href="#">Susan</a></li>
</ul>
<table><thead>
<tr><th>Categories</th><th>Week 1</th><th>Week 2</th><th>Week 3</th></tr>
</thead>
<tbody>
<tr><td>Category 1</td>
<td><span data-name="Sam">Sam: 5</span> <br /> <span data-name="Roger">Roger 10</span></td>
<td><span data-name="Sam">Sam: 0</span> <br /> <span data-name="Roger">Roger 5</span></td>
<td><span data-name="Susan">Susan: 25</span> <br /> <span data-name="Aimee">Aimee 15</span></td>
</tr>
<tr><td>Category 2</td>
<td><span data-name="Sam">Sam: 5</span> <br /> <span data-name="John">John: 15</span></td>
<td><span data-name="Sam">Sam: 0</span> <br /> <span data-name="Roger">Roger: 15</span></td>
<td><span data-name="Susan">Susan: 25</span> <br /> <span data-name="Aimee">Aimee: 15</span></td>
</tr>
</tbody></table>
js Code:
$(".names a").click(function(){
$("table span[data-name]").hide();
$("table span[data-name='"+$(this).text()+"']").show();
});
Working fiddle: https://jsfiddle.net/1bh7ue49/1/
Upvotes: 3
Reputation: 7100
In your HTML, just wrap your player names in a span and proceed further.
JS:
function begin(){
$('a').on('click', function(){
let $span = $('span');
$span.show();
const player = $(this).text().trim();
$span.each((i, s) => {
if($(s).text().indexOf(player) === -1){
$(s).hide();
}
});
});
}
$(document).ready(begin);
HTML:
<table>
<thead>
<tr>
<th>Categories</th>
<th>Week 1</th>
<th>Week 2</th>
<th>Week 3</th>
</tr>
</thead>
<tbody>
<tr>
<td>Category 1</td>
<td>
<span>Sam: 5</span>
<br />
<span>Roger 10</span>
</td>
<td>
<span>Sam: 0</span>
<br />
<span>Roger 5</span>
</td>
<td>
<span>Susan: 25</span>
<br />
<span>Aimee 15</span>
</td>
</tr>
<tr>
<td>Category 2</td>
<td>
<span>Sam: 5</span>
<br />
<span>John: 15</span>
</td>
<td>
<span>Sam: 0</span>
<br />
<span>Roger: 15</span>
</td>
<td>
<span>Susan: 25</span>
<br />
<span>Aimee: 15</span>
</td>
</tr>
</tbody>
</table>
<ul>
<li><a href="#">John</a></li>
<li><a href="#">Roger</a></li>
<li><a href="#">Aimee</a></li>
<li><a href="#">Sam</a></li>
<li><a href="#">Susan</a></li>
</ul>
Upvotes: 0