簡體   English   中英

如何修改inputText以使用inputCheckbox

[英]how do I modify inputText to use inputCheckbox

我試圖做類似的東西 ,在對字符串列表中的元素,我旁邊一個復選框,並找出哪些復選框被選中與否。 使用互聯網上的示例,我能夠運行一個示例

{-# 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,以便它可以在其旁邊顯示復選框,並且可以正常運行該表單。 另外,在我發布的上一個鏈接中 ,我不知道函數inputCheckBox是什么,因為我只能找到inputCheckBox 也許這是一個過時的功能?

我正在回答自己的問題,因為我想出了如何獲取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 <> "!")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM