Skip to content

Instantly share code, notes, and snippets.

@upinetree
Created January 21, 2014 12:41
Show Gist options
  • Save upinetree/bfab3ee679af210fefc1 to your computer and use it in GitHub Desktop.
Save upinetree/bfab3ee679af210fefc1 to your computer and use it in GitHub Desktop.
テキストをクリックして入力フォームを出現させる (rails+haml+coffee)

一覧画面とかで、表示されているテキストクリックしたら入力フォームが出現して、 サクッと内容変更できたらカッコ良いじゃないですか。

というわけで毎度のメモです。

haml

#posts
  - @posts.each do |post|
    .title
      .value= post.value
      = text_field :post, :title, class: :field

scss

.title {
  .value {
    display: inline;
  }
  .field {
    display: none;
  }
}

coffee

$('.value').on('click', ->
  $(@).hide()
  $(@).next('.field').show()
                     .val($(@).text())
                     .focus()
)

$('.field').on('blur', ->
   # $.ajax
   #   hogehoge

   $(@).hide()
   $(@).prev('.value').show()

# 直感的にはEnterで確定したいので作ったる
$('.label_field').keypress (e) ->
  if e.which == 13
    e.target.blur()

ちなみに

単純なテキストの切り替えだったらこんな感じ。

#toggle_me
  #showing.toggle_pair Toggle Me!!
  #hidden.toggle_pair Toggled!!

:scss
  #hidden {
    display: none;
  }
$('#toggle_me').on 'click', ->
  $(@).children('.toggle_pair').toggle()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment