Clearing out options when submitting an empty multiSelect form

49 προβολές
Παράβλεψη και μετάβαση στο πρώτο μη αναγνωσμένο μήνυμα

Aaron Valade

μη αναγνωσμένη,
16 Σεπ 2009, 10:57:45 π.μ.16/9/09
ως Lift
Hello all-
I'm having an issue when trying to update a many-to-one collection
attached to a JPA object in Lift. I'm currently using a
SHtml.multiSelect for choosing which objects should be assigned to the
many-to-one collection. My bind statement looks like this:

bind("location", xhtml,
"id" -> SHtml.hidden(() => locationVar(current)),
"name" -> SHtml.text(location.name, location.name = _),
"beacons" -> SHtml.multiSelect(choices, default, {bsIds : List
[String] => {
location.beacons.clear
bsIds.map( id => location.beacons.add(Model.getReference(classOf
[Beacon], id.toLong) ) )
} } ),
"save" -> SHtml.submit(?("Save"), doAdd) )

This works great when I'm adding or removing beacons to my location as
long as I don't unselect all options. When I unselect all options, my
closure for the multiSelect doesn't get called so the beacons Set
doesn't get cleared out.

How can I capture when I unselect everything in my multiSelect?

Thanks in advance!
- Aaron

Derek Chen-Becker

μη αναγνωσμένη,
16 Σεπ 2009, 1:07:23 μ.μ.16/9/09
ως lif...@googlegroups.com
What's happening here is that the form doesn't submit a field for "beacons" at all if nothing is selected, so the callback doesn't get called. What I typically do in scenarios like this is add a hidden field whose callback clears the collection. In your case, you could move the "location.beacons.clear" inside the hidden callback for "id" and have it all taken care of.

Derek

Aaron Valade

μη αναγνωσμένη,
16 Σεπ 2009, 1:10:56 μ.μ.16/9/09
ως lif...@googlegroups.com
On Sep 16, 2009, at 1:07 PM, Derek Chen-Becker wrote:

> What's happening here is that the form doesn't submit a field for
> "beacons" at all if nothing is selected, so the callback doesn't get
> called. What I typically do in scenarios like this is add a hidden
> field whose callback clears the collection. In your case, you could
> move the "location.beacons.clear" inside the hidden callback for
> "id" and have it all taken care of.


Ah, I didn't think about adding that clear statement there. Thanks
Derek!

- A

Απάντηση σε όλους
Απάντηση στον συντάκτη
Προώθηση
0 νέα μηνύματα