Search code examples
lift

Lift - how to get default value of select using WiringUI


I have an issue with getting default value of select dropdown. i have fruits val:

 val fruits = List("apple", "banana", "other")

and i render a tr with:

<tr id={ theLine.guid }>
      <td>
        {
          SHtml.ajaxSelect(fruits, Full(fruits(0)),
            s => {
              mutateLine(theLine.guid) {
                l => Line(l.guid, l.name, s, l.note)
              }
              Noop
            })
        }
      </td>
(...)

on page html is rendered correctly with option selected="selected", but when i try to save it to DB i get empty value of fruit. if i change option to 'other' and then i select it back to 'apple', it saves right value. i add also a println function to addLine to see what values are in this vector, and there is empty value if i dont change fruit option, so i suppose that it is not problem when i process lines to save it to DB.

can you help me with this?

thanks Gerard


Solution

  • Before you change your select option, you are not triggering the event that calls your function. The function is bound to onChange and that only gets fired when the value changes.

    To fix, you could either: Start with an option like "Select a value". This would require the user to change the item, but is the only way to trigger the onchange.

    If you don't want to do that, you could add a button and add your logic to a button click handler that would get called when submitted. Something like this should help - you'll need to bind it to your output, either inline as you provided, or via CSS Selectors:

    var selected = Full(fruits(0))
    SHtml.ajaxSelect(fruits, selected,
            s => {
              selected = Full(s)
              Noop
            })
    
    SHtml.ajaxSubmit("Submit", () => {
      mutateLine(theLine.guid) {
        l => Line(l.guid, l.name, selected, l.note)
      }
    })