Skip to content Skip to sidebar Skip to footer

Jquery Filtering By Two Values

This time I have a problem with jQuery filtering. $( document ).ready(function() { $('#search').keyup(function() { var s = new RegExp(this.value); $('#support

Solution 1:

The simple answer is to add :visible to the selector:

$('select#sel-supporttask-projects').change(function() {
    var s = newRegExp(this.value);
    $('#support-tasks-table tbody tr:visible').each(function() {
        if(s.test(this.cells[3].innerHTML)) $(this).show();
        else $(this).hide();
    });
});

But it highlights a design flaw: if you select a different option, you would want some answers that were previously invisible to become visible.

Essentially, you need to have a searchParameters class that includes both search filters:

var searchParameters = {
    supportTask: null,
    searchRegex: null
};

functionshouldRowBeVisible(row) {
   if(searchParameters.supportTask) {
      if(!(searchParameters.supportTask.test(row.cells[3].innerHTML))) {
          returnfalse;
       }
   }
   if(searchParameters.searchRegex) {
                if(!(searchParameters.searchRegex.test(row.cells[3].innerHTML))) {
          returnfalse;
       }
   }
   returntrue;
}


functionupdateVisibility() {
   $('#support-tasks-table tbody tr').each(function() {
       if(shouldRowBeVisible(this) {
         $(this).show();
       } else {
         $(this).hide();
       }
    });
}

 $('#search').keyup(function() {
     searchParameters.searchRegex = newRegExp(this.value);
     updateVisibility();
});
$('select#sel-supporttask-projects').change(function() {
    searchParameters.supportTask = newRegExp(this.value);
    updateVisibility();
});

Post a Comment for "Jquery Filtering By Two Values"