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