html - how to filter table ( table data is populated from json file) by using jquery? -
i have table has user details. have populated data json. @ bottom of table , have text field provided users can type information filter table. example,
consider table contains 10 rows. first 6 rows contains developer details , last 4 rows contains tester details. if user type developer in text box , table should display rows, contain information developer. how can achieve using jquery ?
i don't want post wrong code here. please don't ask me post code.
you can use filter() jquery function filter through rows until find ones <td>
matches write in text box , filter after click button.
example:
html:
<table id="mytable"> <thead><tr><td>member</td><td>stuff</td></tr></thead> <tbody> <tr><td>developer</td><td>1</td></tr> <tr><td>developer</td><td>2</td></tr> <tr><td>developer</td><td>3</td></tr> <tr><td>tester</td><td>4</td></tr> <tr><td>tester</td><td>5</td></tr> </tbody> </table> <input type="text" id="filter" /> <input type="button" id="btnfilter" value="filter" />
javascript:
$(document).ready(function() { $("#btnfilter").click(function() { $("#mytable tbody tr").show(); if($("#filter").val.length > 0) { $("#mytable tbody tr").filter(function(index, elm) { return $(elm).html().touppercase().indexof($("#filter").val().touppercase()) < 0; }).hide(); } }); });
working fiddle: http://jsfiddle.net/tkqw9/1/
edit: added touppercase() text compare part compares case insensitive. added compare whole <tr>
element content can search on cell value in row.
Comments
Post a Comment