Reputation: 65
I have this script for filter table: (change this script into function)
$(document).ready(function(){
var $rows = $('tbody > tr'),
$filters = $('#tableP input');
$filters.on("keyup", function () {
var $i = $filters.filter(function () {
return $.trim(this.value).length > 0;
}),
len = $i.length;
if (len === 0) return $rows.show();
var cls = '.' + $i.map(function () {
return this.className
}).get().join(',.');
$rows.hide().filter(function () {
return $('td', this).filter(cls).filter(function() {
var content = this.textContent.toLowerCase(),
inputVal = $i.filter('.' + this.className).val().toLowerCase();
return content.indexOf(inputVal) > -1;
}).length === len;
}).show();
});
});
<script src="http://code.jquery.com/jquery-3.3.1.min.js" type="text/javascript"></script>
<table id='tableP'>
<thead>
<tr>
<th>
<input type='text' class="nome" placeholder="Nome.."/>
</th>
<th>
<input type='text' class="cognome" placeholder="Cognome.." />
</th>
<th>
<input type='text' class="citta" placeholder="Città.." />
</th>
</tr>
</thead>
<tbody>
<tr>
<td class='nome'>Carlo</td>
<td class='cognome'>Grasso</td>
<td class='citta'>Italia</td>
</tr>
<tr>
<td class='nome'>Giuseppe</td>
<td class='cognome'>Puglisi</td>
<td class='citta'>Italia</td>
</tr>
<tr>
<td class='nome'>Franc</td>
<td class='cognome'>Justin</td>
<td class='citta'>Francia</td>
</tr>
</tbody>
</table>
This works! I want to create a function to call in the input tag, I do not know how to do it. I want to create a function to call in the input tag, I do not know how to do it.
I want to create function in tag input onkeyup myFilter()
, how can I do?
Can someone help me?
Upvotes: 0
Views: 359
Reputation: 6516
Here's an option:
Add a property to identify the inputs
that you want to use to call the function, in my example I added the property data-filter="true"
When you start your script, add the listener to those inputs which have that attribute, and get it's parent <table>
, using closest(...)
(Documentation)
Inside the function, make sure to find just the elements that is children of that table, using $(table).find()
That's it, check below if this helps you
$(document).ready(function(){
$("input[data-filter='true']").on("keydown", function(){
let parentTable = this.closest('.table-filtered');
myTableFilter(parentTable);
});
function myTableFilter(table){
let $rows = $(table).find('tbody > tr');
let $filters = $(table).find('input');
$filters.on("keyup", function () {
var $i = $filters.filter(function () {
return $.trim(this.value).length > 0;
});
var len = $i.length;
if (len === 0) return $rows.show();
var cls = '.' + $i.map(function () {
return this.className;
}).get().join(',.');
$rows.hide().filter(function () {
return $('td', this).filter(cls).filter(function() {
var content = this.textContent.toLowerCase(),
inputVal = $i.filter('.' + this.className).val().toLowerCase();
return content.indexOf(inputVal) > -1;
}).length === len;
}).show();
});
}
});
#tableP{
border: 1px solid blue;
}
#tableP2{
border: 1px solid red;
}
<script src="http://code.jquery.com/jquery-3.3.1.min.js" type="text/javascript"></script>
<table id='tableP' class="table-filtered">
<thead>
<tr>
<th>
<input type='text' class="nome" placeholder="Nome.." data-filter="true"/>
</th>
<th>
<input type='text' class="cognome" placeholder="Cognome.." data-filter="true"/>
</th>
<th>
<input type='text' class="citta" placeholder="Città.." data-filter="true"/>
</th>
</tr>
</thead>
<tbody>
<tr>
<td class='nome'>Carlo</td>
<td class='cognome'>Grasso</td>
<td class='citta'>Italia</td>
</tr>
<tr>
<td class='nome'>Giuseppe</td>
<td class='cognome'>Puglisi</td>
<td class='citta'>Italia</td>
</tr>
<tr>
<td class='nome'>Franc</td>
<td class='cognome'>Justin</td>
<td class='citta'>Francia</td>
</tr>
</tbody>
</table>
<br>
<table id='tableP2' class="table-filtered">
<thead>
<tr>
<th>
<input type='text' class="nome" placeholder="Nome.." data-filter="true"/>
</th>
<th>
<input type='text' class="cognome" placeholder="Cognome.." data-filter="true" />
</th>
<th>
<input type='text' class="citta" placeholder="Città.." data-filter="true" />
</th>
</tr>
</thead>
<tbody>
<tr>
<td class='nome'>Sir</td>
<td class='cognome'>Calvin</td>
<td class='citta'>Brazil</td>
</tr>
<tr>
<td class='nome'>Carlo</td>
<td class='cognome'>Justin</td>
<td class='citta'>Francia</td>
</tr>
<tr>
<td class='nome'>Franc</td>
<td class='cognome'>Grasso</td>
<td class='citta'>Italia</td>
</tr>
</tbody>
</table>
Basically, What I added is this part:
$("input[data-filter='true']").on("keydown", function(){
let parentTable = this.closest('.table-filtered');
myTableFilter(parentTable);
});
And inside your function I added the $(table).find(...)
where it was needed.
Upvotes: 1