javascript - 搜索 Html 表格并在不刷新的情况下显示结果

标签 javascript php jquery html css

我的表格上方有一组字段,这些字段用于搜索我的表格。这是一个代码片段:

<?php $form = ActiveForm::begin(['id' => 'devicemgmt-form']); ?>
<div class="container-fluid">
    <div class="row">
        <div class="col-sm-5">
            <div class="row">
                <div class="col-sm-5">Device Name</div>
                <div class="col-sm-7">
                    <input type="text" name="device-name" class="form-control input-sm">
                </div>
            </div>
            <br>
            <div class="row">
                <div class="col-sm-5">Broadcasted Program</div>
                <div class="col-sm-7">
                    <select name="country" class="form-control input-sm">
                        <option selected disabled>Please Select</option>
                        <?php
                            foreach($devices as $key => $value):
                            echo '<option value="'.$key.'">'.$value['Broadcasted Program'].'</option>'; 
                            endforeach;
                        ?>
                    </select>                                
                </div>
            </div> 
            <br>
            <div class="row">
                <div class="col-sm-5">Store</div>
                <div class="col-sm-7">
                    <select class="form-control input-sm">
                        <option selected disabled>Please Select</option>
                        <option>1</option>
                        <option>2</option>
                        <option>3</option>
                        <option>4</option>
                        <option>5</option>
                    </select>
                </div>
            </div> 
        </div>
        <div class="col-sm-5">
            <div class="row">
                <div class="col-sm-5">Model name</div>
                <div class="col-sm-7">
                    <select name="country" class="form-control input-sm">
                        <option selected disabled>Please Select</option>
                        <?php
                            foreach($devices as $key => $value):
                            echo '<option value="'.$key.'">'.$value['Device Model'].'</option>'; 
                            endforeach;
                        ?>
                    </select> 
                </div>
            </div> 
            <br>
            <div class="row">
                <div class="col-sm-5">Status</div>
                <div class="col-sm-7">
                    <select class="form-control input-sm">
                        <option selected disabled>Please Select</option>
                        <option>Started</option>
                        <option>Pending</option>
                        <option>Complete</option>
                    </select>
                </div>
            </div> 
            <br>
            <div class="row">
                <div class="col-sm-5">Log Output Date</div>
                <div class="col-sm-7">
                    <input type="text" name="device-name" class="form-control input-sm">
                </div>
            </div>
        </div>
        <div class="col-sm-2">
            <div class="row" style="height: 98px;"></div>
            <div class="row">
                <button href="#" class="btn download-btn" id="downloadBtn">Log Download</button>
            </div>
        </div>
    </div>
</div>
<?php ActiveForm::end(); ?>

<div class="container-fluid">            
    <table id="device-list_table" class="table table-striped table-bordered dataTable display">
        <thead>
            <tr>
                <th class="text-center"><input type="checkbox" id="selectAll"></th>
                <th class="text-center">Device Name</th>
                <th class="text-center">Device Model</th>
                <th class="text-center">Broadcasted Program</th>
                <th class="text-center" colspan="2">Device Status</th>
                <th class="text-center">Last Communication Date and Time</th>
                <th class="text-center"></th>
            </tr>
        </thead>
        <tbody>
            <?php foreach($devices as $key => $value) { ?>
            <tr>
                <td class="text-center"><input type="checkbox" name="device-checkbox" value="<?= $value['Device Name'] ?>" id="<?php echo 'boxId'.$key ?>"></td>
                <td class="text-center"><?= $value['Device Name']; ?></td>
                <td class="text-center"><?= $value['Device Model']; ?></td>
                <td class="text-center"><?= $value['Broadcasted Program']; ?></td>
                <td class="text-center">
                    <?php
                        if ($value['Device Status'] == "Start") {
                            echo "<font color='yellow'>&#x26AB;</font>";
                        } elseif ($value['Device Status'] == "Pending") {
                            echo "<font color='red'>&#x26AB;</font>";
                        } elseif ($value['Device Status'] == "Complete") {
                            echo "<font color='green'>&#x26AB;</font>";
                        }
                    ?>
                </td>
                <td class="text-center"><?= $value['Device Status']; ?></td>
                <td class="text-center"><?= $value['Last Communication Date and Time']; ?></td>
                <td class="text-center">
                    <ul class="list-inline">
                        <li>
                            <button href="#" class="btn">
                                Display Log
                            </button>
                        </li>
                        <li>
                            <button href="#" class="btn">
                                Edit
                            </button>
                        </li>
                    </ul>
                </td>
            </tr>
            <?php } ?>
        </tbody>
    </table>
</div>

如何在不刷新页面的情况下搜索表中的数据并显示结果(在同一个表中)?如您所见,我有一个“日志下载”按钮,也许当我点击它时,结果就会显示出来。

编辑: 有点像 dataTables ( https://www.datatables.net/) 的实现,但在我的例子中,我有多个表单字段用于搜索。

最佳答案

你可以这样做

$("#deviceName").keyup(function(){                      
         var filter = $(this).val();
        $("#device-list_table tr td").each(function(){
            if ($(this).text().search(new RegExp(filter, "i")) < 0) {
                $(this).hide();
            } else {
                $(this).show();

            }
        });
      });

这仅适用于 deviceName 类型您还可以添加其他属性

关于javascript - 搜索 Html 表格并在不刷新的情况下显示结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35450186/

相关文章:

javascript - 全日历.js : Event callbacks not firing for added events that get resized/clicked/dropped?

javascript - 为什么在 OO JavaScript 中使用 "this"?

jquery - 在单击发生之前,键盘事件在页面加载时不会响应(使用 jquery 热键或捕鼠器)

javascript - JQuery DatePicker 不尊重最小和最大日期范围设置

javascript - 添加新元素后 Jquery 主题不适用

javascript - 是否可以在页面加载时加载所有 WOW.js 隐藏的 HTML 元素?

javascript - 通过 jQuery 更改表单上的操作后,GET 变量消失。

php - Symfony 项目 CMS

php - Mysql如何连接2个表和1个表本身

php - php下拉菜单有问题