6

I'm trying to dynamically change (if it got clicked) a normal table header (which is a link) to another defined CSS class 'th.hilite'. This link simply sorts this column and the header should got highlighted every time a user sorts the list.

The view where the class in question should be changed, looks like this:

%table#mytable
  %thead
    %tr
      %th= link_to 'Title', mytable_path(:sort => 'title'), :id => 'title_header'

My question is simply: How and where could I dynamically set the class to %th.hilite if the header is clicked?

5
  • You have to do this in Javascript, not in ruby. Commented Mar 10, 2012 at 13:28
  • 3
    You don't have to and shouldn't use JavaScript to do this. Besides that you ought do the SaaS homework #2 by yourself. Note: The presented answer is correct. Commented Mar 10, 2012 at 19:41
  • 20
    You should not be posting questions directly from the saas-class.org homework 2 assignment here. You should be asking how to do something.. but not for the exact answer to your question. Commented Mar 10, 2012 at 22:01
  • 7
    Most importantly you should NOT have posted the code above that directly shows how to answer part of the question. Its just bad behaviour. Commented Mar 10, 2012 at 22:29
  • 8
    It's pretty disappointing that when I search for information about conditional rendering in haml that this is the first link on google. Everyone who signed up for that course agreed to the honour code, it's just a shame that some people don't have any personal honour anymore. Commented Jun 5, 2012 at 15:18

5 Answers 5

26

You can bind it from the view directly:

%th{:class => ("hilite" if @sort == "title")}= link_to 'Movie Title'...

However, you will also have to declare the @sort instance variable in your controller, and set it to either title or release, according to which column you are sorting. (i.e. @sort = params[:sort])

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

1 Comment

actually you dont have to declare an instance variable, you can simply use params[:sort]
14

Be careful not to put logic (even conditionals) in your views if you can find a way around it. In order to avoid this common mistake, you need to make good use of the params and session hashes and set appropriate variables in controllers

# In your view
%th{:class => @title_header}= link_to 'Title', my_path(:sort => 'title'), :id => 'title_header'

# In your controller
sort = params[:sort] || session[:sort]
if sort == 'title'
  ordering = {:order => :title}
end

2 Comments

If you are wondering why the ||session[:sort], it is because you may click on something other than a sortable column, in which case you do not want sorting to become undone because your params hash has expired. You will also need a little logic in controller like "if params[:sort] != session[:sort] then session[:sort] = sort" type of thing
I'm trying to understand this answer. Where in the docs can I read about "what?" to understand how the title_header can have different values for each th?
3

BTW as long as values of your param match column name - no need to code each one

def index
    @by=params[:by]
    @movies = Movie.order(params[:by]).all
  end

Comments

3

Javascript or jquery is not required..

The following HAML will work

%th{:class=>('title' == @sortby)?'hilite':""}= link_to  'Movie Title', movies_path(:sort => 'title'), :id => 'title_header'

Comments

-2

You should use JavaScript to do this. I think it's good idea for you to use jQuery instead of pure JavaScript. Here are some examples

http://api.jquery.com/click/

http://api.jquery.com/css/

1 Comment

Neither JavaScript nor jQuery is required for this.

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.