Rails视图中的Bootstrap Active选项卡

时间:2015-06-07 22:39:01

标签: javascript jquery ruby-on-rails twitter-bootstrap haml

Tab Will Not Change Active State

我在让BS标签更改为活动状态时遇到了一些困难

index.html.haml

- if @tabs
%ul#tabs.nav.nav-tabs{:role => "tablist"}
  %li.active{:role => "presentation"}
    = link_to "All", search_jobs_path(tab: "ALL"), {"aria-controls" => "home", :role => "tab"}
  - @tabs.each do |tab|
    %li{:role => "presentation"}
      = link_to tab, search_jobs_path(tab: tab), {"aria-controls" => "home", :role => "tab"}
  :javascript
    $('#tabs').click(function (e) {
      e.preventDefault()
      $(this).tab('show')
    })
.tab-content
= render partial: 'jobs/list', locals: { jobs: @jobs }

我已尝试关注BS guide,但无法让它发挥作用。我尝试用data-toggle换出data-target。我完全删除了data-toggle。当我将鼠标悬停在另一个标签上时,我所做的最好的就是显示阴影;但是,它会继续显示第一个标签为活动标签。

3 个答案:

答案 0 :(得分:0)

您链接的指南部分仅显示如何在单击选项卡时更改内容。为了使标签本身能够正确绘制,无论何时切换标签,都需要在新标签页上设置{{1}}类,并将其从不再活动的标签中删除。

您可以尝试这样的事情:

{{1}}

答案 1 :(得分:0)

data-toggle="tab"添加到您的<a>

并将$('#tabs').click更改为$('#tabs a').click

此处的工作示例:http://jsfiddle.net/killerbytes/syrywhkn/

答案 2 :(得分:0)

原来它呈现相同的局部。使用ajax渲染视图解决了这个问题。

修复:

index.html.haml:

%ul.accordion-tabs
%li.tab-header-and-content
  %a.tab-link.is-active{:href => "/jobs/search?tab=ALL"} All
  .tab-content{:data => { :url => "/jobs/search?tab=ALL&inline=true" }}
    = render partial: 'jobs/list', locals: { jobs: @jobs }
    .loading{style: 'display:none;'}
      = image_tag "spinner.gif"
      %p Loading
- @tabs.each do |tab|
  %li.tab-header-and-content
    %a.tab-link{:href => "/jobs/search?tab=#{tab}"} #{tab.capitalize}
    .tab-content{:data => { :url => "/jobs/search?tab=#{tab}&inline=true" }}
      .loading
        = image_tag "spinner.gif"
        %p Loading

jobs_controller.rb:

if params[:inline]
  render partial: 'jobs/list', locals: { jobs: @jobs }, layout: false
else
  render :index
end

jobs.js.coffee:

$(document).on 'page:change', ->
  $('.accordion-tabs').each (index) ->
    $(this).children('li').first().children('a').addClass('is-active').next().addClass('is-open').show()
  $('.accordion-tabs').on 'click', 'li > a.tab-link', (event) ->
    accordionTabs = undefined
    nextTab = undefined
    if !$(this).hasClass('is-active')
      event.preventDefault()
      accordionTabs = $(this).closest('.accordion-tabs')
      accordionTabs.find('.is-open').removeClass('is-open').hide()
      nextTab = $(this).next()
      nextTab.toggleClass('is-open').toggle()
      accordionTabs.find('.is-active').removeClass 'is-active'
      $(this).addClass 'is-active'
      nextTab.find('.loading').show()
      nextTab.find('.loading p').text 'Loading'
      $.ajax
        url: nextTab.data('url')
        success: (data) ->
          nextTab.html data
          nextTab.find('.loading').hide()
        error: ->
          nextTab.find('.loading p').text 'Error'
    else
      event.preventDefault()