Rechercher dans un tableau

Par Grope, il y a 10 ans


Bonjour,
J'ai ce code javascript qui me permet de rechercher dans tout un tableau par rapport à la saisie effectué dans un input.
Cependant je souhaiterais effectuer cette recherche seulement sur une colonne, voici mon code javascript :

$(document).ready(function () { (function ($) { $('#filter').change(function () { var rex = new RegExp($(this).val(), 'i'); $('.search tr').hide(); $('.search tr').filter(function () { return rex.test($(this).text()); }).show(); });

Et mon code html :

<input type="text" class="form-control" id="filter"> <table class="table table-hover"> <thead> <tr> <th>Nom</th> <th>Prénom</th> <th>Ville</th> <th>Type</th> <th>Editer</th> <th>Supprimer</th> </tr> </thead> <tbody class="search"> <tr> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> </tbody> </table>

Je n'arrive pas à trouver quelle partie du code Javascript il faudrait modifier si vous pouviez m'aider, merci ;)

2 réponses

betaWeb, il y a 10 ans

Salut,

C'est normal, tu dois rechercher dans le texte tu TD, et non du TR :

$('#filter').change(function () { var rex = new RegExp($(this).val(), 'i'); $('.search tr').hide(); $('.search tr').filter(function () { return rex.test($('td', this).text()); }).show(); });
Grope, il y a 10 ans

Merci pour ta réponse ! ;D