문제

How do I pre-populate a form input field while rendering it.

What I want to achieve is display a form with a input field pre-populated. This value is to be populated dynamically.

This is my form

<form class="lift:Expense.add?form=post">
  Date: <input type="text" id="date" name="date" /><br />
  Amount: <input type="text" name="amount" /><br /> 
  Towards: <input type="text" name="towards" /><br /> 
  <input type="submit" value="Add" />
</form>

This is the corresponding snippet

def add = {
  var dateParam = ""
  var amount: Int = 0
  var towards: String = ""

  def process() {
    S.redirectTo("/")
  }

  "name=date" #> SHtml.onSubmit(dateParam = _) &
  "name=amount" #> SHtml.onSubmit(value => asInt(value).foreach(amount = _)) &
  "name=towards" #> SHtml.onSubmit(towards = _) &
  "type=submit" #> SHtml.onSubmitUnit(process)
}

What I want is to set the server date to the date input field and save it when I submit the form. For now my problem is how do I display the date.

도움이 되었습니까?

해결책

There are two ways to do that. The first is to set each attribute independently, like this:

"name=amount" #> SHtml.onSubmit(value => asInt(value).foreach(amount = _)) &
"name=amount [value]" #> amount

The other would be to use a Lift component to replace the whole input. Something like:

"name=amount" #> SHtml.text(amount, value => asInt(value).foreach(amount = _))

The difference between the two approaches is that in the first, you are only adding specific attributes to the element in your template. In the second, you are replacing the entire element with an input type="text" created by Lift.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top