how I can bind to the key/value pairs within a json object in polymer notation. i have template repeat="{{objects in jsonarray}}"... I want to lay out a table. say each object has {1: one, 2: two, 3: three}
something like:
<template repeat="{{item in mylist}}">
<tr>
<template repeat="{{key, value in item}}">
<td>{{key}}: {{value}}</td>
</template>
</tr>
</template>
this code works for me:
Dart:
#observable List jsonlist = toObservable(JSON.decode('[{"1":"one"},{"2":"two"}]'));
HTML:
<template repeat="{{ foo in jsonlist }}">
{{ foo }}
<template repeat="{{ key in foo.keys }}">
{{ key }} = {{ foo[key] }}
</template>
</template>
With this code I get the following output:
{1: one} 1 = one {2: two} 2 = two
Regards
Robert
I think it should work this way:
<template repeat="{{item in mylist}}">
<tr>
<template repeat="{{key in item.keys}}">
<td>{{key}}: {{item[key]}}</td>
</template>
</tr>
</template>
Please try and add a comment when it doesn't work (so I get a notification) then I build a demo app and try it myself.
I managed to get this working the way I wanted, which was to output a JSON array of JSON objects to a table, like this:
jsontable.dart
import 'package:polymer/polymer.dart';
import 'dart:html';
import 'dart:convert';
#CustomTag('json-table')
class jsontable extends PolymerElement {
#observable List jsonarray = [{"id":1,"description":"cat"},{"id":2,"description":"dog"}, {"id":3,"description":"fairy"}];
jsontable.created() : super.created() {}
}
jsontable.html
<polymer-element name="json-table">
<template>
<div>
<table border="1">
<tbody>
<thead>
<tr>
<template repeat="{{ key in jsonarray.first.keys }}">
<th>{{key}}</th>
</template>
</tr>
</thead>
<template repeat="{{ jsonobject in jsonarray }}">
<tr>
<template repeat="{{ value in jsonobject.values }}">
<td>{{value}}</td>
</template>
</tr>
</template>
</tbody>
</table>
</div>
</template>
<script type="application/dart;component=1" src="jsontable.dart"></script>
</polymer-element>
Related
<dom-repeat id="xRepeat" as="x">
<template is="dom-repaet">
<div>
<dom-repeat items="[[x]]" as="y">
<template is="dom-repaet">
<div>
<dom-repeat items="[[y]]" as="z">
<template is="dom-repaet">
<span checked-data-id="{{x*y*z}}" >
{{z}}
</span>
</template>
</dom-repeat>
</div>
</template>
</dom-repeat>
</div>
</template>
</dom-repeat>
"{{xyz}}" is no response in Polymer#2.0.
What can I do to make it work?
? ? ?
?
( # # )
I'm feeling ( < ) ...
I have an observable map, which maps arbitrary objects (say another maps) to their id's. When I make changes to these objects, the objects should be updated in the view, too. However, I didn't get it to work. Here is my setup so far:
myexample.html
<polymer-element name="my-example">
<script type="application/dart" src="myexample.dart"></script>
<template>
<style></style>
<div>
<ul>
<template repeat="{{ entry in map.values }}">
<li>{{ entry }}</li>
</template>
</ul>
<button on-click="{{change}}">Change</button>
</div>
</template>
</polymer-element>
myexample.dart
#CustomTag('my-example')
class MyExample extends PolymerElement {
#observable Map<int, String> map = toObservable({'123': {'name': 'XYZ', 'size': 12}});
MyExample.created() : super.created() {
map.changes.listen((_) => notifyPropertyChange(#map, 1, 0));
}
void change() {
var object = map['123']
object['size'] = 100;
map.notifyChange(new MapChangeRecord('123', null, object));
}
}
On clicking the 'Change'-Button, the object with id '123' is updated in the map, but isn't updated in view. Has anyone an idea, how to deliver the changes to the view?
<link rel="import" href="../../packages/polymer/polymer.html">
<polymer-element name="app-element">
<template>
<div>
<ul>
<template repeat="{{ entry in map.values }}">
<li>x{{ entry['name'] }} {{ entry['size']}}</li>
<template repeat="{{ item in entry.values}}">
y{{item}}
</template>
</template>
</ul>
<button on-click="{{change}}">Change</button>
</div>
</template>
<script type="application/dart" src="app_element.dart"></script>
</polymer-element>
import 'package:polymer/polymer.dart';
#CustomTag('app-element')
class AppElement extends PolymerElement {
#observable
Map map = toObservable({'123': {'name': 'XYZ', 'size': 12}});
AppElement.created() : super.created();
void change() {
var object = map['123']['size'] = 100;
}
}
How do I write a numeric for loop in a polymer custom element template? I mean something like
<template repeat="{{for i = 1 to 10}}">
<div>item</td>
</template>
Is it possible in the current version of Dart 1.0?
Currently no, this is not possible in Polymer.dart (or Polymer.js to my knowledge). The repeat binding requires an iterable (See Repeating Templates section of the Polymer_expressions library). Unfortunately due to Issue 12669 it is also not possible to use a list literal to accomplish this either.
Using a filter we can accomplish this:
<!-- myelement.html -->
<polymer-element name="my-element">
<template>
<div>
<template repeat="{{ 5 | myFilter }}">
<p>Write me {{ }}</p>
</template>
</div>
</template>
<script type="application/dart" src="myelement.dart"></script>
</polymer-element>
// myelement.dart
import 'package:polymer/polymer.dart';
import 'package:polymer_expressions/filter.dart';
#CustomTag('my-element')
class MyElement extends PolymerElement {
final Transformer myFilter = new GenerateIterable();
MyElement.created() : super.created();
}
class GenerateIterable extends Transformer<Iterable, int> {
int reverse(Iterable i) => i.length;
Iterable forward(int i) => new Iterable.generate(i, (j) => j + 1);
}
Creating a page which imports myelement.html and using <my-element></my-element> will output:
<div>
<p>Write me 1</p>
<p>Write me 2</p>
<p>Write me 3</p>
<p>Write me 4</p>
<p>Write me 5</p>
</div>
If I have two completely different polymer elements, and in one i have
<template if="{{MyVar}}">htmlhere</template>
<template if="{{!MyVar}}">otherhtmlhere</template>
the other i have
<template if="{{MyVar}}">hello</template>
<template if="{{!MyVar}}">world</template>
what I want to do is if on one MyVar changes, it should change on the other as well... How would I handle this situation?
To explain further what I am looking for is in effect a way to have various bindings / reactions throughout the whole page... so if some method / module somewhere changes MyVar's state it would ripple through the entire page make changes where it should
I have a similar situation where I have an observable object in the root polymer element which I assign to an attribute of the child polymer element.
The child polymer element can then bind to this attribute.
AppModel (global model)
class AppModel extends Object with Observable {
#observable bool isLoggedIn = false;
#observable List<String> authenticationProvider = ['Google', 'Yahoo', 'GitHub', 'Amazon'];
}
#CustomTag("app-element")
class AppElement extends PolymerElement {
#observable AppModel appModel;
AppElement.created() : super.created() {
}
}
AppElement (html) here the global model get's assigned to a child element
<polymer-element name="app-element">
<template>
<my-login id="mylogin" model="{{appModel}}"></my-login>
</template>
...
</polymer-element>
MyLogin (dart) the model Attribute is assigned to the model field.
#CustomTag("my-login")
class MyLogin extends PolymerElement {
MyLogin.created() : super.created();
#published AppModel model;
}
MyLogin (html) the global model is used to show/hide the login button/logged-in user info
<polymer-element name="bwu-login">
<template>
<template if="{{!model.isLoggedIn}}">
<bs-dropdown>
<div class="dropdown">
<div class="dropdown-toggle btn btn-default btn-xs navbar-btn" role="button" data-toggle="dropdown">
<span class="glyphicon glyphicon-log-in"></span> Login
</div>
<ul class="dropdown-menu" role="menu" aria-labelledby="select authentication provider">
<template repeat="{{ap in model.authenticationProvider}}">
<li role="presentation">
<a role="menuitem" tabindex="-1" href="{{ap.authenticationUrl}}" on-click="{{openLogin}}" target="_blank">{{ap.name}}</a>
</li>
</template>
</ul>
</div>
</bs-dropdown>
</template>
<template if="{{model.isLoggedIn}}">
<small>{{model.name}}<template if="{{model.isAdmin}}"> (Admin)</template></small>
<div id="logoutButton" on-click="{{onLogoutHandler}}" class="btn btn-default btn-xs navbar-btn" role="button">
<span class="glyphicon glyphicon-log-out"></span> Logout
</div>
<!--<div><img src="{{model.avatar}}"></img>{{model.name}} <button id="logout">Log out</button></div>-->
</template>
</template>
<script type="application/dart" src='my_login.dart'></script>
</polymer-element>
I would like to embed phone-component.html in another phone-view-component.html custom element. In the columns of the phone-view-component.html there is a 'Type', 'Provider', 'Number' - all text - and a button - 'New' - in the final column. I would like use the 'New' button in the phone-view-component to add a phone-component consisting of a <select>, <datalist> and <tel> and <button> ('Delete') under each of the the first row. So each time I clicked the 'New' button a new row would be inserted. The 'Delete' button would remove the row of which it is a part.
My attempts so far are shown below:
phone-component.html
<!DOCTYPE html>
<polymer-element name='epimss-phone-component'>
<template>
<roole src="reg-roole.rin" monitor="1"></roole>
<table border="1" width="100%">
<tbody>
<tr>
<td>
<select id="phoneCmbo" selectedIndex='{{phoneSelectedIndex}}' >
<option value="{{phone}}"
template repeat="{{phone in phones}}" >{{phone}}
</option>
</select>
</td>
<td>
<input id='providerTxt' type='text' value='{{phone.provider}}' list='providers'>
<datalist id='providers'>
<template repeat='{{provider in providers}}'>
<option value='{{provider}}'>{{provider}}</option>
</template>
</datalist>
</td>
<td><input id='num' type='tel' value='{{phone.num}}'>
</td>
<td>
<button id='deleteBtn on-click={{deleteRow}}'></button>
</td>
<td>
</td>
</tr>
</tbody>
</table>
</template>
<script type="application/dart">
import 'package:polymer/polymer.dart' show CustomTag, PolymerElement, observable;
import 'dart:html' show Event, Node;
import 'package:epimss_polymer/reg_popo.dart' show Phone;
#CustomTag( 'epimss-phone-component' )
class PhoneElement extends PolymerElement
{
#observable Phone phone = new Phone();
List<String> phones = ['', 'Car', 'Fax', 'Home', 'Home Fax', 'Home Mobile',
'Home Video', 'Mobile', 'Pager', 'Work',
'Work Direct', 'Work Fax', 'Work Mobile', 'Work Video',
'Next-of-Kin Home', 'Next-of-Kin Mobile',
'Next-of-Kin Work', 'Tollfree', 'Web Phone'];
List<String> providers = [ '', 'AT&T', 'Digicel', 'Flow', 'Lime' ];
int providerSelectedIndex = 0;
//#observable Map phone = toObservable( new Phone() );
PhoneElement.created() : super.created();
void addRow( Event e, var detail, Node target)
{
print( 'New row created' );
}
void deleteRow( Event e, var detail, Node target)
{
print( 'Current row deleted' );
}
}
</script>
</polymer-element>
phone-view-component.html
<!DOCTYPE html>
<link rel="import" href="phone-component.html">
<polymer-element name='epimss-phone-view-component'>
<template>
<roole src="reg-roole.rin" monitor="1"></roole>
<table border="1" width="100%">
<tbody>
<tr>
<td><label id='typeLbl' for='typeDlst'>Type</label></td>
<td><label id='providerLbl' for='providerDlst'>Provider</label></td>
<td><label id='numberLbl' for='numberTxt'>Number</label></td>
<td><input id='newBtn' type='button' value='New' on-click='{{addRow}}'>
</tr>
</tbody>
</table>
<epimss-phone-component></epimss-phone-component>
</template>
<script type="application/dart">
import 'package:polymer/polymer.dart' show CustomTag, observable;
import 'dart:html' show Event, Node;
import 'package:roole_element/element.dart' show RooleElement;
import 'package:epimss_polymer/reg_popo.dart' show Phone;
#CustomTag( 'epimss-phone-view-component' )
class PhoneViewElement extends RooleElement
{
#observable Phone phone = new Phone();
PhoneViewElement.created() : super.created();
}
</script>
</polymer-element>
phone.dart
part of reg_popo;
class Phone extends Observable
{
String type = '';
String provider = '';
String num = '';
}
I don't know how to place widget in each of the table columns and retrieve each row of new data in a List model.
Any help is appreciated.
Thanks