Charles,

Thanks for the suggestion.

I've just checked up code that looks something like yours:

  def ajaxButton(text: NodeSeq, func: () => JsCmd, attrs: (String,
String)*): Elem =
    attrs.foldLeft(<button
onclick={makeAjaxCall(Str(mapFunc(func)+"=true")).toJsCmd+"; return false;"}
        >{text}</button>)(_ % _)

All the various form element creation methods take an attrs vararg.

Also, note the use of foldLeft

Thanks,

David


On Tue, Nov 18, 2008 at 1:25 AM, Charles F. Munat <[EMAIL PROTECTED]> wrote:

>
> So I copied SHtml over for fun and renamed it FH (form helper). Then I
> tried messing with textarea first, just for fun. This can probably be
> done much better, but this works just fine:
>
> private def getAttrs(attrs: Tuple2[String, String]*) : MetaData = {
>   attrs.toList match {
>     case x :: xs =>
>       new UnprefixedAttribute(x._1, x._2, getAttrs(xs: _*))
>     case _ => Null
>   }
> }
>
> def textarea(value: String, func: String => Any,
>   attrs: Tuple2[String, String]*): Elem =
>     textarea_*(value, SFuncHolder(func), attrs: _*)
>
> def textarea_*(value: String, func: AFuncHolder,
>   attrs: Tuple2[String, String]*): Elem = {
>     (<textarea name={mapFunc(func)}
>         rows="8" cols="40">{value}</textarea>) %
>       getAttrs(attrs: _*)
> }
>
> Now I can call it like this:
>
> FH.textarea(contact.message, contact.message = _, ("id", "myId"),
>   ("class", "myClass"), ("rows", "12"))
>
> And I get this:
>
> <textarea id="myId" class="myClass" rows="12" cols="40"></textarea>
>
> (I added default rows and cols attributes because they are required for
> valid XHTML.)
>
> For what it's worth, this works much better for me.
>
> Chas.
>
> Charles F. Munat wrote:
> > Ha. Great minds stink alike. I just discovered this little setback
> > (after adding "id" attributes to about fifty checkboxes -- note to self:
> > check one before doing them all).
> >
> > I've been messing with the map but it's messing with me. I can grab the
> > right node pretty easily, but then I can't seem to add the MetaData.
> >
> > If you're looking to add an id, you can use checkbox_id:
> >
> > SHtml.checkbox_id(user.isStupid, user.isStupid = _, Full("isStupid"))
> >
> > which adds id="isStupid" to the checkbox input.
> >
> > Chas.
> >
> >
> >
> > Derek Chen-Becker wrote:
> >> For a really neat trick, how would I set the ID for a checkbox? The
> >> SHtml checkbox method returns a NodeSeq. I suppose I could just do a
> >> "map", but I was wondering if there was a simpler way.
> >>
> >> Thanks,
> >>
> >> Derek
> >>
> >> On Mon, Nov 17, 2008 at 4:54 PM, David Pollak
> >> <[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>>
> >> wrote:
> >>
> >>
> >>
> >>     On Mon, Nov 17, 2008 at 3:41 PM, Charles F. Munat <[EMAIL PROTECTED]
> >>     <mailto:[EMAIL PROTECTED]>> wrote:
> >>
> >>
> >>         Added:
> >>
> >>
> http://liftweb.net/index.php/FAQ#How_do_I_add_attributes_to_form_fields_created_with_SHtml_methods.3F
> >>
> >>
> >>     Awesome!  Thanks!
> >>
> >>
> >>
> >>         Chas.
> >>
> >>         Jorge Ortiz wrote:
> >>          > Oops, it might be:
> >>          >
> >>          > SHtml.text(user.name <http://user.name> <http://user.name>,
> >>         user.name <http://user.name> <http://user.name> = _) %
> >>          >   ("size" -> "24") %
> >>          >   ("maxlength" -> "48") %
> >>          >   ("id" -> "user_name") %
> >>          >   ("title" -> "Enter your name")
> >>          >
> >>          > One or both of those should work.
> >>          >
> >>          > --j
> >>          >
> >>          > On Mon, Nov 17, 2008 at 12:27 PM, Jorge Ortiz
> >>         <[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>
> >>          > <mailto:[EMAIL PROTECTED]
> >>         <mailto:[EMAIL PROTECTED]>>> wrote:
> >>          >
> >>          >     Try:
> >>          >
> >>          >     SHtml.text(user.name <http://user.name>
> >>         <http://user.name>, user.name <http://user.name>
> >>          >     <http://user.name> = _) %
> >>          >       ("size", "24") %
> >>          >       ("maxlength", "48") %
> >>          >       ("id", "user_name") %
> >>          >       ("title", "Enter your name")
> >>          >
> >>          >     That should work.
> >>          >
> >>          >     --j
> >>          >
> >>          >     On Mon, Nov 17, 2008 at 12:21 PM, Charles F. Munat
> >>         <[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>
> >>          >     <mailto:[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>>> 
> >> wrote:
> >>          >
> >>          >
> >>          >         I spend a lot of time writing % new
> >>         UnprefixedAttribute(...) to add
> >>          >         attributes to SHtml form elements (input, textarea,
> >>         select, etc.).
> >>          >
> >>          >         It would be nice if the relevant SHtml methods would
> >>         permit optional
> >>          >         extra parameters in the form of tuples where contents
> >>         are name-value
> >>          >         pairs that get translated to attributes. I don't
> >>         think this
> >>          >         would be a
> >>          >         breaking change.
> >>          >
> >>          >         In other words, instead of:
> >>          >
> >>          >         SHtml.text(user.name <http://user.name>
> >>         <http://user.name>, user.name <http://user.name>
> >>          >         <http://user.name> = _) %
> >>          >           new UnprefixedAttribute("size", "24",
> >>          >             new UnprefixedAttribute("maxlength", "48",
> >>          >               new UnprefixedAttribute("id", "user_name",
> >>          >                 new UnprefixedAttribute("title", "Enter your
> >>         name", Null)
> >>          >               )
> >>          >             )
> >>          >           )
> >>          >
> >>          >         I could do:
> >>          >
> >>          >         SHtml.text(user.name <http://user.name>
> >>         <http://user.name>, user.name <http://user.name>
> >>          >         <http://user.name> = _,
> >>          >           ("size", "24"),
> >>          >           ("maxlength", "48"),
> >>          >           ("id", "user_name"),
> >>          >           ("title", "Enter your name")
> >>          >         )
> >>          >
> >>          >         Which is a hell of a lot cleaner and faster, and I
> >>         don't have to
> >>          >         import
> >>          >         UnprefixedAttribute and remember the % and the new
> >>         keywords.
> >>          >
> >>          >         Or is this already possible and I'm missing it?
> >>          >
> >>          >         I have an online survey with 43 questions on 5 pages
> >>         involving
> >>          >         more than
> >>          >         150 database fields. The snippet is already over 700
> >>         lines and
> >>          >         growing.
> >>          >         It's just unmanageable.
> >>          >
> >>          >         Another thing: the <label> element is a very
> >>         important part of
> >>          >         accessibility to persons with disabilities, but it
> >>         needs a "for"
> >>          >         attribute that references the "id" attribute of the
> input
> >>          >         element (or
> >>          >         textarea, etc.). The current SHtml output (unlike
> >>         Rails) does not
> >>          >         generate an id attribute, so they have to be added by
> >>         hand. Has
> >>          >         anyone
> >>          >         given any thought to automatically-generated id
> >>         elements on form
> >>          >         fields?
> >>          >         Better yet, some way to integrate labels?
> >>          >
> >>          >         Ideally, I would do something like this:
> >>          >
> >>          >         <survey:name label="Your name"/>
> >>          >
> >>          >         Or this:
> >>          >
> >>          >         <survey:name>Your name</survey:name>
> >>          >
> >>          >         and bind would allow me to generate this:
> >>          >
> >>          >         <label for="id123">Your name
> >>          >         <input type="text" id="id123" value=""
> >>          >         name="F1226951216428645000_I0G"/>
> >>          >         </label>
> >>          >
> >>          >         What do others think? Is this already possible?
> >>          >
> >>          >         Chas.
> >>          >
> >>          >
> >>          >
> >>          >
> >>          >
> >>          > >
> >>
> >>
> >>
> >>
> >>
> >>     --
> >>     Lift, the simply functional web framework http://liftweb.net
> >>     Collaborative Task Management http://much4.us
> >>     Follow me: http://twitter.com/dpp
> >>     Git some: http://github.com/dpp
> >>
> >>
> >>
> >>
> >>
> >
> > >
>
> >
>


-- 
Lift, the simply functional web framework http://liftweb.net
Collaborative Task Management http://much4.us
Follow me: http://twitter.com/dpp
Git some: http://github.com/dpp

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Lift" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/liftweb?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to