Hi,

I'm having trouble working out how to do a really simple thing -
submit a form and return some results.
Imagine a simple page that took a name of a person to search for, and
displayed a list of people matching that name.
I've put together a dummy example to show you what I've tried :

So the view is pretty simple :

        <lift:PeopleSearchSnippet.searchForPeople form="post">
            <table>
                <tr>
                    <td>Find People !</td>
                    <td><peopleSearch:nameToSearchFor>Value to search
on</peopleSearch:nameToSearchFor></td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td><peopleSearch:submit><button>Find People</
button></peopleSearch:submit></td>
                </tr>
                <tr>
                    <td colspan="2"><peopleSearch:searchResults>Value
to search on</peopleSearch:searchResults></td>
                </tr>
            </table>
        </lift:PeopleSearchSnippet.searchForPeople>


The snippet is :

class PeopleSearchSnippet  {

  def searchForPeople(form: NodeSeq) = {
    val search = PeopleSearch.create
    println("\n\n
\n-------------------------------------------------------------------
\n" +search)

    def performSearch(): Unit =
      search.validate match {
        case Nil => performSearchOn(search)
        case xs => S.error(xs); S.mapSnippet
("PeopleSearchSnippet.searchForPeople", doBind)
      }

    def doBind(form: NodeSeq) = {
      bind("peopleSearch", form,
        "nameToSearchFor" -> search.nameToSearchFor.toForm,
        "submit" -> submit("Search For People", performSearch),
        "searchResults" -> renderResults(search)
        )
    }

    doBind(form)
  }

  /**
   *  Dummy render method which would render the search results
   */
  def renderResults(search: PeopleSearch) = {
    // Just make some dummy results if they actually performed a
search
    // If done for real, it would render the results on the
search:PeopleSearch object
    if (search.nameToSearchFor != "") {
      List("dave smith", "jimmy cracked corn", "lazy t.
bones").foldLeft (List[Elem]())((x, y) => <div>{y}</div> :: x)
    } else {
      <div>No search done yet</div>
    }
  }

  /**
   *  Dummy method that would do a search, save some results to be
rendered to the view
   */
  def performSearchOn(search: PeopleSearch) = {
    println("Searching on " +search)
    // This would populate the results to display if done for real,
    // so that renderResults() method could render the search results
into html
    search.save
  }


I think I know whats going wrong, but I'm not really sure how to get
Lift to do what I'm after.

The problem is that when "renderResults()" is called, the search value
("nameToSearchFor") is always empty.
But when the submit button is clicked, the "performSearchOn" method
has a populated search value.

I think the reason is that "performSearch()" is bound to the submit
action in doBind() - and is executed when a form submit is done - but
the "searchResults" value is bound before that method is actually
called - hence its always got empty search values.

I guess its because I'm expecting List to be doing something I don't
quite understand, based on my experience with  MVC frameworks like
Spring MVC etc.
In my mind, the process is submit form, use object to perform search
on, populate the results, render the page, but obviously thats not
whats going on.

Any help on how to achieve my aim (ie submit a form, render some
results) would be most appreciated,

Thanks,

Ben

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

Reply via email to