You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

datamodel-fields.asciidoc 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. ---
  2. title: Editing Values in Fields
  3. order: 2
  4. layout: page
  5. ---
  6. [[datamodel.fields]]
  7. = Editing Values in Fields
  8. Input field components (implementing the `HasValue` interface) are in a very central role for handling data in an application since different types of fields are the main user interface controls used for displaying and editing data.
  9. While each field implementation has its own functionality, all fields also have some common core functionality.
  10. By using these common building blocks, the data binding part of the framework can help simplify the code we need to write for many common data entry cases.
  11. At the very core, each field has a value that the user can see and edit through the user interface.
  12. The value can also be read and set through code.
  13. [source,java]
  14. ----
  15. TextField nameField = new TextField("Enter your name");
  16. Button sayHelloButton = new Button("Say hello", clickEvent -> {
  17. String name = nameField.getValue();
  18. Notification.show("Hello " + name);
  19. });
  20. ----
  21. Each field implementation has its own specific value type – for instance, the type of a `TextField` is `String`, the type of a `Slider` is `Double`, the type of a `DateField` is `LocalDate`, and so on.
  22. == Reacting to Value Changes
  23. When the value of a field changes, it fires a value change event.
  24. By listening to the event, we can find out the new value of the field and whether the value was changed by the user through the user interface or by code through the `setValue` method.
  25. [source,java]
  26. ----
  27. TextField nameField = new TextField("Enter your name");
  28. nameField.addValueChangeListener(event -> {
  29. String origin = event.isUserOriginated()
  30. ? "by the user"
  31. : "from code";
  32. String message = "Name is " + event.getValue()
  33. + " as set " + origin;
  34. Notification.show(message);
  35. });
  36. Button setButton = new Button("Set name", event -> {
  37. // Will show "Name is Zaphod as set from code"
  38. nameField.setValue("Zaphod");
  39. });
  40. ----
  41. Fields can also be set in read-only mode, which means that the user is not able to directly edit the value through the user interface, but the value can still be changed through code.
  42. This is useful for showing the user that the data is there, even though the user is currently not allowed to edit it.
  43. When editing multiple values from the same business object, you can use `Binder` to simplify how the values of all input fields in a form are handled.
  44. <<dummy/../../../framework/datamodel/datamodel-forms.asciidoc#datamodel.forms,"Binding Data to Forms">> describes how this is done.