summaryrefslogtreecommitdiffstats
path: root/documentation/datamodel/datamodel-fields.asciidoc
diff options
context:
space:
mode:
authorLeif Åstrand <leif@vaadin.com>2016-07-18 11:01:45 +0300
committerArtur Signell <artur@vaadin.com>2016-08-04 17:17:02 +0300
commit7a3ba5b563e742c800c0d0c58695bbed88381578 (patch)
tree7e988364815442431c3b75d9694cdc2cdb03cbc7 /documentation/datamodel/datamodel-fields.asciidoc
parent3abd26bf5a763e480266da7bffa17b64a38cb2ab (diff)
downloadvaadin-framework-7a3ba5b563e742c800c0d0c58695bbed88381578.tar.gz
vaadin-framework-7a3ba5b563e742c800c0d0c58695bbed88381578.zip
Replace old data binding chapters with updated ones
* Add documentation about Fields for the data binding chapter * Add documentation about form binding for the data binding chapter * Add documentation about data sources for the data binding chapter Change-Id: I99297f2ebd3d874bd78569fd8d05bc649654c91d
Diffstat (limited to 'documentation/datamodel/datamodel-fields.asciidoc')
-rw-r--r--documentation/datamodel/datamodel-fields.asciidoc68
1 files changed, 68 insertions, 0 deletions
diff --git a/documentation/datamodel/datamodel-fields.asciidoc b/documentation/datamodel/datamodel-fields.asciidoc
new file mode 100644
index 0000000000..f6d1530420
--- /dev/null
+++ b/documentation/datamodel/datamodel-fields.asciidoc
@@ -0,0 +1,68 @@
+---
+title: Editing Values in Fields
+order: 2
+layout: page
+---
+
+[[datamodel.fields]]
+= Editing Values in Fields
+
+The [interfacename]#Field# interface is in a very central role for handling data in an application since different types of fields are the main user interface controls used for entering data into the application.
+
+While each field implementation has its own functionality, all fields also have some common core functionality.
+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.
+
+At the very core, each field has a value that the user can see and edit through the user interface.
+The value can also be read and set through code.
+
+[source,java]
+----
+TextField nameField = new TextField("Enter your name");
+
+Button sayHelloButton = new Button("Say hello", clickEvent -> {
+ String name = nameField.getValue();
+ Notification.show("Hello " + name);
+});
+----
+
+Each field implementation has its own specific value type – the type of a [classname]#TextField# is [classname]#String#, the type of a [classname]#Slider# is [classname]#Double#, the type of a [classname]#PopupDateField# is [classname]#LocalDate#, and so on.
+
+== Reacting to Value Changes
+
+When the value of a field changes, it fires a value change event.
+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 [methodname]#setValue# method.
+
+[source,java]
+----
+TextField nameField = new TextField("Enter your name");
+nameField.addValueChangeListener(clickEvent -> {
+ String origin = event.isUserOriginated()
+ ? "by the user"
+ : "from code";
+ String message = "Name is " + event.getValue()
+ + " as set " + origin;
+ Notification.show(message);
+});
+
+Button button = new Button("Set name", event -> {
+ // Will show "Name is Zaphod as set from code"
+ nameField.setValue("Zaphod");
+});
+----
+
+If we only need to get the new value whenever it changes, we can use the [methodname]#onChange# method.
+This kind of listener directly receives the new value, which makes it convenient if we want to directly pass on the value to some other part of the application.
+It is often practical to use a method reference for defining where the new value should be delivered.
+
+[source,java]
+----
+Person person = new Person("Douglas Adams", 49);
+
+TextField nameField = new TextField("Name");
+nameField.setValue(person.getName());
+nameField.onChange(person::setName);
+
+Button button = new Button("Show name", event -> {
+ Notification.show("Person name: " + person.getName());
+})
+----