如何修改inputText以使用inputCheckbox

时间:2015-10-26 14:30:50

标签: haskell digestive-functors haskell-spock

我正在尝试做类似this的事情,对于字符串列表中的元素,我旁边有一个复选框,并确定选中或未选中的复选框。通过互联网上的例子,我得到了一个运行示例

{-# LANGUAGE OverloadedStrings #-}

import Data.Monoid
import Data.String
import Data.List
import qualified Data.Text as T

import Web.Spock.Safe
import Web.Spock.Digestive

import Text.Blaze (ToMarkup(..))
import Text.Blaze.Html5 hiding (html, param, main)
import qualified Text.Blaze.Html5 as H
import Text.Blaze.Html.Renderer.Utf8 (renderHtml)

import Text.Digestive
import Text.Digestive.Blaze.Html5

import System.Directory
import Control.Monad.IO.Class
import Control.Monad (forM_)

gen :: Html -> [Html] -> Html
gen title elts  = H.html $ do
  H.head $
    H.title title
  H.body $
    H.ul $ mapM_ H.li elts

data CheckBox = CheckBox { postTitle :: T.Text }

checkboxForm = CheckBox
             <$> "title" .: Text.Digestive.text Nothing

renderForm :: View Html -> Html
renderForm v = do
  Text.Digestive.Blaze.Html5.form v "POST" $ do
    H.p $ do
          Text.Digestive.Blaze.Html5.label "title" v "Post title: "
          inputText "text" v
    inputSubmit "Submit Post"

main :: IO ()
main =
    runSpock 8080 $ spockT Prelude.id $ do
       get root $ do
           listing <- liftIO $ getDirectoryContents "/home/hasenov/mydir"
           let filteredListing = filter (\l -> not $ isPrefixOf "." l) listing
           (view, result) <- runForm "checkboxForm" checkboxForm
           case result of
               Nothing -> lazyBytes $ renderHtml (renderForm view)
               Just newCheckbox -> lazyBytes $ renderHtml (renderForm view)
--           lazyBytes $ renderHtml (gen "My Blog" (Data.List.map fromString filteredListing))
--       get ("hello" <//> var) $ \name ->
--           text ("Hello " <> name <> "!")

但是,在函数renderForm中,当我将inputText更改为 inputCheckbox“True”之类的时候,我收到错误 True不存在。我无法找到使用inputCheckbox的示例,我希望有人能帮我调整 filteredString ,这样它会显示旁边的复选框,我可以正确运行表单。另外,在previous link I posted中,我不知道函数 inputCheckBox ,因为我只能找到 inputCheckbox 。也许这是一个过时的功能?

1 个答案:

答案 0 :(得分:1)

我回答了自己的问题,因为我想出了如何获取 inputCheckbox 而不是 inputText 。实际上,this example帮了很多忙。这是我唯一能找到的使用inputCheckbox的人。我需要做的是改变     data CheckBox = CheckBox { postTitle :: T.Text } 至     data CheckBox = CheckBox Bool 然后我可以初始化

checkboxForm = CheckBox
         <$> "title" .: bool (Just False)

以下是完整资料来源:

{-# LANGUAGE OverloadedStrings #-}

import Data.Monoid
import Data.String
import Data.List
import qualified Data.Text as T

import Web.Spock.Safe
import Web.Spock.Digestive

import Text.Blaze (ToMarkup(..))
import Text.Blaze.Html5 hiding (html, param, main)
import qualified Text.Blaze.Html5 as H
import Text.Blaze.Html.Renderer.Utf8 (renderHtml)

import Text.Digestive
import Text.Digestive.Blaze.Html5

import System.Directory
import Control.Monad.IO.Class
import Control.Monad (forM_)

gen :: Html -> [Html] -> Html
gen title elts  = H.html $ do
  H.head $
    H.title title
  H.body $
    H.ul $ mapM_ H.li elts

data CheckBox = CheckBox Bool

checkboxForm = CheckBox
             <$> "title" .: bool (Just False)

renderForm :: View Html -> [Html] -> Html
renderForm v strings = do
  Text.Digestive.Blaze.Html5.form v "POST" $ do
    H.p $ mapM_ (\string -> do
      inputCheckbox "title" v
      Text.Digestive.Blaze.Html5.label "title" v string
      H.br) strings
    inputSubmit "Submit Post"

main :: IO ()
main =
    runSpock 8080 $ spockT Prelude.id $ do
       get root $ do
           listing <- liftIO $ getDirectoryContents "/home/ecks/btsync-gambino"
           let filteredListing = filter (\l -> not $ isPrefixOf "." l) listing
           (view, result) <- runForm "checkboxForm" checkboxForm
           case result of
               Nothing -> lazyBytes $ renderHtml (renderForm view (Data.List.map fromString filteredListing))
               Just newCheckbox -> lazyBytes $ renderHtml (renderForm view (Data.List.map fromString filteredListing))
--           lazyBytes $ renderHtml (gen "My Blog" (Data.List.map fromString filteredListing))
--       get ("hello" <//> var) $ \name ->
--           text ("Hello " <> name <> "!")