简体   繁体   English

如何修改inputText以使用inputCheckbox

[英]how do I modify inputText to use inputCheckbox

I am trying to do something similar to this , where for an element in list of strings, I have a checkbox next to it and figure out which checkbox is checked or not. 我试图做类似的东西 ,在对字符串列表中的元素,我旁边一个复选框,并找出哪些复选框被选中与否。 Using examples from the internet, I was able to get an example running 使用互联网上的示例,我能够运行一个示例

{-# 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 <> "!")

However, in the function renderForm, when I change inputText to something like inputCheckbox "True" , I get the error True does not exist . 但是,在函数renderForm中,当我将inputText更改为类似inputCheckbox“ True”时 ,我得到错误True不存在 I am not able to find an example where inputCheckbox is used, I was hoping someone would help me adapt filteredString so it would display checkboxes next to it, and I can run the form properly. 我找不到使用inputCheckbox的示例,我希望有人可以帮助我适应filteredString,以便它可以在其旁边显示复选框,并且可以正常运行该表单。 Also, in previous link I posted , I don't know what the function inputCheckBox , since I could only find inputCheckbox . 另外,在我发布的上一个链接中 ,我不知道函数inputCheckBox是什么,因为我只能找到inputCheckBox Perhaps this is an outdated function? 也许这是一个过时的功能?

I'm answering my own question since I figured out how to get inputCheckbox instead of inputText . 我正在回答自己的问题,因为我想出了如何获取inputCheckbox而不是inputText Actually, this example helped alot. 实际上, 这个例子很有帮助。 It was the only one I could find which uses inputCheckbox. 这是我能找到的唯一使用inputCheckbox的代码。 What I needed to do was change data CheckBox = CheckBox { postTitle :: T.Text } to data CheckBox = CheckBox Bool Then I could just initialized 我需要做的是将data CheckBox = CheckBox { postTitle :: T.Text }更改为data CheckBox = CheckBox Bool然后我可以初始化

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

Here is the full source: 这是完整的源代码:

{-# 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