Scala列表validSelectValues未更新

时间:2014-02-25 08:52:37

标签: scala lift

这是我在类Building中的代码:

object province extends MappedLongForeignKey(this, Province) {
    override def dbColumnName = "province_id"

    def selectValues: Box[List[(String, String)]] =
        Full((("0", "")) +: Province.findAll(OrderBy(Province.name, Ascending)).
                map( p => (p.id.is.toString, p.name.is)))

    override def _toForm: Box[Elem] = Full(selectValues.flatMap{
        case Nil => Empty
        case xs => Full(SHtml.ajaxSelect(xs, Full(this.is.toString), v => {
            this.set(v.toLong)
            JsCmds.ReplaceOptions("council_select",
                   councilsList(v).map(c => (c.id.is.toString, c.name.is)), None)
        }))
    }.openOr(<span>{"sin provincias"}</span>))

    private def councilsList(p: String) = p match {
        case id if id != "" =>
            Council.findAll(By(Council.province, p.toLong),
                            OrderBy(Council.name, Ascending))
        case _ => List()
    }
}

// Council
object council extends MappedLongForeignKey(this, Council) {
    override def dbColumnName = "council_id"
    val id = "council_select"

    override def validSelectValues: Box[List[(Long, String)]] =
        Full(((0l, "")) +: Council.findAll(By(Council.province, province),
                             OrderBy(Council.name, Ascending)).
                map( c => (c.id.is, c.name.is)))

    override def _toForm: Box[Elem] = Full(validSelectValues.flatMap{
        case Nil => Empty
        case xs => Full(SHtml.selectObj(xs, Full(this.is), this.set, "id" -> id))
    }.openOr(<select id={id}/>))
}

在编辑表单中,当我在列表中更改省份时,理事会列表被省级过滤器完全替换,但是当我保存建筑物时,第一次没有保存/更新议员。下一次,它保存完美,因为该省被选中,直到我改变省其他时间...所以,当我设置一个新的省,国家没有保存,我需要保存2次,1省为省,1为议会。

我认为问题是因为对象委员会的validSelectValues,是我第一次加载页面时加载的,它会被省选中过滤,或者当省未设置时为空列表...我该怎么重装当我改变省名单时,理事会有效选择值吗?

由于

2 个答案:

答案 0 :(得分:0)

尝试将ajaxSelect的来电替换为ajaxUntrustedSelect。 Lift中的常规选择选项检查提交的值是否可信,IE:它是生成页面时可用的选项之一。如果您使用javascript或其他方法来改变列表,则可以使用组件的不受信任版本来避免该检查。

答案 1 :(得分:0)

我找到的解决方案......

object council extends MappedLongForeignKey(this, Council) {
override def dbColumnName = "council_id"
val id = "council_select"

def selectValues: Box[List[(String, String)]] = province.obj match {
   case Full(o) => Full( Council.findAll(By(Council.province, o),
                          OrderBy(Council.name, Ascending)).
            map( c => (c.id.is.toString, c.name.is)))

   case _ => Full((("0","")) Council.findAll(OrderBy(Council.name, Ascending)).
            map( c => (c.id.is.toString, c.name.is)))   
}

override def _toForm: Box[Elem] = Full(selectValues.flatMap{
    case Nil => Empty
    case xs => Full(SHtml.untrustedSelect(xs, Full(this.is.toString), v => {this.set(v.toLong)}, "id" -> id))
}.openOr(<select id={id}/>))

}