doupingzhi9674 2013-08-13 17:51
浏览 15

使用javascript从表中搜索数据

I am currently working on an application based on JavaScript and PHP. In my app, I have a table and a textbox - when I enter data in the textbox, the table should show the particular value that I typed. My current implementation does not work as I am not sure how to complete the search component - can anyone help?

          <table name="tablecheck" class="DataTable" id="results" >
     <thead>
          <tr ><th>&nbsp;</th>
    <th>&nbsp;</th>
    <th><center><b>COURSE CODE</b></center></th>
    <th><center>COURSE NAME</center></th></tr>
    </thead>
       <?php if(isset($records)) : foreach ($records as $row) : ?>
       <tbody>
       <tr id="rowUpdate" class="TableHeaderFooter">
    <td>
     <?php echo anchor('coursemaster_site/delete/'.$row->id, 'Delete',array('onClick'=>"return confirm('ARE YOU WANT TO DELETE..?')")); ?>
    </td>

     <td>
        <input type=checkbox name="editcustomer[] "    id="editcustomer[]"  value="<?php echo $row->id ?>" >

      </td>

      <td >
     <input class="inputwide span2 "    type="text"    onblur="upper(this)"  name="course_code_<?php echo $row->id ?>" id=" course_code_<?php echo $row->id ?>"   value="<?php echo   $row->course_code ; ?> " >

     </td>
     <td>
       <input class="inputmedium span2"    type="text" name="course_name_<?php echo $row->id ?>" id="course_name_<?php echo $row->id ?>" value="<?php echo $row->course_name ; ?>" >

    </td>


    </tr>
</tbody>
   <?php endforeach ; ?>
   <?php endif ; ?>
   </table>




   <form action="#" method="get" onSubmit="return false;">
    <label for="q">Search Here:</label><input type="text" size="30" name="q" id="q" value="" onKeyUp="doSearch();" /> 
   </form>
    <script>
  function doSearch() {
  var rowContainsSearchTerm, fullname;
   var q = document.getElementById("q");
   var v = q.value.toLowerCase();
   var rows = document.getElementsByTagName("tr");
     var searchTermMoreThanTwoCharsLong = v.length > 2; 

       for ( var i = 1; i < rows.length; i++ ) {
    fullname = concatInputValues(rows[i].getElementsByTagName("input"));
    if (fullname) {
        rowContainsSearchTerm = fullname.indexOf(v) > -1;
        if (searchTermMoreThanTwoCharsLong && rowContainsSearchTerm) {
            rows[i].style.backgroundColor = "#00F";
        } else {
            rows[i].style.backgroundColor = "";                
        }
    }
   }
   }

    function concatInputValues(inputs){ 
    var inputContents = "";
    for (var j = 0; j < inputs.length; j++) {
    inputContents = inputContents + inputs[j].value;
     }
   return inputContents;
   }
   </script>
  • 写回答

1条回答 默认 最新

  • dongmopu6734 2013-08-13 18:00
    关注

    Have you considered using JQuery DataTables? The plugin has a really nice table view as well as automatically enabled search textbox, and should fit in easily with your JavaScript/PHP solution.

    See example table below: JQuery Data Table

    评论

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端