artimys
4/14/2017 - 5:27 PM

Nice Way to toggle between forms and change link text

Nice Way to toggle between forms and change link text

	# Clone
	# -------------------------------------------------------------------------
	# make sure $(".clone_pricesheet_forms") are hidden
	$clone_pricesheet_links = $("a.clone_pricesheet_links").click (e) ->
		e.preventDefault()
		$link = $(@)
		$form = $link.next()

		# Remove current form that is selected so we can hide the rest
		# Makes toggling the current form easy
		$(".clone_pricesheet_forms").not( "##{$form.attr('id')}" ).hide()

		$form.toggle ->
			if $(@).is(':visible')
				$link.text 'Cancel'
			else
				$link.text 'Clone'
			return
			<% pricesheets.each do |pricesheet| %>
			<tr>
				<td>
					<%= link_to "Clone", "", class: "clone_pricesheet_links" %>

					<%= form_tag clone_pricesheet_url(pricesheet), method: :get, id: "clone_pricesheet_form_#{pricesheet.id}", class: "clone_pricesheet_forms" do %>
						<%= select_tag :pl_id, options_for_select(Pricelevel.all.map { |p| [p.name, p.id] }) %>
						<%= select_tag :pl_year, options_for_select(PricelevelYear.pluck(:year)) %>
						<%= submit_tag "Proceed to CLONE" %>
					<% end %>
				</td>
				<td><%= pricesheet.id %></td>
			</tr>
			<% end %>