使用Om的bootstrap下拉列表

时间:2014-07-12 01:47:12

标签: twitter-bootstrap-3 clojurescript om

这就是我所拥有的:

(defn view [cursor owner]
  (reify
    om/IDidMount
    (did-mount [_]
      (-> (js/$ ".dropdown-toggle")
          (.dropdown)))

    om/IRender
    (render [_]
      (dom/div #js {:className "dropdown"}
               (dom/button #js {:className "btn btn-default dropdown-toggle"
                                :type "button"
                                :id "dropdownMenu1"} "Dropdown" (dom/span #js {:className "caret"}))
               (dom/ul #js {:className "dropdown-menu"
                            :role "menu"
                            :ariaLabelledby "dropdownMenu1"}
                       (dom/li #js {:role "presentation"}
                               (dom/a #js {:role "menuitem"
                                           :tabIndex "-1"
                                           :href "#"} "Action"))
                       (dom/li #js {:role "presentation"}
                               (dom/a #js {:role "menuitem"
                                           :tabIndex "-1"
                                           :href "#"} "Another action")))))))

问题在于,一旦打开下拉列表,它就不再隐藏了,因为当它点击它或其他地方时应该是这样。击键也不起作用。我相信这里缺少一些重要的东西,它会是什么?我正在使用bootstrap 3.1.1和jquery 1.11.0。

感谢。

2 个答案:

答案 0 :(得分:8)

以下是我创建下拉组件的方法:

(defn dropdown [cursor owner {:keys [id text values]}]
 (om/component
   (html
     [:div.dropdown
       [:button {:type "button"
                 :class "btn dropdown-toggle"
                 :data-toggle "dropdown"
                 :id id}
                text
                [:span {:class "caret"}]]
       [:ul {:class "dropdown-menu" :role "menu" :aria-labelledby id}
         [:li {:role "presentation"}
           (for [v values] 
             [:a {:role "menuitem" :tabIndex "-1" :href "#"} v])]]])))

隐藏它应该的时候。为了清楚起见,我使用jQuery 1.11.1,Bootstrap 3.2.0和sablono,但这不会影响任何内容。我认为您不应该将IDIDMount用于jQuery,因为所有交互都是通过bootstrap的dropdown JavaScript插件(包含在Bootstrap库中)来处理的。

答案 1 :(得分:4)

另一种选择是使用我写的Om-Bootstrap库;有一个下拉组件,可以在内部处理所有这种状态。

下拉列表变为:

(:require [om-bootstrap.button :as b])

(b/toolbar
 {}
 (for [title ["Default" "Primary" "Success" "Info" "Warning" "Danger" "Link"]
       :let [style (.toLowerCase title)]]
   (b/dropdown {:bs-style style, :title title}
               (b/menu-item {:key 1} "Action")
               (b/menu-item {:key 2} "Another action")
               (b/menu-item {:key 3} "Something else here")
               (b/menu-item {:divider? true})
               (b/menu-item {:key 4} "Separated link"))))

http://om-bootstrap.herokuapp.com上的文档网站提供了有关如何使用所有这些组件的示例和代码段。