简体   繁体   中英

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 . 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. Also, in previous link I posted , I don't know what the function inputCheckBox , since I could only find inputCheckbox . Perhaps this is an outdated function?

I'm answering my own question since I figured out how to get inputCheckbox instead of inputText . Actually, this example helped alot. It was the only one I could find which uses inputCheckbox. What I needed to do was change data CheckBox = CheckBox { postTitle :: T.Text } to data CheckBox = CheckBox Bool Then I could just initialized

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM