In a rails 3 app using Jquery and JqueryUI, I’m rendering a partial to display an accordion list of Items. I wish to call a controller action (let’s call it the Item#get_details action) to retrieve additional details via a web service about the Item upon the expansion of the appropriate accordion component.
The collection of items passed into the partial contains a description and an item number, as such:
render :partial => 'candidates' , :collection => @candidates
#Candidates is a list of hashes, each hash containing keys "Desc" and "id_number"
Each pass through the partial renders markup which jqueryUI renders as an accordion section, as below:
<h3><%= candidate[:desc] %>
<div id="candidate_details">
</div>
What is the best practice for binding a controller call to the <h3> which will retrieve details and populate the “candidate_details” div upon activation of the accordion item?
Thanks in advance.
First of all, I’d close that
<h3>. Then use a data attribute on the<h3>to hold theid_number:Sounds like you want your accordion to start out with everything closed so you’d use something like this to bind the accordion:
The
collapsibleoption allows all the panels to be closed at once, theautoHeightsetting allows the panels to resize to fit their content, and theactivesetting starts them all out closed.Then, you could use
oneto bind a click handler to the<h3>s that will remove itself after its first activation. That click handler can dig out thedata-idvalue and do an AJAX call to get the content for the corresponding<div>:Then your controller would use the
idto look up the necessary content and send it back as a little blob of HTML.Demo of the jQuery-UI side: http://jsfiddle.net/ambiguous/BhJYv/