I'm trying to apply one to several filters in order to display specific rows in a table from the value of an input field.
I know that the slice() method allows us to do this process, but when multiple values are put used I don't know exactly how to manage structure of several values to achieve the desired result.
For example, if the value field corresponds to 1,5-10,15 it will then show me line 1, lines 5 to 10 and line 15. It is to know that the order of value field doesn't matter. This may look like 5-10, 15, 20-30, 19 or 2,5,8-10,11-15 for example.
Here is what I've tried so far :
for (let i = 0; i <= 50; i++) {
let rows = '<tr>';
rows += '<td>100</td><td>100</td><td>100</td>';
rows += '</tr>';
$('table tbody').append(rows);
}
$('input').off().on('focusout', function() {
let format, row = '';
let val = $(this).val();
if (val.includes(',')) {
row = val.split(',');
}
format = !!row ? row : val;
if (!format.includes('-')) {
let filters = '';
for (let i = 0; i < format.length; i++) {
filters += '.not(":nth-child(' + format[i] + ')")';
}
$('table tbody tr') + filters + '.css("display", "none")';
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" placeholder="1-5,9">
<table border="border">
<thead>
<tr>
<th class="text-center">data</th>
<th class="text-center">data</th>
<th class="text-center">data</th>
</tr>
</thead>
<tbody>
</tbody>
</table>