how to conditionally mark a SHtml.checkbox as disabled

121 views
Skip to first unread message

Damian Helme

unread,
Aug 3, 2012, 4:59:33 PM8/3/12
to lif...@googlegroups.com
I want to add the 'disabled' attribute to a SHtml.checkbox based on a conditional check. 

The only way I've found so far is:

   "#isShared" #> SHtml.checkbox(isSharedRV.is, processIsShared(_))
                                      .map(x => x.asInstanceOf[scala.xml.Elem] % ("disabled" -> (if (!isOwner) "disabled" else Empty)))

The trick that's being used here is that if you call '% ("disabled -> Empty)' on an Elem then the 'disabled' attribute gets ignore altogether. 

However, the complicating factor is that SHtml.checkbox returns a NodeSeq rather than an Elem, so you can't apply the % operator directly as you could with, say, SHtml.text.

It feels a bit too clunky. Is there a better / easier way that I've missed?

Thanks

Damian

David Pollak

unread,
Aug 3, 2012, 5:28:46 PM8/3/12
to lif...@googlegroups.com
On Fri, Aug 3, 2012 at 1:59 PM, Damian Helme <damia...@gmail.com> wrote:
I want to add the 'disabled' attribute to a SHtml.checkbox based on a conditional check. 

The only way I've found so far is:


"#isShared" #> (if (!isOwner) "* [disabled]" #> "disabled" else PassThru ).apply(SHtml.checkbox(isSharedRV.is, processIsShared(_)))
 

   "#isShared" #> SHtml.checkbox(isSharedRV.is, processIsShared(_))
                                      .map(x => x.asInstanceOf[scala.xml.Elem] % ("disabled" -> (if (!isOwner) "disabled" else Empty)))

The trick that's being used here is that if you call '% ("disabled -> Empty)' on an Elem then the 'disabled' attribute gets ignore altogether. 

However, the complicating factor is that SHtml.checkbox returns a NodeSeq rather than an Elem, so you can't apply the % operator directly as you could with, say, SHtml.text.

It feels a bit too clunky. Is there a better / easier way that I've missed?

Thanks

Damian

--
--
Lift, the simply functional web framework: http://liftweb.net
Code: http://github.com/lift
Discussion: http://groups.google.com/group/liftweb
Stuck? Help us help you: https://www.assembla.com/wiki/show/liftweb/Posting_example_code
 
 
 



--
Telegram, Simply Beautiful CMS https://telegr.am
Lift, the simply functional web framework http://liftweb.net


Damian Helme

unread,
Aug 5, 2012, 5:38:25 AM8/5/12
to lif...@googlegroups.com
neat - thanks!
Reply all
Reply to author
Forward
0 new messages