3

Though I don't doubt this has been answered I cannot find a great match for my question.

I have a table for which I'd like to filter rows based on whether or not they contain a hidden field matching a value.

I understand that the technique tends to be "show all rows", "filter the set", "show/hide that filtered set"

I have the following jquery but I'm aweful with filter and my filtered set seems to always contain no elements.

my table is the usual

<table>
<tr><td>header></td><td>&nbsp;</tr>
<tr>
<td>a visible cell</td><td><input type='hidden' id='big-asp.net-id' value='what-im-filtering-on' />
</td>
</tr>
</table>

My goal is to be able to match on tr who's descendent contains a hidden input containing either true or false.

this is how I've tried the selector (variations of this) and I'm not even testing for the value yet.

function OnFilterChanged(e){
    //debugger;
    var checkedVal = $("#filters input[type='radio']:checked").val();
    var allRows = $("#match-grid-container .tabular-data tr");
    if(checkedVal=="all"){        
         allRows.show();
    }
    else if(checkedVal=="matched"){
         allRows.show();
         allRows.filter(function(){$(this).find("input[type='hidden'][id~='IsAutoMatchHiddenField']")}).hide();

    }
    else if(checkedVal=="unmatched"){

    }
}

Am I way off with the filter? is the $(this) required in the filter so that i can do the descendant searching?

Thanks kindly

Building upon those great suggestions below I have found that the following does the trick. I'd missed the fact that the filter closure function must return true/false based on the filter condition. Also, that the ends-with selector is great for asp.net generated ids based on INamingContainer

allRows.show();
allRows.filter(function(){
            return $(this).find(
               "input[type='hidden'][id$='IsAutoMatchHiddenField']").val() == "False";
         }).hide();
Famous Nerd
  • 416
  • 4
  • 12

3 Answers3

3
$('#mySelector :hidden').filter(
    function (index)
    {
        return $(this).find('.repeatedObject').val() == 'someValue';
    }
).hide();

The filter() function needs a boolean to be returned to actually determine whether or not to leave an element in the list. Check the API (http://api.jquery.com/filter/) for more information.

Also, as a sidenote, the val(), html(), text(), and other related functions return the information from the first element in the set. If you want to loop through, you'd have to use each or a for loop.

Jeff Rupert
  • 4,690
  • 2
  • 20
  • 17
  • Wonderful, thank you for that crucial piece of info. I feel noobish. Also, for people doing asp.net if you want to do ID ends with please refer to this: http://stackoverflow.com/questions/609382/jquery-selector-id-ends-with – Famous Nerd Apr 15 '10 at 21:09
  • You're welcome! Don't worry about feeling noobish, happens to me on a weekly basis at work. A mistyped path, forgetting to type `return`... Thankfully my co-workers don't make fun of me for those mistakes. =) – Jeff Rupert Apr 15 '10 at 23:28
2

Couple of suggestions.

  1. The find function needs to return a boolean.
  2. Are you looking for an id? or looking for the value? [id~='IsAutoMatchHiddenField']
  3. The [attribute~=value], will look for a word in the value separated by whitespace, example: [value~='foo'] will not match value="foo-bar" but will match value="foo bar".

.

// Chain your functions, the beauty of jQuery.
allRows.show()
   .filter(function(index){
      // function needs to return a boolean.
      return $(this)
         .find("input[type='hidden'][id~='IsAutoMatchHiddenField']")
         .val() == 'valuetocheck';
   });
guzart
  • 3,700
  • 28
  • 23
1

I think you need to return a boolean or equivalent from the filter function. How about:

allrows.filter(function() {
    return $(this).find(':hidden').length;
}).hide();

Or:

$('tr :hidden').closest('tr').hide();
David Hellsing
  • 106,495
  • 44
  • 176
  • 212
  • I believe I have just discovered that I'd missed that crucial point. thank you.. filter acts like Predicate in C# – Famous Nerd Apr 15 '10 at 21:08