Skip to content

Instantly share code, notes, and snippets.

@mnishiguchi
Last active January 10, 2016 21:00
Show Gist options
  • Save mnishiguchi/dc24c4ee840614498be1 to your computer and use it in GitHub Desktop.
Save mnishiguchi/dc24c4ee840614498be1 to your computer and use it in GitHub Desktop.
Bootstrap3 tabs - コントローラに応じて、activeタブを変更する。 ref: http://qiita.com/mnishiguchi/items/fbcfc1721787395fa6f2
-# デフォルト:#tabs_table_all_itemsがactive.
-# MovingItemsControllerからレンダリングされた場合のみ例外的に#tabs_add_formをactiveにする。
-# クラスのリスト
- tab_active = "active"
- tab_default = ""
- content_default = %w[tab-pane fade]
- content_active = %w[tab-pane fade active in]
-# クラス交換機
- if params[:controller] == 'moving_items'
- tab_add_form = tab_active
- tab_table_all_item = tab_default
- content_add_form = content_active
- content_table_all_item = content_default
- else
- tab_add_form = tab_default
- tab_table_all_item = tab_active
- content_add_form = content_default
- content_table_all_item = content_active
/ タブ
%ul{ class: "nav nav-tabs", role: 'tablist' }
%li{ class: tab_table_all_item }
%a{ href: "#tabs_table_all_items", role: "tab", data: { toggle: "tab" } }
All items
%li{ class: tab_add_form }
%a{ href: "#tabs_add_form", role: "tab", data: { toggle: "tab" } }
Add item
/ タブコンテンツ
/ .fade : アニメーションを有効にする。
/ .in : ページが読み込まれた時に表示する。
.tab-content
#tabs_table_all_items{ class: content_table_all_item }
- if @moving_items.count > 0
= render 'moving_items/table_moving_items'
- else
%p{ style: "margin: 16px;" } No item found
#tabs_add_form{ class: content_add_form }
= render 'moving_items/add_form'
/ タブ
%ul{ class: "nav nav-tabs", role: 'tablist', id: "tabs_movings" }
%li{ class: "active" }
%a{ href: "#tabs_1", role: "tab", data: { toggle: "tab" } }
All items
%li
%a{ href: "#tabs_2", role: "tab", data: { toggle: "tab" } }
Add item
/ タブコンテンツ
.tab-content
#tabs_1{ class: "tab-pane fade active in" }
- if @moving_items.count > 0
= render 'moving_items/table'
- else
%p No item found
#tabs_2{ class: "tab-pane fade" }
= render 'moving_items/form'
:coffee
// タブ交換機 in JavaScript
// ページが読み込まれた後、2番目のタブをactiveにする。
jQuery ->
if #{ params[:controller] == 'moving_items' }
$('#tabs_movings li:eq(1) a').tab('show')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment