drxdn40242 2018-04-19 06:20
浏览 152
已采纳

在视图中过滤/删除数据而不刷新页面

Here is my view page which has data array and its viewed using foreach and table tag.

            <?php if (count($catArray) > 0) { ?>
                <center>
                    <table class="table_existing" >
                        <thead>
                            <tr>
                                <th>Cat</th>
                           </tr>
                        </thead>
                        <tbody>

                            <?php
                            foreach ($catArray as $egs) {
                                ?>  
                                <tr style="border-bottom: 1px solid #000">
                                    <td><?php echo $egs->cat; ?></td>
                                  </tr>
                            <?php } ?>
                        </tbody>
                    </table>
                </center>
            <?php } ?>

I want to filter data of category on change of dropdown :

My Dropdown Code like below :

 <select  class="inputnw" onchange="searchAllData()">
    <?php echo getType($cd) ?>
</select>

My JS like below :

function searchAllData(){
        var url = '<?php echo site_url("c/e"); ?>';
        window.location.href=url;
    }

It filters data when page is reloaded. What i want is to filter $catArray array on change searchAllData method. without refreshing the page.

Suggest me a work around.

  • 写回答

1条回答 默认 最新

  • douhuanchi6586 2018-04-19 08:11
    关注

    Here is the solution :

    JS

     function onPageSearch() {
                var input, filter, table, tr, td, i;
                input = document.getElementById("search");
                filter = input.value.toUpperCase();
                table = document.getElementById("catTable");
                tr = table.getElementsByTagName("tr");
                for (i = 0; i < tr.length; i++) {
                    td = tr[i].getElementsByTagName("td")[0];
                    if (td) {
                        if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
                            tr[i].style.display = "";
                        } else {
                            tr[i].style.display = "none";
                        }
                    }
                }
        }
    

    View data :

     <?php if (count($catArray) > 0) { ?>
                <center>
                    <table id="catTable" class="table_existing" >
                        <thead>
                            <tr>
                                <th>Cat</th>
                           </tr>
                        </thead>
                        <tbody>
    
                            <?php
                            foreach ($catArray as $egs) {
                                ?>  
                                <tr style="border-bottom: 1px solid #000">
                                    <td><?php echo $egs->cat; ?></td>
                                  </tr>
                            <?php } ?>
                        </tbody>
                    </table>
                </center>
            <?php } ?>
    

    Search input field :

    <input type="text" id="search" onkeyup="onPageSearch()" placeholder="Search">
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么