0

我正在尝试做类似的事情对于字符串列表中的元素,我旁边有一个复选框,并确定哪个复选框被选中。使用来自互联网的示例,我能够运行一个示例

{-# 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 does not exist。我找不到使用 inputCheckbox 的示例,我希望有人能帮助我调整过滤字符串,以便在它旁边显示复选框,并且我可以正确运行表单。另外,在我发布的上一个链接中,我不知道函数inputCheckBox是什么,因为我只能找到inputCheckbox。也许这是一个过时的功能?

4

1 回答 1

1

我正在回答我自己的问题,因为我想出了如何获取inputCheckbox而不是inputText。实际上,这个例子很有帮助。这是我能找到的唯一一个使用 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 <> "!")
于 2015-10-28T04:40:54.513 回答