[英]How to get a DoubleClicked event from a reflex-dom listbox
The following code displays a reflex-dom dropdown element visually as a listbox and displays at the bottom always the last selected (clicked) line. 以下代码在视觉上以列表框形式显示reflex-dom下拉元素,并在底部始终显示最后选择(单击)的行。
{-# LANGUAGE OverloadedStrings #-}
import Reflex.Dom
import qualified Data.Text as T
import qualified Data.Map as Map
import Data.Monoid((<>))
import Data.Maybe (fromJust)
main :: IO ()
main = mainWidget $ el "div" $ do
dd <- dropdown "2" (constDyn countries) $ def & attributes .~ constDyn ("size" =: "10")
el "p" $ return ()
let selItem = result <$> value dd
dynText selItem
return ()
countries :: Map.Map T.Text T.Text
countries = Map.fromList [("1", "France"), ("2", "Switzerland"), ("3", "Germany"), ("4", "Italy"), ("5", "USA")]
result :: T.Text -> T.Text
result key = "You selected: " <> fromJust (Map.lookup key countries)
I want to change this code, so it displays at the bottom always the last double clicked line! 我想更改此代码,因此它始终显示最后双击的行!
I tried several things 我尝试了几件事
Question: How can I get at the double click event? 问题:如何获得双击事件?
You can use domEvent
to get at the double click. 您可以使用
domEvent
来双击。
The following code uses elAttr
to create a listbox like the one you created with dropdown. 以下代码使用
elAttr
创建一个列表框,就像您使用下拉列表创建的列表框一样。 The domEvent
function is used to create double click Event
's for each of the listbox options which are then combined to get a Dynamic
that represents the most recently double clicked option. domEvent
函数用于为每个列表框选项创建双击Event
,然后组合这些事件以获得表示最近双击选项的Dynamic
。
I left the dropbox code in place for comparison purposes. 为了进行比较,我保留了Dropbox代码。
{-# LANGUAGE OverloadedStrings #-}
import Reflex.Dom
import qualified Data.Text as T
import qualified Data.Map as Map
import Data.Monoid((<>))
import Data.Maybe (fromJust)
import Data.Traversable (forM)
-- a listbox that responds to double clicks
listbox :: MonadWidget t m => T.Text -- default
-> Map.Map T.Text T.Text -- entries
-> Map.Map T.Text T.Text -- attributes
-> m (Dynamic t T.Text)
listbox def entries attr = do
optEv <- elAttr "select" attr $
forM (Map.toList entries) $ \(i,c) -> do
let sel = if i == def
then "selected" =: "selected"
else mempty
(e, _) <- elAttr' "option" sel $ text c
return (i <$ domEvent Dblclick e)
holdDyn def $ leftmost optEv
main :: IO ()
main = mainWidget $ el "div" $ do
-- original code (responds to single clicks)
dd <- dropdown "2" (constDyn countries) $ def & attributes .~ constDyn ("size" =: "10")
el "p" $ return ()
let selItem = result <$> value dd
dynText selItem
el "p" $ return ()
-- new code (responds to double clicks)
lb <- listbox "3" countries ("size" =: "10")
el "p" $ return ()
let dblItem = result <$> lb
dynText dblItem
return ()
countries :: Map.Map T.Text T.Text
countries = Map.fromList [("1", "France"), ("2", "Switzerland"), ("3", "Germany"), ("4", "Italy"), ("5", "USA")]
result :: T.Text -> T.Text
result key = "You selected: " <> fromJust (Map.lookup key countries)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.