使用jquery从表中的一列中过滤多个值

时间:2022-12-23 03:37:21

I would like to filter a specific column in the table with jquery using a multiple select where user can choose the multiple values. Here's the HTML and PHP code:

我想使用多重选择过滤表中的特定列,其中用户可以选择多个值。这是HTML和PHP代码:

<table id="simple-table" class="table table-striped table-bordered table-hover">
        <thead>
            <tr>
                <th>Codice Pezzo</th>
                <th>Descrizione</th>
                <th class="center">Fornitore
                <div class="col-sm-12">
                    <select multiple="multiple" class="chosen-select form-control tag-input-style" id="fornitore-select" data-placeholder="Scegli un fornitore..." name="fornitore[]"><option value="">  </option><?php
            // esecuzione della query
            $query_fornitore = "SELECT p.Marca FROM pezzi p INNER JOIN distinte d ON (d.ID_Pezzo = p.ID_Pezzo) INNER JOIN ordini o ON (o.ID_Impianto = d.ID_Impianto) WHERE o.Data_Consegna >= '".$_GET["data1"]."' AND o.Data_Consegna <= '".$_GET["data2"]."' AND o.Elaborato = 0 GROUP BY p.Marca;";
            $result_fornitore = @mysqli_query($conn, $query_fornitore);

            // controllo sul numero dei record coinvolti
            if(@mysqli_num_rows($result_fornitore)!=0)
            {
              // risultato sotto forma di array asscociativo
              while($row_fornitore = mysqli_fetch_array($result_fornitore, MYSQLI_ASSOC))
              {
                echo "<option value=\"".$row_fornitore["Marca"]."\">".$row_fornitore["Marca"]."</option>";
              }
            }
            ?></select>
            </div>
                </th>
                <th>Quantità</th>
            </tr>
        </thead>

        <tbody id="body-fornitore">
            <?php
            if(isset($_GET["ordini_materiali"]) && isset($_GET["data1"]) && isset($_GET["data2"])):
                $query_distinta = "SELECT d.ID_Pezzo, p.Descrizione, p.Marca, SUM(d.Quantita), o.Quantita FROM ordini o INNER JOIN distinte d ON (d.ID_Impianto = o.ID_Impianto) INNER JOIN pezzi p ON (p.ID_Pezzo = d.ID_Pezzo) WHERE o.Data_Consegna >= '".$_GET["data1"]."' AND o.Data_Consegna <= '".$_GET["data2"]."' AND o.Elaborato = 0 GROUP BY p.ID_Pezzo;";
                $result_distinta = @mysqli_query($conn, $query_distinta);
                while($row_distinta = mysqli_fetch_array($result_distinta)): ?>
                <tr>
                    <td><?php echo $row_distinta[0]; ?></td>
                    <td><?php echo $row_distinta[1]; ?></td>
                    <td class="row-fornitore"><?php echo $row_distinta[2]; ?></td>
                    <td><?php echo number_format($row_distinta[3]*$row_distinta[4],2,",","."); ?></td>
                </tr>
            <?php endwhile; endif; ?>
        </tbody>
    </table>

Here's the Jquery code:

这是Jquery代码:

$(document).ready(function () {
        $('#fornitore-select').change(function () {
            var valore = $(this).val() + '';
            //console.log(valore);
            if(valore == null + '')
            {
                $('#body-fornitore tr').show();
            }
            else
            {
                $('#body-fornitore tr').hide();
                $.each(valore.split(","), function(index, item) {
                    var rex = new RegExp(item, 'i');
                    $('#body-fornitore tr').filter(function () {
                        return rex.test($(this).text());
                    }).show();
                });
            }
        });
    });

With this method, when I select the multiple choise the filter considers all the columns of the table, I would like to filter only one (the third in specific). There is a way to do it? I'm going crazy!!

使用这种方法,当我选择多重选择时,过滤器会考虑表的所有列,我想只过滤一个(特定的第三个)。有办法吗?我要疯了!!

1 个解决方案

#1


0  

You are working on:

你正在努力:

#body-fornitore tr

which means all the row, while you want to work on the third column, that is:

这意味着所有的行,而你想在第三列上工作,即:

.row-fornitore

Regards

问候

#1


0  

You are working on:

你正在努力:

#body-fornitore tr

which means all the row, while you want to work on the third column, that is:

这意味着所有的行,而你想在第三列上工作,即:

.row-fornitore

Regards

问候