3

So I have this table:

<table border="1" align="center">
    <tr>
        <td>Broj_pu</td>
        <td>Naziv_pu</td>
        <td>ID</td>
        <td>Naselje</td>
        <td>zupanija</td>
    </tr>
    <tr>
        <td><input type="text" ID="broj_pu" onkeydown="Filter(document.getElementById('broj_pu').value,     'broj_pu')" /></td>
        <td><input type="text" ID="naziv_pu" onkeydown="Filter(document.getElementById('naziv_pu').value,   'naziv_pu')" /></td>
        <td><input type="text" ID="ID" onkeydown="Filter(document.getElementById('ID').value,           'ID')" /></td>  
        <td><input type="text" ID="naselje" onkeydown="Filter(document.getElementById('naselje').value,     'naselje')" /></td>
        <td><input type="text" ID="zupanija" onkeydown="Filter(document.getElementById('zupanija').value,   'zupanija')" /></td>    
    </tr>

    <tr class="row" ID="row_filter">
        <td>10000</td>
        <td>Zagreb</td>
        <td>1</td>
        <td>Sljeme</td>
        <td>ZAGREBACKA</td>
    </tr>

    <tr class="row" ID="row_filter">
        <td>10000</td>
        <td>Zagreb</td>
        <td>2</td>
        <td>Zagreb-dio</td>
        <td>ZAGREBACKA</td>
    </tr>
<!-- A lot of rows -->
...
</table>

And also I have started this JavaScript:

<script type="text/javascript">
    function Filter(text, column_name){
        var x = document.getElementByClassName("row");
        var i = 0;
        var y;

        if (text != ""){
            switch (column_name){
                case "broj_pu":
                for (i = 0; i < x.length; i++){
                    y = x[i].getElementByTagName("td");
                    if((y[0].value).match(text) == null){
                        x[i].attributes(style) = "{display:none;}";
                    }
                }
                break;

                case "naziv_pu":
                    y = x[i].getElementByTagName("td");
                    if((y[1].value).match(text) == null){
                        x[i].attributes(style) = "{display:none;}";
                    }
                }
                break;

                case "ID":
                    y = x[i].getElementByTagName("td");
                    if((y[2].value).match(text) == null){
                        x[i].attributes(style) = "{display:none;}";
                    }
                }
                break;

                case "naselje":
                    y = x[i].getElementByTagName("td");
                    if((y[3].value).match(text) == null){
                        x[i].attributes(style) = "{display:none;}";
                    }
                }
                break;

                case "zupanija":
                    y = x[i].getElementByTagName("td");
                    if((y[4].value).match(text) == null){
                        x[i].attributes(style) = "{display:none;}";
                    }
                }
                break;
            }
        }
    }
</script>

Now, I need to filter the table as the user inputs letters to the text fields, but I have no idea how to edit the display document as I enter the data.

Anyone have an idea?

EDIT1:

So I edited the script but it doesn't seem to work. What did I do wrong?

8
  • Can an answer include jQuery? Commented Apr 10, 2012 at 16:37
  • 3
    You should not have multiple HTML elements with the same IDs. The element's ID is supposed to be unique. Commented Apr 10, 2012 at 16:40
  • 1
    Don't edit the document, edit the styling of the document. So select the elements that don't match and add display:none; to their rows' style attribute. Commented Apr 10, 2012 at 16:41
  • i am totally unfamiliar with jQuery, so i would prefer an answer without it. As far as the ID goes, i dont know how to get the elements i want in any other way, if you know one please do tell :) Commented Apr 10, 2012 at 16:42
  • Also, you might have better luck using the onkeyup attribute. Or JQuery, for that matter. Commented Apr 10, 2012 at 16:42

2 Answers 2

6

This question is reminding me of how java script is nasty without any framework support :)

However I have sorted-out this issue for you ( tested on firefox 10.0.2).

check the complete working solution on jsfiddle

please remember this is just working example , you might need to write ALL-Browser compliant script .

script:

var filters=['hide_broj_pu','hide_naziv_pu','hide_ID','hide_naselje','hide_zupanija'];

function ExcludeRows(cls){

  var skipRows=[];

  for(i=0;i<filters.length;i++)
      if(filters[i]!=cls) skipRows.push(filters[i]);

  var pattern=skipRows.join('|')

  return pattern;
}

function Filter(srcField){

   var node=srcField.parentNode;

   var index=srcField.parentNode.cellIndex;
    //all the DATA rows

   var dataRows= document.getElementsByClassName("row");

   //ensure that dataRows do not have any filter class added already
   var kids= dataRows.length;

   var filter ='hide_'+srcField.id;

   var pattern = ExcludeRows(filter);

   var skipRow = new RegExp(pattern,"gi");

   var searchReg =new RegExp('^'+srcField.value,'gi');

   var replaceCls= new RegExp(filter,'gi');

   for(i=0; i< kids ; i++){
       //skip if already filter applied  

       if(dataRows[i].className.match(skipRow)) continue;

       //now we know which column to search
       //remove current filter
       dataRows[i].className=dataRows[i].className.replace(replaceCls,'');

       if(!dataRows[i].cells[index].innerHTML.trim().match(searchReg))
          dataRows[i].className=dataRows[i].className +' '+ filter;

    }



}

HTML

<table border="1" align="center">
<tr><td>Broj_pu</td><td>Naziv_pu</td><td>ID</td><td>Naselje</td><td>zupanija</td></tr>
<tr>
<td><input type="text" ID="broj_pu"    onkeydown="Filter(this)" /></td>
<td><input type="text" ID="naziv_pu"   onkeydown="Filter(this)" /></td>
<td><input type="text" ID="ID"         onkeydown="Filter(this)" /></td>  
<td><input type="text" ID="naselje"    onkeydown="Filter(this)" /></td>
<td><input type="text" ID="zupanija"   onkeydown="Filter(this)" /></td>   
</tr>

<tr class="row" ><td>10000</td><td>Zagreb</td><td>1</td><td>Sljeme</td><td>ZAGREBACKA</td></tr>
<tr class="row" ><td>10000</td><td>Zagreb</td><td>2</td><td>Zagreb-dio</td><td>ZAGREBACKA</td></tr>
</table>

CSS

.hide_broj_pu,
.hide_naziv_pu,
.hide_ID,
.hide_naselje,
.hide_zupanija
{display:none}
Sign up to request clarification or add additional context in comments.

4 Comments

btw, do you have a good framework to suggest for JS, HTMP, PHP and CSS? just started so im down to notepad++ and xampp for the server side stuff, but i could use a good framework.
All in one choose Yii , which has JQuery support
That works. But it is possible that it can work on href fields ? <td class="R0C1"><s:property value="prs"/> <a href="<s:url value='ur;/web/default/%{prs}'/>#scope_tab" target="_blank"></a>
1

For Javascript Table Search, Try:

<p>Search: <input type="text" id="searchTerm" onkeyup="doSearch()" /></p>

<table id="dataTable">

<script>
function doSearch() {
            var input, filter, found, table, tr, td, i, j;
            input = document.getElementById("searchTerm");
            filter = input.value.toUpperCase();
            table = document.getElementById("dataTable");
            tr = table.getElementsByTagName("tr");
            for (i = 0; i < tr.length; i++) {
                td = tr[i].getElementsByTagName("td");
                for (j = 0; j < td.length; j++) {
                    if (td[j].innerHTML.toUpperCase().indexOf(filter) > -1) {
                        found = true;
                    }
                }
                if (found) {
                    tr[i].style.display = "";
                    found = false;
                } else {
                    tr[i].style.display = "none";
                }
            }
        }
    </script>

Comments

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.