为什么我可以按int而不是字符串过滤?

时间:2015-12-10 08:34:00

标签: javascript html

现在我的代码可以通过int过滤:

<input name='tablefilter' type='checkbox' value='1' id='tablefilter1' checked/>
<label for='tablefilter1'>1</label>
<input name='tablefilter' type='checkbox' value='2' id='tablefilter2' checked/>
<label for='tablefilter2'>2</label>
<input name='tablefilter' type='checkbox' value='3' id='tablefilter3' checked/>
<label for='tablefilter3'>3</label>
<table>
  <thead>
    <tr>
      <th>Col1</th>
      <th>Col2</th>
      <th>Col3</th>
    </tr>
  </thead>
  <tbody id='tablebody'>
    <tr>
      <td>1</td>
      <td>One</td>
      <td>First</td>
    </tr>
    <tr>
      <td>2</td>
      <td>Two</td>
      <td>Second</td>
    </tr>
    <tr>
      <td>3</td>
      <td>Three</td>
      <td>Third</td>
    </tr>
  </tbody>
</table>

JS

/* Demo filtering table using checkboxes. Filters against first td value */

/* Set 'ready' handler' */
document.addEventListener('DOMContentLoaded', initFunc);

/* When document ready, set click handlers for the filter boxes */
function initFunc(event) {
  var filters = document.getElementsByName('tablefilter');
  for (var i = 0; i < filters.length; i++) {
    filters[i].addEventListener('click', buildAndExecFilter);
  }
}

/*
    This function gets called when clicking on table filter checkboxes.
    It builds a list of selected values and then filters the table based on that
*/
function buildAndExecFilter() {
  var show = [];
  var filters = document.getElementsByName('tablefilter');
  for (var i = 0; i < filters.length; i++) {
    if (filters[i].checked) {
      show.push(filters[i].value);
    }
  }
  execFilter(show); // Filter based on selected values
}

function execFilter(show) {
  /* For all rows of table, see if td 0 contains a selected value to filter */
  var rows = document.getElementById('tablebody').getElementsByTagName('tr');
  for (var i = 0; i < rows.length; i++) {
    var display = ""; // Default to display
    // If it is not found in the selected filter values, don't show it
    if (show.indexOf(rows[i].children[0].textContent) === -1) {
      display = "none";
    }
    // Update the display accordingly
    rows[i].style.display = display;
  }
}

http://jsfiddle.net/2Lm7pytt/3/

但是,该过滤器无法按字符串过滤。例如,如果我想使用“one”而不是1,那就不行了。

有谁知道解决方案的原因和解决方案?

谢谢

2 个答案:

答案 0 :(得分:1)

execFilter()方法的这些行,

 if (show.indexOf(rows[i].children[0].textContent) === -1) {
      display = "none";
    }

仅比较索引0,它是数值而不是其他列。

除非您将值与所有列(rows[i].children的所有索引)进行比较,否则它不会为您提供所需的结果。

因此,您可能无法运行for循环来遍历rows[i].children的所有子项并比较它们的文本。

var foundResult = false;
for ( var counter = 0; counter < rows[i].children.length; counter++ )
{
   if (show.indexOf(rows[i].children[0].textContent) != -1) 
   {
       foundResult= true;
       break;
   }
}
if ( !foundResult )
{
   display = 'none';
}

答案 1 :(得分:1)

你的意思是这样的

var flt = ["zero","one","two","three"];
...
var showIt = show.indexOf(rows[i].children[0].textContent) !=-1;
for (var j=0;j<show.length;j++) {
  if (flt[show[j]] == rows[i].children[1].textContent) {
    showIt=true;
    break;
  }
}
rows[i].style.display = showIt?"":"none";