I often use javascript to set the class names of the rows after the page renders.
Here are many examples:
I often use javascript to set the class names of the rows after the page renders.
Here are many examples:
How is it "resorting to JavaScript", when you're already doing AJAX (AKA: JavaScript)?
If you were using the link that Greg posted to handle doing the alternation on non AJAXy pages, I can definitely see why you'd want to avoid that (I would, too). On an AJAXy page, it seems, to me, like there's no problem with this solution.
Thanks
The link is greatly appreciated! But I was hoping for a solution from within rails really,
Are you aware Rails _comes_ with Prototype and Scriptaculous as built-in javascript libraries? Any Rails helper you find that has "remote" in the name uses javascript.
mainly for my learning purposes, I apprecaite doing it that way isn't really going to cause any harm, but there most be a solution to the problem without resorting to javascript?
It's 2009, it's ok to use javascript.. seriously.
I guess my real question is,
Why does the cycle reset on subsequent renderings of the partial? Doing it with JS is fine, I'm more curious as to why this doesn't work though. Apologies if my intent wasn't too clear in the original post!
I shall just do it using JS for now anyway, but would love to know whats
going on behind the scenes in the example I gave ![]()
If you want it to know what style/class to draw next you need a better cycle() method, one that tracks it's own state across multiple requests. I have this alt method in one of my Rails apps:
def alt( s='', s2=' class="alt-row"' ) session[:alt] ||= '1' session[:alt] = session[:alt] == '1' ? '2' : '1' session[:alt] == '1' ? s2 : s end
Usage is just: <tr<%= alt %>
But using jQuery is just as easy:
$("tr:nth-child(odd)").addClass("odd");
Nice thanks!
I'm guessing different http requests cause a new cycle object to be instantiated meaning it will be back to the inital style? I couldn't find an answer within the API.
Anyway that's great I shall implement my own cycle as per your advice, cheers!
Remember that cycle() is executed in the server as a result of your ajax call to it. It has no way of knowing what state the cycle was at from the last call. In the extreme it might not even be the same server as last time (though probably this is not the case here). You could save the state in the session as suggested by another post or you could pass the current state as a parameter to the ajax call, or probably better something that indicates the row number from which the required class can be determined.
Colin