Filtered table

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Seth

    #1

    Filtered table

    How can I hide an entire row of a table if one of the cells in that row is
    equal to a value drop down box value.

    My goal is to have a filtered table so that each cell in each row determines
    if it should be hidden by drop down boxes. Thanks for your help.


  • Evertjan.

    #2
    Re: Filtered table

    Seth wrote on 18 apr 2004 in comp.lang.javas cript:
    [color=blue]
    > How can I hide an entire row of a table if one of the cells in that
    > row is equal to a value drop down box value.
    >
    > My goal is to have a filtered table so that each cell in each row
    > determines if it should be hidden by drop down boxes. Thanks for your
    > help.[/color]


    <table border=1>
    <tr id=tr1><td id=td1>qwerty</td><td>more</td></tr>
    <tr id=tr2><td id=td2>qwerty</td><td>more</td></tr>
    <tr id=tr3><td id=td3>blah</td><td>more</td></tr>
    </table>
    <br>

    Do not show:
    <select onChange=doHide (this); >
    <option selected>none</option>
    <option>blah</option>
    <option>qwert y</option>
    </select>



    <script>
    function doHide(that){
    testvalue = that.options[that.selectedIn dex].innerHTML
    for(i=1;i<4;i++ ){
    trps = document.getEle mentById("tr"+i ).style
    tdp = document.getEle mentById("td"+i )
    trps.display=(t dp.innerHTML==t estvalue)?"none ":""
    }
    }
    </script>

    IE6 tested


    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)

    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: Filtered table

      "Seth" <seth@doesnotex ist.com> writes:
      [color=blue]
      > How can I hide an entire row of a table if one of the cells in that row is
      > equal to a value drop down box value.[/color]

      To hide a row, set its visibility to hidden (which leaves its space
      blank) or its display to none (which completely removes the row).

      ---
      <table id="table">
      <tr><td>foo</td><td>bar<td></tr>
      <tr><td>foo</td><td>baz<td></tr>
      <tr><td>doo</td><td>bar<td></tr>
      <tr><td>doo</td><td>baz<td></tr>
      </table>

      <lable for="col1sel">C olumn 1:
      <select id="col1sel"
      onchange="filte rTable('table', 0,
      this.options[this.selectedIn dex].value);">
      <option selected="selec ted">None</option>
      <option value="foo">Fil ter out "foo"</option>
      <option value="doo">Fil ter out "doo"</option>
      </select>
      </label>

      <script type="text/javascript">
      function filterTable(tab le,column,value ) {
      var tableRef = document.getEle mentById(table) ;
      var rows = tableRef.rows;
      if (value) { // for partial matching
      var valueRE = new RegExp(value);
      }
      for(var i=0,n=rows.leng th;i<n;i++) {
      var row = rows.item(i);
      if (!value) {
      // set default, block in IE, table-row in standards
      row.style.displ ay = "";
      } else {
      var cell = row.cells.item( column);
      var cellText = cell.firstChild .nodeValue;
      if (valueRE.test(c ellText)) {
      row.style.displ ay = "none";
      } else {
      row.style.displ ay = "";
      }
      }
      }
      }
      </script>
      ---
      This assumes that the table cells contains only text, not HTML, because that
      makes it easier to extract the text content.
      It also doesn't generaliz very well to filtering on more than one column,
      because it sets the display property directly (so unhiding on cell in
      a column will make its row visible, even though it might have been hidden
      by another column too).
      [color=blue]
      > My goal is to have a filtered table so that each cell in each row determines
      > if it should be hidden by drop down boxes. Thanks for your help.[/color]

      For this you need to remember the hidden state for each row, so if a row is
      hidden by more than one cell, it has to be unhidden by all of them.
      ---
      <table id="table">
      <tr><td>foo</td><td>bar<td></tr>
      <tr><td>foo</td><td>baz<td></tr>
      <tr><td>doo</td><td>bar<td></tr>
      <tr><td>doo</td><td>baz<td></tr>
      </table>

      <lable for="col1sel">C olumn 1:
      <select id="col1sel"
      onchange="filte rTable('table', 0,
      this.options[this.selectedIn dex].value);">
      <option selected="selec ted">None</option>
      <option value="foo">Fil ter out "foo"</option>
      <option value="doo">Fil ter out "doo"</option>
      </select>
      </label>

      <lable for="col2sel">C olumn 2:
      <select id="col2sel"
      onchange="filte rTable('table', 1,
      this.options[this.selectedIn dex].value);">
      <option selected="selec ted">None</option>
      <option value="bar">Fil ter out "bar"</option>
      <option value="baz">Fil ter out "baz"</option>
      </select>
      </label>

      <script type="text/javascript">
      // format : map(tableId->array(object(h idNum:int,hidCo l:array(bool))) )
      var tableFilterData = {};
      function filterTable(tab leId,column,tex t) {
      var tableRef = document.getEle mentById(tableI d);
      var filterData = tableFilterData[tableId];
      if (!filterData) { // create if first time
      filterData = tableFilterData[tableId] = [];
      }
      var rows = tableRef.rows;
      if (text) {
      var textRE = new RegExp(text);
      }
      for (var i=0,n=rows.leng th;i<n;i++) {
      var row = rows.item(i);
      var rowData = filterData[i];
      if (!rowData) {
      rowData = filterData[i] = {hidNum:0,hidCo l:[]};
      }
      var nowHide = false;
      if (text) {
      var cell = rows.item(i).ce lls.item(column );
      var cellText = cell.firstChild .nodeValue;
      newHide = textRE.test(cel lText);
      }
      if (newHide != !!rowData.hidCo l[column]) {
      rowData.hidCol[column] = newHide;
      if (newHide) {
      rowData.hidNum ++;
      } else {
      rowData.hidNum --;
      }
      row.style.displ ay = ((rowData.hidNu m == 0) ? "" : "none");
      }
      }
      }
      </script>
      ---
      This script works with multiple tables and columns, but is not stable
      against changes in the table structure. If that is needed, you can
      try putting the rowData object on the row element itself, i.e., change
      var rowData = filterData[i];
      if (!rowData) {
      rowData = filterData[i] = {hidNum:0,hidCo l:[]};
      }
      to
      var rowData = row.filterData;
      if (!rowData) {
      rowData = row.filterData = {hidNum:0,hidCo l:[]};
      }
      It requires you to be able to add properties to the rowElement, which
      is most likely possible, but not required by the standard (it's a host
      object, so it can behave almost arbitrarily).

      Good luck.
      /L
      --
      Lasse Reichstein Nielsen - lrn@hotpop.com
      DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
      'Faith without judgement merely degrades the spirit divine.'

      Comment

      Working...