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

标签 php jquery html mysql

我想使用 jquery 过滤表中的特定列,使用多重选择,用户可以在其中选择多个值。 这是 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>

这是 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();
                });
            }
        });
    });

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

最佳答案

您正在从事:

#body-fornitore tr

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

.row-fornitore

问候

关于php - 使用 jquery 仅从表中的一列过滤多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36161149/

相关文章:

javascript - 如何每 x 秒更改数组中 <li> 的内容?

html - 如何将 textarea 宽度扩展到父级的 100%(或者如何将任何 HTML 元素扩展到父级宽度的 100%)?

css - 页脚布局问题

javascript - Safari 中的随机行?

php - 使用ajax将 session 变量插入数据库

php - 为什么 Doctrine 2.2 使用 -namespace- 和 -use- 而没有任何包含或要求语句?

javascript - jQuery在随机时间生成随机数

javascript - 仅在显示源代码时更改 js 文件位置

来自mysql结果的php多维数组

jquery - Angularjs 如何使用来自不同 Controller 的数据附加 ng-repeat