| OLD | NEW |
| 1 --- | 1 --- |
| 2 layout: default | 2 layout: default |
| 3 title: "Get Input from a Form" | 3 title: "Get Input from a Form" |
| 4 description: "Using HTML forms and input elements to send data to a server" | 4 description: "Using HTML forms and input elements to send data to a server" |
| 5 has-permalinks: true | 5 has-permalinks: true |
| 6 tutorial: | 6 tutorial: |
| 7 id: forms | 7 id: forms |
| 8 next: indexeddb/ | 8 next: indexeddb/ |
| 9 next-title: "Use IndexedDB" | 9 next-title: "Use IndexedDB" |
| 10 prev: fetchdata/ | 10 prev: fetchdata/ |
| 11 prev-title: "Fetch Data Dynamically" | 11 prev-title: "Fetch Data Dynamically" |
| 12 --- | 12 --- |
| 13 | 13 |
| 14 {% capture whats_the_point %} | 14 {% capture whats_the_point %} |
| 15 | 15 |
| 16 * Use forms with inputs to gather data from a user. | 16 * Use forms with inputs to gather data from a user. |
| 17 * Send data to the server, declaratively or programmatically. | 17 * Send data to the server, declaratively or programmatically. |
| 18 * Get the response from the server with HttpRequest. | 18 * Get the response from the server with HttpRequest. |
| 19 * Handle communication asynchronously with Futures. | 19 * Handle communication asynchronously with Futures. |
| 20 * Use HttpServer to set up a server to listen on a port and host. | 20 * Use HttpServer to set up a server to listen on a port and host. |
| 21 * Use CORS headers to set access permissions for each request. | 21 * Use CORS headers to set access permissions for each request. |
| 22 * Use Web UI binding to sync form data and Dart data. | 22 * Use Polymer data binding to sync form data and Dart data. |
| 23 | 23 |
| 24 {% endcapture %} | 24 {% endcapture %} |
| 25 | 25 |
| 26 {% capture sample_links %} | 26 {% capture sample_links %} |
| 27 | 27 |
| 28 <p> | 28 <p> |
| 29 Get the source code for the samples featured in this target:</p> | 29 Get the source code for the samples featured in this tutorial:</p> |
| 30 | 30 |
| 31 <ul> | 31 <ul> |
| 32 <li> | 32 <li> |
| 33 <a href="https://github.com/dart-lang/dart-tutorials-samples/tree/master/web
/target10/search_form" | 33 <a href="https://github.com/dart-lang/dart-tutorials-samples/tree/master/web
/forms/search_form" |
| 34 target="_blank">search_form</a> | 34 target="_blank">search_form</a> |
| 35 </li> | 35 </li> |
| 36 <li> | 36 <li> |
| 37 <a href="https://github.com/dart-lang/dart-tutorials-samples/tree/master/web
/target10/slambook" | 37 <a href="https://github.com/dart-lang/dart-tutorials-samples/tree/master/web
/forms-polymer/slambook" |
| 38 target="_blank">slambook</a> (web_ui) | 38 target="_blank">slambook</a> |
| 39 </li> | 39 </li> |
| 40 <li> | 40 <li> |
| 41 <a href="https://github.com/dart-lang/dart-tutorials-samples/tree/master/web
/target10-polymer/slambook" | 41 <a href="https://github.com/dart-lang/dart-tutorials-samples/tree/master/web
/forms/multiselect" |
| 42 target="_blank">slambook</a> (polymer) | |
| 43 </li> | |
| 44 <li> | |
| 45 <a href="https://github.com/dart-lang/dart-tutorials-samples/tree/master/web
/target10/multiselect" | |
| 46 target="_blank">multiselect</a> | 42 target="_blank">multiselect</a> |
| 47 </li> | 43 </li> |
| 48 </ul> | 44 </ul> |
| 49 | 45 |
| 50 {% endcapture %} | 46 {% endcapture %} |
| 51 | 47 |
| 52 {% capture content %} | 48 {% capture content %} |
| 53 | 49 |
| 54 <div class="tute-target-title"> | 50 <div class="tute-target-title"> |
| 55 <h1>{{page.title}}</h1> | 51 <h1>{{page.title}}</h1> |
| 56 <h3>Use forms to get data from users.</h3> | 52 <h3>Use forms to get data from users.</h3> |
| 57 </div> | 53 </div> |
| 58 | 54 |
| 59 Many web applications rely on forms to collect data | 55 Many web applications rely on forms to collect data |
| 60 and submit that data to a server. | 56 and submit that data to a server. |
| 61 A form usually contains several input elements | 57 A form usually contains several input elements |
| 62 for entering data of various kinds, | 58 for entering data of various kinds, |
| 63 such as names and addresses, birthdays, email addresses, | 59 such as names and addresses, birthdays, email addresses, |
| 64 and so on. | 60 and so on. |
| 65 HTML supports several kinds of input elements, | 61 HTML supports several kinds of input elements, |
| 66 including text fields, text areas, radio buttons, and checkboxes. | 62 including text fields, text areas, radio buttons, and checkboxes. |
| 67 HTML5 adds more specialized input elements such | 63 HTML5 adds more specialized input elements such |
| 68 as email and password fields, | 64 as email and password fields, |
| 69 color pickers, date and time widgets, and range elements. | 65 color pickers, date and time widgets, and range elements. |
| 70 | 66 |
| 71 The main example in this target contains a client and a server. | 67 The main example in this tutorial contains a client and a server. |
| 72 The client uses the Web UI package to present its user interface | 68 The client uses Polymer to present its user interface |
| 73 (a form with many kinds of input elements), | 69 (a form with many kinds of input elements), |
| 74 and keep the interface in sync with Dart data. | 70 and keep the interface in sync with Dart data. |
| 75 The client and server communicate using | 71 The client and server communicate using |
| 76 several classes from the core Dart library, | 72 several classes from the core Dart library, |
| 77 including streams, Futures, HttpRequest, and so on. | 73 including streams, Futures, HttpRequest, and so on. |
| 78 The server uses CORS headers to allow cross-origin requests. | 74 The server uses CORS headers to allow cross-origin requests. |
| 79 | 75 |
| 80 This target brings together concepts from all previous targets. | |
| 81 Before you go on, | |
| 82 make sure you are comfortable with the information in | |
| 83 [Target 6](/docs/tutorials/web-ui/), | |
| 84 [Target 7](/docs/tutorials/templates/), | |
| 85 [Target 8](/docs/tutorials/custom-elements/), and | |
| 86 [Target 9](/docs/tutorials/fetchdata/), | |
| 87 in particular. | |
| 88 | |
| 89 * [About forms, generally](#about-forms) | 76 * [About forms, generally](#about-forms) |
| 90 * [About the slambook example, specifically](#about-the-slambook-example) | 77 * [About the slambook example, specifically](#about-the-slambook-example) |
| 91 * [Submitting a form](#submitting-a-form) | 78 * [Submitting a form](#submitting-a-form) |
| 92 * [Resetting a form](#resetting-a-form) | 79 * [Resetting a form](#resetting-a-form) |
| 93 * [Creating a server and listening on a port](#creating-a-server) | 80 * [Creating a server and listening on a port](#creating-a-server) |
| 94 * [Handling OPTIONS requests](#handling-options-requests) | 81 * [Handling OPTIONS requests](#handling-options-requests) |
| 95 * [Setting CORS headers](#setting-cors-headers) | 82 * [Setting CORS headers](#setting-cors-headers) |
| 96 * [Handling POST requests](#handling-post-requests) | 83 * [Handling POST requests](#handling-post-requests) |
| 97 * [Recipe for client-server web apps](#recipe) | 84 * [Recipe for client-server web apps](#recipe) |
| 98 * [Binding data to different kinds of inputs](#binding-data) | 85 * [Binding data to different kinds of inputs](#binding-data) |
| (...skipping 18 matching lines...) Expand all Loading... |
| 117 for the text entered by the user. | 104 for the text entered by the user. |
| 118 In this example, | 105 In this example, |
| 119 called `search_form`, | 106 called `search_form`, |
| 120 the default is to search | 107 the default is to search |
| 121 [dartlang.org](http://www.dartlang.org) | 108 [dartlang.org](http://www.dartlang.org) |
| 122 for "Cookbook", | 109 for "Cookbook", |
| 123 another useful resource for learning about Dart. | 110 another useful resource for learning about Dart. |
| 124 | 111 |
| 125 <iframe class="running-app-frame" | 112 <iframe class="running-app-frame" |
| 126 style="height:100px;width:500px;" | 113 style="height:100px;width:500px;" |
| 127 src="http://dart-lang.github.io/dart-tutorials-samples/web/target10/sear
ch_form/web/search_form.html"> | 114 src="http://dart-lang.github.io/dart-tutorials-samples/web/forms/search_
form/web/search_form.html"> |
| 128 </iframe> | 115 </iframe> |
| 129 | 116 |
| 130 The source code for this example is available on github: | 117 The source code for this example is available on github: |
| 131 <a href="https://github.com/dart-lang/dart-tutorials-samples/tree/master/web/tar
get10/search_form" | 118 <a href="https://github.com/dart-lang/dart-tutorials-samples/tree/master/web/for
ms/search_form" |
| 132 target="_blank">search_form</a>. | 119 target="_blank">search_form</a>. |
| 133 | 120 |
| 134 Here is the HTML code that creates the form: | 121 Here is the HTML code that creates the form: |
| 135 | 122 |
| 136 {% prettify html %} | 123 {% prettify html %} |
| 137 <form action="http://www.google.com/search" | 124 <form action="http://www.google.com/search" |
| 138 method="GET" | 125 method="GET" |
| 139 target="_blank"> | 126 target="_blank"> |
| 140 ... | 127 ... |
| 141 </form> | 128 </form> |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 and a basic GET request. | 191 and a basic GET request. |
| 205 For forms that contain a lot of data, | 192 For forms that contain a lot of data, |
| 206 or for web applications that communicate with a specialized server, | 193 or for web applications that communicate with a specialized server, |
| 207 you usually need to handle forms programmatically. | 194 you usually need to handle forms programmatically. |
| 208 | 195 |
| 209 The next example shows a more complex form that sends data | 196 The next example shows a more complex form that sends data |
| 210 to a server programmatically using a POST request. | 197 to a server programmatically using a POST request. |
| 211 | 198 |
| 212 ##About the slambook example, specifically {#about-the-slambook-example} | 199 ##About the slambook example, specifically {#about-the-slambook-example} |
| 213 | 200 |
| 214 The primary example in this target consists of two programs. | 201 The primary example in this tutorial consists of two programs. |
| 215 | 202 |
| 216 * First, | 203 * First, |
| 217 a basic server program, called `slambookserver`, | 204 a basic server program, called `slambookserver`, |
| 218 listens on port 4040 on the local host | 205 listens on port 4040 on the local host |
| 219 and handles POST and OPTIONS requests | 206 and handles POST and OPTIONS requests |
| 220 by printing a message and sending a confirmation to the client. | 207 by printing a message and sending a confirmation to the client. |
| 221 The server uses CORS headers to allow requests from applications | 208 The server uses CORS headers to allow requests from applications |
| 222 running from a different origin. | 209 running from a different origin. |
| 223 | 210 |
| 224 * Second, | 211 * Second, |
| 225 the client program, | 212 the client program, |
| 226 called `slambook`, | 213 called `slambook`, |
| 227 provides a form into which users can enter some information. | 214 provides a form into which users can enter some information. |
| 228 It uses Web UI to bind the input data to Dart variables. | 215 It uses Polymer data binding to bind the input data to Dart variables. |
| 229 When the user clicks the submit button, | 216 When the user clicks the submit button, |
| 230 the Dart code formats the data into a JSON string, | 217 the Dart code formats the data into a JSON string, |
| 231 sends it to slambookserver using an OPTIONS request to | 218 sends it to slambookserver using an OPTIONS request to |
| 232 get access first and then a POST request to send the data. | 219 get access first and then a POST request to send the data. |
| 233 When the response from the server is ready, | 220 When the response from the server is ready, |
| 234 the client displays it. | 221 the client displays it. |
| 235 | 222 |
| 236 The following diagram shows the flow of communication between | 223 The following diagram shows the flow of communication between |
| 237 the server and the client in this example. | 224 the server and the client in this example. |
| 238 <hr> | 225 <hr> |
| 239 | 226 |
| 240  | 227  |
| 241 | 228 |
| 242 <hr> | 229 <hr> |
| 243 | 230 |
| 244 **Try it!** | 231 **Try it!** |
| 245 Enter some data and push the **Submit** button. | 232 Enter some data and push the **Submit** button. |
| 246 | 233 |
| 247 <iframe class="running-app-frame" | 234 <iframe class="running-app-frame" |
| 248 style="height:500px;width:525px;" | 235 style="height:500px;width:525px;" |
| 249 src="http://dart-lang.github.io/dart-tutorials-samples/web/target10/slam
book/web/out/slambook.html"> | 236 src="http://dart-lang.github.io/dart-tutorials-samples/web/forms/slamboo
k/web/out/slambook.html"> |
| 250 </iframe> | 237 </iframe> |
| 251 | 238 |
| 252 The complete source code for both the client and the | 239 The complete source code for both the client and the |
| 253 server is available on github: | 240 server is available on github: |
| 254 <a href="https://github.com/dart-lang/dart-tutorials-samples/tree/master/web/tar
get10/slambook" target="_blank">slambook</a> | 241 <a href="https://github.com/dart-lang/dart-tutorials-samples/tree/master/web/for
ms/slambook" target="_blank">slambook</a> |
| 255 | 242 |
| 256 The request gives you an innocent stare and displays "No server" | 243 The request gives you an innocent stare and displays "No server" |
| 257 because you are not running the server on your machine. | 244 because you are not running the server on your machine. |
| 258 Let's fix that. | 245 Let's fix that. |
| 259 | 246 |
| 260 ###Run the server | 247 ###Run the server |
| 261 | 248 |
| 262 Download or copy the source code for the basic server program | 249 Download or copy the source code for the basic server program |
| 263 from the github repository: | 250 from the github repository: |
| 264 <a href="https://github.com/dart-lang/dart-tutorials-samples/tree/master/web/tar
get10/slambook/bin/slambookserver.dart" | 251 <a href="https://github.com/dart-lang/dart-tutorials-samples/tree/master/web/for
ms/slambook/bin/slambookserver.dart" |
| 265 target="_blank">slambookserver.dart</a> | 252 target="_blank">slambookserver.dart</a> |
| 266 | 253 |
| 267 Run the server program from the command line: | 254 Run the server program from the command line: |
| 268 | 255 |
| 269 {% prettify %} | 256 {% prettify %} |
| 270 % dart slambookserver.dart | 257 % dart slambookserver.dart |
| 271 Listening for GET and POST on http://127.0.0.1:4040 | 258 Listening for GET and POST on http://127.0.0.1:4040 |
| 272 {% endprettify %} | 259 {% endprettify %} |
| 273 | 260 |
| 274 Now, you can try submitting data again with the slambook app above. | 261 Now, you can try submitting data again with the slambook app above. |
| 275 | 262 |
| 276 <aside class="alert"> | 263 <aside class="alert"> |
| 277 Note: If another program is already listening on localhost 4040, | 264 Note: If another program is already listening on localhost 4040, |
| 278 the server prints an error message and quits. | 265 the server prints an error message and quits. |
| 279 The app running on this page expects slambookserver to be at localhost 4040, | 266 The app running on this page expects slambookserver to be at localhost 4040, |
| 280 so for the app to work, | 267 so for the app to work, |
| 281 you have to kill the other process and start slambookserver again. | 268 you have to kill the other process and start slambookserver again. |
| 282 Alternatively, | 269 Alternatively, |
| 283 you can change the port number in both the client and the server code. | 270 you can change the port number in both the client and the server code. |
| 284 Avoid using 3030 because Dart Editor listens there. | 271 Avoid using 3030 because Dart Editor listens there. |
| 285 Then run both the client and the server locally on your machine. | 272 Then run both the client and the server locally on your machine. |
| 286 </aside> | 273 </aside> |
| 287 | 274 |
| 288 The rest of this target explains the code for both the client and the server. | 275 The rest of this tutorial explains the code for both the client and the server. |
| 289 | 276 |
| 290 On the client side, you learn about | 277 On the client side, you learn about |
| 291 | 278 |
| 292 * Submitting the form | 279 * Submitting the form |
| 293 * Resetting the form | 280 * Resetting the form |
| 294 * Using Web UI to bind the form data to variables in the Dart program | 281 * Using Polymer to bind the form data to variables in the Dart program |
| 295 (You've seen some data binding in previous targets. We'll talk about | |
| 296 using Web UI with HTML5 widgets at the end of this target.) | |
| 297 | 282 |
| 298 On the server side, the sections cover | 283 On the server side, the sections cover |
| 299 | 284 |
| 300 * CORS headers | 285 * CORS headers |
| 301 * Handling OPTIONS requests | 286 * Handling OPTIONS requests |
| 302 * Handling POST requests | 287 * Handling POST requests |
| 303 | 288 |
| 304 ##Submitting a form | 289 ##Submitting a form |
| 305 | 290 |
| 306 Let's first take a look at how that data is submitted to the server. | 291 Let's first take a look at how that data is submitted to the server. |
| (...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 597 Client-server programs should almost always handle communication | 582 Client-server programs should almost always handle communication |
| 598 and other forms of I/O asynchronously with Futures. | 583 and other forms of I/O asynchronously with Futures. |
| 599 | 584 |
| 600 ##Handling OPTIONS requests | 585 ##Handling OPTIONS requests |
| 601 | 586 |
| 602 With help from the | 587 With help from the |
| 603 <a href="https://api.dartlang.org/dart_html/HttpRequest.html" | 588 <a href="https://api.dartlang.org/dart_html/HttpRequest.html" |
| 604 target="_blank">HttpRequest</a> | 589 target="_blank">HttpRequest</a> |
| 605 class, the slambook client makes a POST request | 590 class, the slambook client makes a POST request |
| 606 when the user clicks the submit button. | 591 when the user clicks the submit button. |
| 607 You saw the code for this earlier in this target. | 592 You saw the code for this earlier in this tutorial. |
| 608 | 593 |
| 609 If the client is running from a different origin than the server, | 594 If the client is running from a different origin than the server, |
| 610 which is common in web apps, | 595 which is common in web apps, |
| 611 the POST request is "preflighted". | 596 the POST request is "preflighted". |
| 612 A preflighted request must first send an OPTIONS request | 597 A preflighted request must first send an OPTIONS request |
| 613 to determine if the actual request is allowed. | 598 to determine if the actual request is allowed. |
| 614 The HttpRequest class | 599 The HttpRequest class |
| 615 automatically handles the preflight OPTIONS request. | 600 automatically handles the preflight OPTIONS request. |
| 616 You do not have to write the client-side code for that. | 601 You do not have to write the client-side code for that. |
| 617 | 602 |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 728 ##Recipe for client-server web apps {#recipe} | 713 ##Recipe for client-server web apps {#recipe} |
| 729 | 714 |
| 730 The slambook client-server example | 715 The slambook client-server example |
| 731 can serve as a starting point for your client-server web apps | 716 can serve as a starting point for your client-server web apps |
| 732 or as a recipe for building your own. | 717 or as a recipe for building your own. |
| 733 | 718 |
| 734 Here's an outline of what the client needs to do. | 719 Here's an outline of what the client needs to do. |
| 735 | 720 |
| 736 * Use forms to gather data from a user. | 721 * Use forms to gather data from a user. |
| 737 * Put input fields in your forms for individual data items. | 722 * Put input fields in your forms for individual data items. |
| 738 * Use Web UI two-way binding to keep the form data in sync with the Dart code. | 723 * Use Polymer two-way binding to keep the form data in sync with the Dart code. |
| 739 * Send data declaratively (action and method attributes on the form) | 724 * Send data declaratively (action and method attributes on the form) |
| 740 * ... or programmatically (overriding the default behavior of | 725 * ... or programmatically (overriding the default behavior of |
| 741 the submit button with Dart code) | 726 the submit button with Dart code) |
| 742 * Get the response from the server from the HttpRequest object. | 727 * Get the response from the server from the HttpRequest object. |
| 743 * Handle communication asynchronously with a Future object. | 728 * Handle communication asynchronously with a Future object. |
| 744 | 729 |
| 745 And, here's what the server needs to do. | 730 And, here's what the server needs to do. |
| 746 | 731 |
| 747 * Use HttpServer to set up a server to listen on a port and host. | 732 * Use HttpServer to set up a server to listen on a port and host. |
| 748 * Listen for requests. | 733 * Listen for requests. |
| 749 * Use CORS headers to set access permissions for each request. | 734 * Use CORS headers to set access permissions for each request. |
| 750 * Respond to requests using HttpResponse. | 735 * Respond to requests using HttpResponse. |
| 751 * Handle communication asynchronously with Futures. | 736 * Handle communication asynchronously with Futures. |
| 752 * Use streams to write the response data. | 737 * Use streams to write the response data. |
| 753 | 738 |
| 754 These resources, mostly from the core Dart libraries, | 739 These resources, mostly from the core Dart libraries, |
| 755 provide support for writing clients and servers. | 740 provide support for writing clients and servers. |
| 756 Note that there are two HttpRequest classes, one in dart:html (for clients) | 741 Note that there are two HttpRequest classes, one in dart:html (for clients) |
| 757 and one in dart:io (for servers). | 742 and one in dart:io (for servers). |
| 758 | 743 |
| 759 | Resource | Library | Description | | 744 | Resource | Library | Description | |
| 760 |---|---| | 745 |---|---| |
| 761 | <a href="https://api.dartlang.org/dart_html/HttpRequest.html" target="_blank">
HttpRequest</a> | dart:html | Client-side HTTP request | | 746 | <a href="https://api.dartlang.org/dart_html/HttpRequest.html" target="_blank">
HttpRequest</a> | dart:html | Client-side HTTP request | |
| 762 | <a href="https://api.dartlang.org/dart_io/HttpRequest.html" target="_blank">Ht
tpRequest</a> | dart:io | Server-side HTTP request | | 747 | <a href="https://api.dartlang.org/dart_io/HttpRequest.html" target="_blank">Ht
tpRequest</a> | dart:io | Server-side HTTP request | |
| 763 | <a href="https://api.dartlang.org/dart_io/HttpServer.html" target="_blank">Htt
pServer</a> | dart:io | Server-side object to handle HTTP communication with cli
ents | | 748 | <a href="https://api.dartlang.org/dart_io/HttpServer.html" target="_blank">Htt
pServer</a> | dart:io | Server-side object to handle HTTP communication with cli
ents | |
| 764 | <a href="https://api.dartlang.org/dart_io/HttpResponse.html" target="_blank">H
ttpResponse</a> | dart:io | Server-side object to carry response to client reque
sts | | 749 | <a href="https://api.dartlang.org/dart_io/HttpResponse.html" target="_blank">H
ttpResponse</a> | dart:io | Server-side object to carry response to client reque
sts | |
| 765 | <a href="https://api.dartlang.org/dart_async/Stream.html" target="_blank">Stre
ams</a> | dart:async | A stream of data | | 750 | <a href="https://api.dartlang.org/dart_async/Stream.html" target="_blank">Stre
ams</a> | dart:async | A stream of data | |
| 766 | <a href="https://api.dartlang.org/dart_async/Future.html" target="_blank">Futu
re</a> | dart:async | A way to get a value asynchronously | | 751 | <a href="https://api.dartlang.org/dart_async/Future.html" target="_blank">Futu
re</a> | dart:async | A way to get a value asynchronously | |
| 767 | <a href="https://api.dartlang.org/dart_json.html" target="_blank">stringify()<
/a> | dart:json | How you format an object into a JSON string | | 752 | <a href="https://api.dartlang.org/dart_json.html" target="_blank">stringify()<
/a> | dart:json | How you format an object into a JSON string | |
| 768 | <a href="https://pub.dartlang.org/packages/web_ui" target="_blank">Web UI pack
age</a> | web_ui | Data binding, templates, and custom elements | | 753 | <a href="https://api.dartlang.org/polymer.html" target="_blank">Polymer</a> |
Polymer | Custom elements, data binding, templates | |
| 769 {: .table} | 754 {: .table} |
| 770 | 755 |
| 771 ##Binding data to different kinds of inputs {#binding-data} | 756 ##Binding data to different kinds of inputs {#binding-data} |
| 772 | 757 |
| 773 The slambook sample uses the Web UI package to | 758 The slambook sample uses Polymer to |
| 774 bind the values of input elements to Dart variables. | 759 bind the values of input elements to Dart variables. |
| 775 If the user changes the value in an input element, | 760 If the user changes the value in an input element, |
| 776 the bound variable in the Dart code automatically changes. | 761 the bound variable in the Dart code automatically changes. |
| 777 Or if the Dart code changes the value of the bound variable, | 762 Or if the Dart code changes the value of the bound variable, |
| 778 the UI automatically updates. | 763 the UI automatically updates. |
| 779 [Target 6: Get Started with Web UI](/docs/tutorials/web-ui/) | 764 [Define a Custom Element](/docs/tutorials/polymer-intro/) |
| 780 provides introductory details | 765 provides introductory details |
| 781 about data binding with Web UI. | 766 about data binding with Polymer. |
| 782 | 767 |
| 783 With this example, | 768 With this example, |
| 784 you can see two-way data binding used with a variety of input elements, | 769 you can see two-way data binding used with a variety of input elements, |
| 785 including new HTML5 elements. | 770 including new HTML5 elements. |
| 786 This table summarizes the two-way data binding attributes | 771 This table summarizes the two-way data binding attributes |
| 787 you can use with Web UI: | 772 you can use with Polymer: |
| 788 | 773 |
| 789 | Attribute | Dart type | Input element | | 774 | Attribute | Dart type | Input element | |
| 790 |---|---| | 775 |---|---| |
| 791 | bind-value | String | any <br>with special considerations for elements <br> th
at allow multiple selection,<br> such as a group of radio buttons and <select
> elements | | 776 | bind-value | String | any <br>with special considerations for elements <br> th
at allow multiple selection,<br> such as a group of radio buttons and <select
> elements | |
| 792 | bind-selected-index | integer | a <select> element in which only one cho
ice is allowed | | 777 | bind-selected-index | integer | a <select> element in which only one cho
ice is allowed | |
| 793 | bind-checked | bool | individual radio buttons or checkboxes | | 778 | bind-checked | bool | individual radio buttons or checkboxes | |
| 794 {: .table .nowraptable} | 779 {: .table .nowraptable} |
| 795 | 780 |
| 796 ###Using bind-value with any input element | 781 ###Using bind-value with any input element |
| 797 | 782 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 824 The slambook form contains three radio buttons, | 809 The slambook form contains three radio buttons, |
| 825 which together make a group | 810 which together make a group |
| 826 because they all have the same name, `catOrDog`. | 811 because they all have the same name, `catOrDog`. |
| 827 All of the radio buttons are bound to the same string. | 812 All of the radio buttons are bound to the same string. |
| 828 Because only one radio button in a group can be selected at a time | 813 Because only one radio button in a group can be selected at a time |
| 829 and each radio button has a different value, | 814 and each radio button has a different value, |
| 830 the bound string reflects the value of the entire group. | 815 the bound string reflects the value of the entire group. |
| 831 | 816 |
| 832  | 817  |
| 833 | 818 |
| 819 {% comment %} |
| 834 ####Special case: Not using bind-value with a multiple select element | 820 ####Special case: Not using bind-value with a multiple select element |
| 835 | 821 |
| 836 By default a <select> element allows | 822 By default a <select> element allows |
| 837 only one option to be selected at a time. | 823 only one option to be selected at a time. |
| 838 For a single selection <select> element, | 824 For a single selection <select> element, |
| 839 use bind-selected-index (explained below) | 825 use bind-selected-index (explained below) |
| 840 to get the index of the selected option. | 826 to get the index of the selected option. |
| 841 If you want to allow the user to select multiple options, | 827 If you want to allow the user to select multiple options, |
| 842 use the `multiple` attribute. | 828 use the `multiple` attribute. |
| 843 | 829 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 854 `multiselect`. | 840 `multiselect`. |
| 855 | 841 |
| 856 **Try it!** | 842 **Try it!** |
| 857 | 843 |
| 858 * Click to select one option. | 844 * Click to select one option. |
| 859 * Shift-click to select options in series. | 845 * Shift-click to select options in series. |
| 860 * Command-click to make a discontiguous selection. | 846 * Command-click to make a discontiguous selection. |
| 861 | 847 |
| 862 <iframe class="running-app-frame" | 848 <iframe class="running-app-frame" |
| 863 style="height:400px;width:500px;" | 849 style="height:400px;width:500px;" |
| 864 src="http://dart-lang.github.io/dart-tutorials-samples/web/target10/mult
iselect/web/out/multiselect.html"> | 850 src="http://dart-lang.github.io/dart-tutorials-samples/web/forms/multise
lect/web/out/multiselect.html"> |
| 865 </iframe> | 851 </iframe> |
| 866 | 852 |
| 867 The source code for this example is available on github: | 853 The source code for this example is available on github: |
| 868 <a href="https://github.com/dart-lang/dart-tutorials-samples/tree/master/web/tar
get10/multiselect" | 854 <a href="https://github.com/dart-lang/dart-tutorials-samples/tree/master/web/for
ms/multiselect" |
| 869 target="_blank">multiselect</a>. | 855 target="_blank">multiselect</a>. |
| 870 | 856 |
| 871 Key-value pairs in a map called `books` contain the data | 857 Key-value pairs in a map called `books` contain the data |
| 872 for the <select> element. | 858 for the <select> element. |
| 873 The keys are strings and the values are boolean. | 859 The keys are strings and the values are boolean. |
| 874 | 860 |
| 875 {% prettify dart %} | 861 {% prettify dart %} |
| 876 final Map<String, bool> books = toObservable( | 862 final Map<String, bool> books = toObservable( |
| 877 { 'The Cat in the Hat': true, 'War and Peace': false, | 863 { 'The Cat in the Hat': true, 'War and Peace': false, |
| 878 'Pride and Prejudice': true, 'On the Road': true, | 864 'Pride and Prejudice': true, 'On the Road': true, |
| 879 'The Hunger Games': true, 'The Java Tutorial': false, | 865 'The Hunger Games': true, 'The Java Tutorial': false, |
| 880 'The Joy of Cooking': false, 'Goodnight Moon': true } | 866 'The Joy of Cooking': false, 'Goodnight Moon': true } |
| 881 ); | 867 ); |
| 882 {% endprettify %} | 868 {% endprettify %} |
| 883 | 869 |
| 884 Using `template repeat` from Web UI, | 870 Using `template repeat` from Polymer, |
| 885 the HTML code puts <option> elements in the | 871 the HTML code puts <option> elements in the |
| 886 <select> element based on the Dart map. | 872 <select> element based on the Dart map. |
| 887 Instead of binding the values of the <option> elements | 873 Instead of binding the values of the <option> elements |
| 888 to the `books` map, | 874 to the `books` map, |
| 889 the HTML code specifies a change event handler | 875 the HTML code specifies a change event handler |
| 890 for the <select> element. | 876 for the <select> element. |
| 891 | 877 |
| 892 {% prettify html %} | 878 {% prettify html %} |
| 893 <select multiple | 879 <select multiple |
| 894 on-change="changeselected($event)" | 880 on-change="changeselected($event)" |
| (...skipping 23 matching lines...) Expand all Loading... |
| 918 This iterative template specifies | 904 This iterative template specifies |
| 919 a Dart getter, called `booksselected`, | 905 a Dart getter, called `booksselected`, |
| 920 that dynamically creates a List object containing the keys | 906 that dynamically creates a List object containing the keys |
| 921 to the selected books. | 907 to the selected books. |
| 922 | 908 |
| 923  | 909  |
| 924 | 910 |
| 925 The `booksselected` getter has an implied dependency on | 911 The `booksselected` getter has an implied dependency on |
| 926 the `books` map, because the getter uses the map. | 912 the `books` map, because the getter uses the map. |
| 927 When `books` changes, the UI updates the unnumbered list of selected books. | 913 When `books` changes, the UI updates the unnumbered list of selected books. |
| 914 {% endcomment %} |
| 928 | 915 |
| 929 ###Using bind-selected-index with a pull-down menu | 916 ###Using bind-selected-index with a pull-down menu |
| 930 | 917 |
| 931 A <select> element contains one or more <option> elements, | 918 A <select> element contains one or more <option> elements, |
| 932 only one of which, by default, can be selected at a time. | 919 only one of which, by default, can be selected at a time. |
| 933 A single-select element is usually implemented as a pull-down menu. | 920 A single-select element is usually implemented as a pull-down menu. |
| 934 You can use the `bind-selected-index` attribute | 921 You can use the `bind-selected-index` attribute |
| 935 to bind a Dart integer to a pull-down menu. | 922 to bind a Dart integer to a pull-down menu. |
| 936 The integer indicates the index of the selected item. | 923 The integer indicates the index of the selected item. |
| 937 Indices begin at 0. | 924 Indices begin at 0. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 951 | 938 |
| 952 <ul> | 939 <ul> |
| 953 <li> The code that handles the communication between the | 940 <li> The code that handles the communication between the |
| 954 client and server is based on code written and explained | 941 client and server is based on code written and explained |
| 955 by Chris Buckett in | 942 by Chris Buckett in |
| 956 <a href="/articles/json-web-service/">Using Dart with JSON Web Services</
a>. | 943 <a href="/articles/json-web-service/">Using Dart with JSON Web Services</
a>. |
| 957 </li> | 944 </li> |
| 958 <li> Check out | 945 <li> Check out |
| 959 <a href="/docs/cookbook/"> | 946 <a href="/docs/cookbook/"> |
| 960 <i class="icon-food"> </i> Dart Cookbook</a>. | 947 <i class="icon-food"> </i> Dart Cookbook</a>. |
| 961 You'll find many recipes related to topics in this Target, | 948 You'll find many recipes related to topics in this tutorial, |
| 962 including Web UI, JSON, and URIs. | 949 including JSON and URIs. |
| 963 </li> | 950 </li> |
| 964 <li> The previous target, | 951 <li> The previous tutorial, |
| 965 <a href="/docs/tutorials/fetchdata/">Fetch Data Dynamically</a>, | 952 <a href="/docs/tutorials/fetchdata/">Fetch Data Dynamically</a>, |
| 966 contains a more basic client program that relies on the server | 953 contains a more basic client program that relies on the server |
| 967 within Dart Editor (port 3030 on localhost), | 954 within Dart Editor (port 3030 on localhost), |
| 968 to serve the contents of a JSON file. | 955 to serve the contents of a JSON file. |
| 969 </li> | 956 </li> |
| 970 </ul> | 957 </ul> |
| 971 | 958 |
| 972 ###What next? | 959 ###What next? |
| 973 | 960 |
| 974 Try our | 961 Try our |
| 975 <a href="/codelabs/web-ui-writer/index.html" target="_blank"><i class="icon-beak
er"> </i>Codelab</a>. | 962 <a href="/codelabs/web-ui-writer/index.html" target="_blank"><i class="icon-beak
er"> </i>Codelab</a>. |
| 976 In this step-by-step guide, you’ll build a simple, | 963 In this step-by-step guide, you’ll build a simple, |
| 977 single-page, modern web app for desktop and mobile. | 964 single-page, modern web app for desktop and mobile. |
| 978 | 965 |
| 979 {% endcapture %} | 966 {% endcapture %} |
| 980 | 967 |
| 981 {% include tutorial.html %} | 968 {% include tutorial.html %} |
| OLD | NEW |