1

This is my follow-up question of this... See here. I have a jscript(courtesy of hex494D49), searching values from first column.

All I need is, when the values is searching by the user, the table headers will also displayed and if the values is not store. There's a message will display like this "No results found". How to do that?

Here's the JSfiddle file with html

Here's the JScript:

document.getElementById('term').onkeyup = function(){
var term = this.value;    
var column = 0;            
var pattern = new RegExp(term, 'g');  
var table = document.getElementById('dataTable');
var tr = table.getElementsByTagName('TR');
for(var i = 0; i < tr.length; i++){
  var td = tr[i].getElementsByTagName('TD');
  for(var j = 0; j < td.length; j++){
    if(j == column && td[j].innerHTML == term){
      console.log('Found it: ' + td[j].innerHTML);
      tr[i].style.display = 'block';
      return;            
      alert('Found it: ' + td[j].innerHTML);
    }else{
      tr[i].style.display = 'none';
    }
  }    
}
};
Community
  • 1
  • 1
User014019
  • 1,215
  • 8
  • 34
  • 66
  • 1
    Tables have a [*rows*](http://www.w3.org/html/wg/drafts/html/master/tabular-data.html#dom-table-rows) collection, and each row has a [*cells*](http://www.w3.org/html/wg/drafts/html/master/tabular-data.html#dom-tr-cells) collection, so there is no need for *getElementsByTagName*. The default *style.display* property for table rows is *block* in some browsers, but but in most it is *table-row*. – RobG Aug 29 '14 at 02:06
  • @RobG so i change the block into table-row and now it display properly. How can I display also the table headers? – User014019 Aug 29 '14 at 02:12
  • 1
    I see. I'll modify the code and come back with a new version shortly. – hex494D49 Aug 29 '14 at 04:30

1 Answers1

1

This would be the table markup. As you can see, I added thead, tbody and tfoot groups

<!-- search box -->
<input type="text" name="term" id="term" autocomplete = "off" />

<!-- table results -->
<table id="dataTable">
<thead>
    <tr>
        <th>Example No.</th>
        <th>Column 1</th>
        <th>Column 2</th>
    </tr>
</thead>
<tfoot>
    <tr>
        <th colspan="3"></th>
    </tr>
</tfoot>
<tbody>
<tbody>    
    <tr>
        <td>345678917</td>
        <td>Test 1</td>
        <td>one_test1@gmail.com</td>
    </tr>
    <tr>
        <td>3512376894</td>
        <td>Test 2</td>
        <td>two.test2@hotmail.com</td>
    </tr>
</tbody>
</table>

Default CSS for the markup above. Next step would be merging the following with the rest of your CSS.

table thead {
    display: table-row-group;
}

table tbody {
    display: table-row-group;
}

table tbody tr {
    display: none;
}

And finally the JavaScript snippet using getElementsByTagName() method

// JavaScript
document.getElementById('term').onkeyup = function(){
  var term = this.value;    
  var column = 0;
  var msg = 'No results found!';
  var pattern = new RegExp(term, 'g');  
  var table = document.getElementById('dataTable');
  var tr = table.getElementsByTagName('TR');

  for(var i = 0; i < tr.length; i++){
    var td = tr[i].getElementsByTagName('TD');
    for(var j = 0; j < td.length; j++){
      if(j == column && td[j].innerHTML == term){
        tr[i].style.display = 'table-row';
        table.tFoot.innerHTML = '';  
        return;
      }else{
        tr[i].style.display = 'none';
        table.tFoot.innerHTML = msg;
      }
    }    
  }
};

Working jsFiddle | Version without tfoot jsFiddle


The same as above but using rows[] and cells[] collection

HTML

<!-- Search box -->
<input type="text" id="search" autocomplete = "off" />
<!-- Table -->
<table id="table">
    <thead>
        <tr>
            <th>Product</th>
            <th>Manufacturer</th>
            <th>Price</th>
            <th>InStock</th>
        </tr>    
    </thead>
    <tbody>
        <tr>
            <td>MacBook Air</td>
            <td>Apple</td>
            <td>$456</td>
            <td>85</td>            
        </tr>
        <tr>
            <td>Arc GIS</td>
            <td>ESRI</td>
            <td>$4556</td>
            <td>15</td>                        
        </tr>
        <tr>
            <td>3ds MAX</td>
            <td>Aurodesk</td>
            <td>$6556</td>
            <td>359</td>                        
        </tr>
        <tr>
            <td>Windows 7</td>
            <td>Micorsoft</td>
            <td>$256</td>
            <td>2567</td>                        
        </tr>        
    </tbody>
</table>
<!-- Message -->
<div id="message"></div>

CSS

table thead {
    display: table-row-group;
}
table tbody tr {
    display: none;
}

JavaScript

document.getElementById('search').onkeyup = function(){
    var table = document.getElementById('table'),
        tr = table.rows, td,
        term = this.value.toLowerCase(), column = 0, i, j,
        message = document.getElementById('message');

    for(i = 1; i < tr.length; i++){
        td = tr[i].cells;
        for(j = 0; j < td.length; j++){
            if(j == column && td[j].innerHTML.toLowerCase() == term){
                tr[i].style.display = 'table-row';
                message.innerHTML = '';
                return;
            }else{
                tr[i].style.display = 'none';
                message.innerHTML = 'No results found!';
            }
        }
    }
};

Working jsFiddle If you won't use thead and tbody in your table here is another version jsFiddle

I case you want to search all columns, just change this line

if(j == column && td[j].innerHTML.toLowerCase() == term){

to this one

if(td[j].innerHTML.toLowerCase() == term){

And finally, if you want to have more flexible search try the version below

document.getElementById('search').onkeyup = function(){
    var table = document.getElementById('table'),
        tr = table.rows, td,
        term = this.value.toLowerCase().trim(), column = 0, i, j,
        message = document.getElementById('message'),
        pattern = new RegExp(term, 'gi');

    for(i = 1; i < tr.length; i++){
        td = tr[i].cells;
        for(j = 0; j < td.length; j++){
            if(j == column && term.length > 0 && td[j].innerHTML.match(pattern)){    
                tr[i].style.display = 'table-row';
                message.innerHTML = '';
                return;
            }else{
                tr[i].style.display = 'none';
                message.innerHTML = 'No results found!';
            }
        }
    }
};

Working jsFiddle

hex494D49
  • 9,109
  • 3
  • 38
  • 47
  • 1
    The suggestion made above related to `display` property is correct and I changed it to `table-row`. As I sad before, in the initial answer, table rows and columns may be referenced by `rows` and `cells` collection but they may be referenced by `getElementsByTagName('TR')` and `getElementsByTagName('TD')` methods as well and that's just fine. However, I'll add the version using `rows` and `cells` in the meantime. – hex494D49 Aug 29 '14 at 06:00
  • 1
    @User014019 As you can see, I changed the `table` markup a bit. I'm not sure if you are able to change it in your project; if not, let me know I'll do it in a different way; but let's say, this is a proper `table` markup having `header`, `body` and `footer`. So, give it a try and let me know if is everything ok. `` this `3` in here is the number of your table columns – hex494D49 Aug 29 '14 at 06:04
  • 1
    This is all I need! Thanks to you! +1 – User014019 Aug 29 '14 at 06:11
  • @User014019 Just in case, this is the version without `tfoot` having an extra `div` for `no results` messag [jsFiddle](http://jsfiddle.net/z82L1Lxu/13/) Added to the answer as well. – hex494D49 Aug 29 '14 at 06:13
  • the last jsfiddle that you gave is same as the first jsfiddle – User014019 Aug 29 '14 at 06:18
  • 1
    @User014019 Oh sorry! Forgot that jsFiddle doesn't update things automatically. Here it is [jsFiddle](http://jsfiddle.net/z82L1Lxu/14/) and updated the answer as well. – hex494D49 Aug 29 '14 at 06:26
  • the second one is much better. +1 – User014019 Aug 29 '14 at 06:42
  • 1
    @User014019 Yes, the second one could be better if you have no control over the (dynamic) table; it doesn't need extra CSS for `tfoot` as well. I'll be off for an hour; test, play around with the code... if you need to improve or change something, let me know. As I said, in the meantime I'll add the version using `rows` and `cells`. – hex494D49 Aug 29 '14 at 06:53
  • @User014019 Check the updated answer. I added another two versions so I hope you'll find them useful ;) If you have any further issues, just drop me a note. – hex494D49 Aug 29 '14 at 08:48
  • Ok. I'll check it :) Thank you for your help. I'll drop some comment here. If I have a further questions :) – User014019 Aug 29 '14 at 09:05
  • 1
    @User014019 I see. Ok, give me a couple of minues ;) – hex494D49 Sep 04 '14 at 07:46
  • @User014019 Thanks a lot for inviting me to answer the question but as you may see I'm a bit late :) I did't see the question (and this message) earlier. I couldn't post another answer since there are already a couple of correct answers and it wouldn't be fair. However you may use `alert(document.getElementsByName("FIELD_01_01_FIRSTNAME")[0].value);` or `alert(document.querySelector('input[name="FIELD_01_01_FIRSTNAME"]').value);` or if you have a `form` around the table use `document.forms['form-name']['FIELD_01_01_FIRSTNAME'].value` – hex494D49 Sep 04 '14 at 08:05
  • i try this alert(document.getElementsByName("FIELD_01_01_FIRSTNAME")[0].value); or this one document.forms['form-name']['FIELD_01_01_FIRSTNAME'].value, but when the alert is popup the values is not displayed see this pls jsfiddle.net/34mjxtqn/15 – User014019 Sep 04 '14 at 08:14
  • @User014019 Because the value of `FIELD_01_01_FIRSTNAME` is empty. Check this out http://jsfiddle.net/34mjxtqn/18/ Or maybe you want to hook an event on every input field? – hex494D49 Sep 04 '14 at 08:14
  • what do you mean an event on every input field? – User014019 Sep 04 '14 at 08:16
  • @User014019 Well, you could alert the value on `onchange`, `onfocusout` or `onblur` event. I'm saying this since I'm not sure what's the final goal. If all you need is accessing and alerting the value of an element this `alert(document.forms['FORM1']['FIELD_01_01_FIRSTNAME'].value)`would do the job. – hex494D49 Sep 04 '14 at 08:20
  • @hex49D49 got it! And oh, I have another question. I hope you can help with that. Here http://stackoverflow.com/questions/25661543/how-to-know-if-browser-tab-is-already-open-using-javascript sorry I have lot of questions – User014019 Sep 04 '14 at 09:14