Christopher Chiche
Christopher Chiche

Reputation: 15345

Liftweb - LiftScreen and ajax

I am trying to use Liftscreen to implement a form in ajax but I want to use Ajax, that is to say that I want the finish function to be called whenever a field is updated. Usually I use functions like SHtml.ajaxText to achieve this goal but I would prefer to use LiftScreen as it already has a lot of routines included.

Here is a basic example of code I am using

class ScreenTest extends LiftScreen {

  // here are the fields and default values
  val name = field("Name", "")

  // the age has validation rules
  val age = field("Age", 0, minVal(13, "Too Young"))

  def finish() {
    println("-------> Finish function has be called")
    S.notice("Name: "+name)
    S.notice("Age: "+age)
  }
}

So I would like the finish function to be called whenever the field name or age is called. I would prefer a general setting instead of manually calling finish in each field.

Update I am working on a search engine depending on some criteria, so I want my results to be updated whenever a criteria is modified.

I already read the documentation page about LiftScreen on Assembla and I also found a discussion about ajax on LiftScreen on the official mailing list (which I will contact if I can't find any answer in here)

Upvotes: 2

Views: 444

Answers (1)

Emil L
Emil L

Reputation: 21111

I would try adding a small javascript to the template you are using for your screen (wizard-all ?). For instance:

jQuery('input').change(function() {
    jQuery('#idOfForm').submit();
});

Not sure if it is possible to add such a script from the LiftScreen snippet.

EDIT Another way would be to use a custom field as described in the wiki here. I think you could do something along the lines of:

object AjaxifiedScreen extends LiftScreen { 
  val name = new Field { 
    type ValueType = String 
    override def name = "name" 
    override implicit def manifest = buildIt[String] 
    override def default = "" 
    override def toForm: Box[NodeSeq] = SHtml.ajaxText(is, doStuff(_)) 
  } 
}

Upvotes: 1

Related Questions