2

I have a table with the odd and even rows with a different CSS style tr:nth-child(2n){...}, and when I filter them with a textbox and jQuery, I hide() all the rows except the ones that match my criteria.

The problem is that now the rows remain with the current style (as I assume they keep the position despite they can't be seen), so the new odd and even rows doesnt match the CSS pattern. How could I fix it?

1
  • Post some code please. Commented Nov 27, 2013 at 22:36

3 Answers 3

2

Try to follow this example:

jQuery('tr:visible').filter(':odd').css({'background-color': 'red'});
jQuery('tr:visible').filter(':even').css({'background-color': 'yellow'});

Check here:

http://jsfiddle.net/KSL7j/1/

Hope it helps

Update

You can check this other example with odd and row CSS classes.

As CAbbott suggested in this fiddle: http://jsfiddle.net/KSL7j/21/

Sign up to request clarification or add additional context in comments.

8 Comments

I created a css class-based variation on your answer here: jsfiddle.net/KSL7j/21
Yes but one thing is for sure that is not possible in pure CSS.
A javascript solution is needed and adding CSS classes odd and rows and then manipulate them with javascript seems a valid approach.
you can feel free to modify your answer with my example if you want. I didn't put it as an answer from me since it was just a variation of what you did.
How would you do to set no background-color? I prefer your answer that the one with the classes, but thanks to all of you guys :)
|
0

nth-child checks for the nth-child, not for the nth visible child or th nth whatever-styled child (hide() just adds display:none and nothing more...) and will never do.

I see two possible solutions:

1.add classes even/odd after filtering, just asking for the visible ones and then use your css on those classes

untested code:

var rows = $(tr[@display=block]);
rows.each(function(){
  var index = rows.index(this);
  if(index%2==0){
    $(this).addClass('even');
  }
  else{
    $(this).addClass('odd');
  }
}

2.really remove the rows, not just hiding them

1 Comment

Option 2 is not really an option for filtering. What if the user uses the backspace? How would you return the deleted rows?
0

when you use hide() it is just set the display to none.

the structure of the dom is not modify so the nth-child do not work as you expected

you need to remove the even tr to get the effect you want.

if you want reset the rows. you can hold them in a variable and restore them back

var rows = $("tr");
var even = rows.filter(":even");

$("#trigger").click(function () {
    even.hide();
    even.remove();
});

http://jsfiddle.net/R2gBt/

3 Comments

As I mentioned in Ria Elliger's post, deleting isn't a good option for simple table filtering.
I thought that solution, but removing the rows and adding back seemed quite "a lot of work" for such a thing as the table appearance
all solution has its tradeoff. you can choose the best to adjust your requirement. i think @alexoulu provides a good solution. you can modify to adjust your need. :)

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.