Scala 电梯里的单选按钮?

Scala 电梯里的单选按钮?,scala,radio-button,lift,Scala,Radio Button,Lift,我正在尝试在我的webapp中呈现单选按钮选项,但我遇到了一些问题 我所做的是尝试以下答案中发布的jcern解决方案: 这是我的HTML代码: 但是编译器在绑定时给了我一个错误: could not find implicit value for parameter computer:net.liftweb.util.CanBind[net.liftweb.http.SHtml.ChoiceHolder[String]] [error] "@choice" #> SHtml.rad

我正在尝试在我的webapp中呈现单选按钮选项,但我遇到了一些问题

我所做的是尝试以下答案中发布的jcern解决方案:

这是我的HTML代码:

但是编译器在绑定时给了我一个错误:

could not find implicit value for parameter computer:net.liftweb.util.CanBind[net.liftweb.http.SHtml.ChoiceHolder[String]]
[error]   "@choice" #> SHtml.radio(radioChoices, choice, (resp) => choice = Full(resp))
[error]             ^
我必须绑定.toForm才能像这样通过编译:

问题是我的网页上没有单选按钮,什么都没有

我做错什么了吗?我看不见。为什么没有.toForm的第一个解决方案在编译时会给我一个错误?

原因是SHtml.radio和SHtml.radioElem返回一个ChoiceHolder,而不是SHtml中大多数其他项所返回的NodeSeq-如图所示。因此,您需要自己调用.toForm或呈现输出

返回ChoiceHolder允许您自定义每个项目的显示方式,以便您可以为标签添加自定义文本等。。。如果基本的toForm输出不适合您,您可以执行以下操作:

val choices =  SHtml.radio(radioChoices, choice, (resp) => choice = Full(resp))
"@choice" #> choices.items.zipWithIndex.map { case(itm, pos) =>
  val rId = "myId-%s".format(pos)
  <div class="radio">{ 
      //We'll add a custom ID attribute to the radio button
      itm.xhtml.asInstanceOf[Elem] % ("id", rId) 
    }<label for={ rId }>{
      //Output the toString or use some other method to output a label 
      itm.toString  
    }</label>
  </div>
}
您可以在中找到有关自定义单选按钮的详细信息

至于为什么你的CSSSelector没有输出任何东西,我不确定。您尝试的代码片段对我有效。下面的屏幕截图演示了我使用toForm方法看到的输出

原因是SHtml.radio和SHtml.radioElem返回一个ChoiceHolder,而不是SHtml中大多数其他项所返回的NodeSeq,正如您在中看到的。因此,您需要自己调用.toForm或呈现输出

返回ChoiceHolder允许您自定义每个项目的显示方式,以便您可以为标签添加自定义文本等。。。如果基本的toForm输出不适合您,您可以执行以下操作:

val choices =  SHtml.radio(radioChoices, choice, (resp) => choice = Full(resp))
"@choice" #> choices.items.zipWithIndex.map { case(itm, pos) =>
  val rId = "myId-%s".format(pos)
  <div class="radio">{ 
      //We'll add a custom ID attribute to the radio button
      itm.xhtml.asInstanceOf[Elem] % ("id", rId) 
    }<label for={ rId }>{
      //Output the toString or use some other method to output a label 
      itm.toString  
    }</label>
  </div>
}
您可以在中找到有关自定义单选按钮的详细信息

至于为什么你的CSSSelector没有输出任何东西,我不确定。您尝试的代码片段对我有效。下面的屏幕截图演示了我使用toForm方法看到的输出


谢谢jcern,我真的不明白为什么我的代码不适合我。。我正在尝试你的,但它也给了我一个错误:在行'}{'编译器说:'not found:value rId'但value rId在上面两行声明..这是因为大括号?不应该..很抱歉,我移动了rId的声明。这应该可以访问。哦,对了,thx我明天早上会试试,希望它能解决这个问题..我正在编辑'target/webapp'fo中的.html文件lder而不是“main/webapp”,这是一个多么有趣的问题。不过你的答案总是很有趣。谢谢你!谢谢jcern,我真的不明白为什么我的代码对我不起作用。我正在尝试你的代码,但它也给了我一个错误:in line'}{'编译器说:'not found:value rId'但value rId在上面两行声明..这是因为大括号?不应该..很抱歉,我移动了rId的声明。这应该可以访问。哦,对了,thx我明天早上会试试,希望它能解决这个问题..我正在编辑'target/webapp'fo中的.html文件不是'main/webapp',而是'main/webapp',这是一个多么有趣的问题啊。不过你的答案总是很有趣。谢谢!解决了这个问题。我在'main/webapp/'的'taget/webapp/'文件夹instaead中编辑html文件。上面发布的代码很好地解决了这个问题。我在'main/webapp/'的'taget/webapp/'文件夹instaead中编辑html文件上面贴的代码工作正常
"name=choice" #> SHtml.radio(radioChoices, choice, (resp) => choice = Full(resp)).toForm
val choices =  SHtml.radio(radioChoices, choice, (resp) => choice = Full(resp))
"@choice" #> choices.items.zipWithIndex.map { case(itm, pos) =>
  val rId = "myId-%s".format(pos)
  <div class="radio">{ 
      //We'll add a custom ID attribute to the radio button
      itm.xhtml.asInstanceOf[Elem] % ("id", rId) 
    }<label for={ rId }>{
      //Output the toString or use some other method to output a label 
      itm.toString  
    }</label>
  </div>
}