0

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
  • Take a look on jQuery DataTables, datatables.net Commented Aug 13, 2013 at 17:56

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

can u provide me where to download the plugin @ Zorayr
If you click on the link on my post (JQuery DataTables), there should be a Download link at the top. You can also do a quick search on the web for JQuery DataTables - it is well documented and widely used. I have used it before, and it's quite easy to drop into an existing application.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.