|
| 1 | +### Using mm-repeater |
| 2 | +`mm-repeater` allows the duplication of a set of fields contained within a template tag. |
| 3 | + |
| 4 | +```html |
| 5 | +<mm-repeater id="myRepeater"> |
| 6 | + <template preserve-content> |
| 7 | + <mm-input name="first_name" placeholder="First Name"></mm-input> |
| 8 | + <mm-input name="last_name" placeholder="Last Name"></mm-input> |
| 9 | + <mm-input name="address" placeholder="Address"></mm-input> |
| 10 | + <mm-input name="city" placeholder="City"></mm-input> |
| 11 | + <mm-dropdown name="state" placeholder="State/Province"> |
| 12 | + ... |
| 13 | + </mm-dropdown> |
| 14 | + <mm-input name="zip_code" placeholder="ZIP/Postal Code"></mm-input> |
| 15 | + </template> |
| 16 | +</mm-repeater> |
| 17 | +``` |
| 18 | + |
| 19 | +#### Validation |
| 20 | +Like `mm-form`, `mm-repeater` takes a `config` object, which can take `validation` as a `string` or a custom validation method taking the arguments `value, row:Object, domref:HTMLElement`, and an `errorMessage`. If a custom validation method is used, `this.errorMessage` can be set dynamically. |
| 21 | + |
| 22 | +```javascript |
| 23 | +var myRepeater = document.getElementById('myRepeater'), |
| 24 | + Validator = StrandLib.Validator; |
| 25 | +myRepeater.config = { |
| 26 | + 'first_name': { |
| 27 | + validation: 'alpha' |
| 28 | + }, |
| 29 | + 'last_name': { |
| 30 | + validation: 'alpha' |
| 31 | + }, |
| 32 | + 'address': { |
| 33 | + validation: function(value) { |
| 34 | + var a = value.split(" "), |
| 35 | + street_num = parseInt(a[0]), |
| 36 | + street_name = a[1]; |
| 37 | + return street_num != NaN; |
| 38 | + }, |
| 39 | + }, |
| 40 | + 'city': { |
| 41 | + validation: 'alpha' |
| 42 | + }, |
| 43 | + 'zip_code': { |
| 44 | + validation: function(zip) { |
| 45 | + var z = zip.replace('-',''); |
| 46 | + return parseInt(z) != NaN && (z.length == 5 || z.length == 9); |
| 47 | + }, |
| 48 | + errorMessage: 'Not a valid ZIP' |
| 49 | + } |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +#### Getting data from `mm-repeater` |
| 54 | +User data from repeated form fields are accessible through the `value` property on the `mm-repeater` element. `value` is a getter/setter interface for the `data` property—this ensures Polymer's data binding updates properly. Each object in the `value` array corresponds to a single repeater row, with key-value pairs corresponding to the name-value pairs of the form elements. |
| 55 | + |
| 56 | +Data can be preloaded into the repeater by setting the `value`. This is useful in views where the end user wishes to edit some pre-existing data. |
| 57 | +```javascript |
| 58 | +var myRepeater = document.getElementById('myRepeater'); |
| 59 | +myRepeater.value = [ |
| 60 | + { |
| 61 | + first_name: "Jerry", |
| 62 | + last_name: "Seinfeld", |
| 63 | + address: "2880 Broadway", |
| 64 | + city: "New York", |
| 65 | + state: "NY", |
| 66 | + zip_code: "10025" |
| 67 | + }, |
| 68 | + { |
| 69 | + first_name: "George", |
| 70 | + last_name: "Costanza", |
| 71 | + address: "2880 Broadway", |
| 72 | + city: "New York", |
| 73 | + state: "NY", |
| 74 | + zip_code: "10025" |
| 75 | + } |
| 76 | +]; |
| 77 | +``` |
| 78 | +By default, `mm-repeater` initializes the `value` property with an array containing a single, empty `Object`. This results in a single, blank instance of the template being rendered when the form loads. |
0 commit comments