Populate Listitem with comboboxes and editables labes and preserve the state of each object in lines

不羁的心 提交于 2019-12-12 00:57:07

问题


For avoid waste your time, this is a question for apache wicket and scala developers, thanks.

I have created a listView with method populateList, that fills a list of customers meetings with the respective manager responsible for each line according the figure bellow:

Two problems here:

  1. When a new new item is created, only the fields (labels) dateBegin and dateEnd are preserved with previous assigned values, I lost the values of Manager DropDownChoice and the Detail TextField in last line of the ListItem.
  2. After change the DropDownChoice of after exit from the Detail TextField is needed update the model with the respective value have chosen from each field.

So the values are kept only for labels, in truth links. Let me show the code:

add(new ListView[Meetings]("listMeetings", listData) {
  override protected def onBeforeRender() {
  periodTotal = new Period()
  super.onBeforeRender()
}

// for populating the listView
def populateItem(item: ListItem[customer]) = {
    var customer = item.getModelObject()

    item.add(new LinkDate("beginDate", customer))
    item.add(new LinkDate("endDate", customer))

    val listManagers: java.util.List[Manager] = managerDAO.listManagers
    item.add(new DropDownChoice("managerSelection", listManagers,new ChoiceRenderer[Manager]("name")))

    item.add(new TextField("detail"))

    /*
     * I tried to use the code bellow but it's cause a markups errors too.
     * Only the code above display the components without errors.
     */  
    //val managerSelection = new LinkManager("managerSelection",customer)
    //item.add(managerSelection)
    //item.add(new LinkDetail("detail",  customer))

    var period = new Period(customer.beginDate, customer.endDate.get)
    item.add(new Label("total", period.toString(getFormatter())))

    item.add(new LinkEdit("edit", customer))
}})

The functions bellow work fine for date value and for edit a respective fields of the line:

private class LinkDate(id: String, customer: Customer) extends Link[String](id) {

    setEnabled(false)
    add(new Label("label", new Model[String]() {
      override def getObject(): String = {
        var result = ""
        if (id == "beginDate") {
          result = customer.beginDate.toString("dd/MM/YYYY HH:mm:ss")
        }
        if (id == "endDate") {
          result = customer.endDate.get.toString("dd/MM/YYYY HH:mm:ss")
        }
        return result
      }
    }))
     // ... doing other stuff
  }

  private class LinkEdit(id: String, customer: Customer) extends Link[String](id) {

    add(new Label("label", new Model[String]() {
      override def getObject: String = "edit"
    }));
    // ... doing other stuff
  }

But for DropDown and TextField I tried to do the same and I failed strongly:

  // Doesn't work
  private class LinkManager(id: String, customer: Customer) extends Link[String](id) {

    val listManagers: java.util.List[Manager] = managerDAO.listManagers
    add(new DropDownChoice("managerSelection", listManagers,new ChoiceRenderer[Manager]("name") {

      def wantOnSelectionChangedNotifications() = {
        true;
      }
      def onSelectionChanged(managerSelection: Manager): String = {
        // saving model
        })
      }
    }))

    // ... doing other stuff
  }

  // Doesn't work
  // Also here I tried to change textFiled for a inline-ajax Editable label
  // And I need the behaviour to change model imadiately after change value
  private class LinkDetail(id: String, customer: Customer) extends Link[String](id) {

    add(new AjaxEditableLabel("detail", new Model[String]() {
      override def getObject(): String = {
        // ... doing other stuff
      }
    }))
  }

The piece of corresponding markup is showed bellow:

<TR wicket:id="listCustomersMeetings">
  <TD><a wicket:id="beginDate"><span wicket:id="label"></span></a></TD>
  <TD><a wicket:id="endDate"><span wicket:id="label"></span></a></TD>
  <TD>
      <SELECT wicket:id="managerSelection" name="id"></SELECT>
      <BR>
  </TD>

  <TD><INPUT wicket:id="detail" type="text" name="obs" value="_"/></TD>

  <TD wicket:id="total"></TD>

  <TD><a wicket:id="edit"><span wicket:id="label" style="text-align: center"></span></a></TD>

</TR>

I think that is needed a Object, similar to LinkDate, to deal and store the values for Manager DropDownChoice and for the textField or AjaxEditableLabel Detail, but I got in trouble to implement them.

Thanks for someone that could help me or give me information about


回答1:


Try to replace these lines

item.add(new DropDownChoice("managerSelection", listManagers,new ChoiceRenderer[Manager]("name")))
item.add(new TextField("detail"))

to something like:

  val managerSelectionCurrent = new PropertyModel(customer, "manager")
  val managerSelection = new DropDownChoice[Manager]("managerSelection", managerSelectionCurrent, listManagers,new ChoiceRenderer[Manager]("name")) {
    protected override def wantOnSelectionChangedNotifications: Boolean = {
      true
    }

    protected override def onSelectionChanged(newSelection: Manager) {
      // save changes
    }
  }
  item.add(managerSelection)

  val detail = new TextField("detail", new PropertyModel(customer, "details"))
  detail.add(new AjaxFormComponentUpdatingBehavior(("keyup")) {
    protected def onUpdate(target: AjaxRequestTarget) {
      // save changes
    }

    protected override def updateAjaxAttributes(attributes: AjaxRequestAttributes) {
      attributes.setThrottlingSettings(new ThrottlingSettings("thr", Duration.milliseconds(800)))
      super.updateAjaxAttributes(attributes)
    }
  })
  item.add(detail)


来源:https://stackoverflow.com/questions/16133260/populate-listitem-with-comboboxes-and-editables-labes-and-preserve-the-state-of

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!