Rendering of polymer element created using template repeat - polymer

In the below code we are able to create 7 div tags dynamicaly, if we want to change the style of third div how to do it?
<template repeat="{{ item in items }}">
<div id="in">q{{item}}<t>{{user.question[item].time}}<t>{{user.question[item].score}} </div>
<br><br>
</template>
the project link in git hub :quiz element

You can do a quick hack
<template repeat="{{ item, i in items }}">
<div class="{{i}} question">q{{item}}<t>{{user.question[item].time}}<t> {{user.question[item].score}} </div>
<br><br>
</template>
And you can use the class name to access it.

You can use a contitional expression with the tokenList filter built in with polymer:
<script src="//cdnjs.cloudflare.com/ajax/libs/polymer/0.3.3/platform.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/polymer/0.3.3/polymer.js"></script>
<polymer-element name="question-list">
<template>
<style>
.thirdQuestion {
border: 1px solid red;
}
</style>
<template repeat="{{ item, index in items }}">
<div class="{{ {thirdQuestion: index === 2} | tokenList }} question">
{{item}}
</div>
<br>
<br>
</template>
</template>
<script>
Polymer({
items: [
"item 1",
"item 2",
"item 3",
"item 4",
"item 5",
"item 6",
"item 7"
]
});
</script>
</polymer-element>
<question-list></question-list>

You can do this easily with pseudo-selectors in CSS.
.question:nth-of-type(3){...}
Here is a live example.

Related

looping through nested array in alpinejs

Nested loop doesn't work. I've checked several times and can't find the problem. My code is :
<script defer src="https://unpkg.com/alpinejs#3.x.x/dist/cdn.min.js"></script>
<div x-data="{ groups:
[{
name:'size',
options:['s','m']
},
{
name:'color',
options:['red','blue']
}]
}">
<template x-for="group in groups">
<h2 x-text="group.name"></h2>
<div>
<template x-for="option in group.options" :key="option">
<div x-text="option"></div>
</template>
</div>
</template>
</div>
Any help is appreciated. Thank you.
Your div placement is wrong.
<script src="https://unpkg.com/alpinejs#3.x.x/dist/cdn.min.js"></script>
<div
x-data="{ groups:
[{
name:'size',
options:['s','m']
},
{
name:'color',
options:['red','blue']
}]
}"
>
<template x-for="group in groups">
<div>
<h2 x-text="group.name"></h2>
<template x-for="option in group.options">
<div x-text="option"></div>
</template>
</div>
</template>
</div>

Want to Display message if the array is empty in Polymerjs

Can someone please help how to make this template show if the filter is not found in the array.
<template is="dom-if" if="{{itemsEmpty}}">
The array is empty!
</template>
here is my entire code. but for some reasons the if condition in the dom-if template is not working
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src='bower_components/webcomponentsjs/webcomponents-lite.min.js'></script>
<link rel="import" href="bower_components/polymer/polymer.html">
<link rel="import" href="bower_components/iron-image/iron-image.html">
<!--<link rel="import" href="bower_components/iron-ajax/iron-ajax.html">-->
<link rel="import" href="bower_components/paper-item/paper-item.html">
<link rel="import" href="bower_components/paper-input/paper-input.html">
<link rel="import" href="bower_components/paper-dropdown-menu/paper-dropdown-menu.html">
<link rel="import" href="bower_components/paper-listbox/paper-listbox.html">
<link rel="import" href="bower_components/paper-button/paper-button.html">
<style>
.taller{
height:120px;
}
[vertical-align="top"] ul {
margin-top: 0;
}
[vertical-align="bottom"] ul {
margin-bottom: 0;
}
button, paper-button {
border: 1px solid #ccc;
background-color: #eee;
/*padding: 1em;*/
border-radius: 3px;
cursor: pointer;
}
button:focus {
outline: none;
border-color: blue;
}
</style>
</head>
<body>
<dom-module id="employee-list">
<template>
<paper-input value="{{filterValue}}" label="Search For a Company" floatingLabel id="searchCompany"></paper-input>
<paper-dropdown-menu label="Select Project Type">
<paper-listbox class="dropdown-content" >
<template is="dom-repeat" items="{{items}}" as="test" filter="{{Filter(filterValue:input)}}">
<paper-item value="{{test.fname}}">{{test.fname}} - {{test.lname}}</paper-item>
</template>
</paper-listbox>
</paper-dropdown-menu>
<paper-listbox >
<template is="dom-repeat" items="{{items}}" filter="{{Filter(filterValue)}}">
<div class="row">
<div class="col-sm-12" style="font-size:15px;font-family:'Open Sans'">
{{item.fname}} - {{item.lname}}
</div>
<hr />
<template is="dom-if" if="{{itemsEmpty}}">
The array is empty!
</template>
</div>
</template>
</paper-listbox>
</template>
<script>
Polymer({
is: 'employee-list',
properties: {
items: {
type: Array,
observer: '_itemsChanged'
},
filterValue: {
type: String,
notify:true
},
itemsEmpty: {
type: Boolean
}
},
ready: function() {
this.items = [{'fname': 'Jack', 'lname':'Bayo'}, {'fname': 'Skellington','lname':'Dar' }];
},
_itemsChanged: function(items){
this.itemsEmpty = items.length == 0;
},
Filter: function (val) {
return function (items) {
if (!items) return false;
if (val != null || val != undefined) {
return (items.fname && ~items.fname.toLowerCase().indexOf(val.toLowerCase())) ||
(items.lname && ~items.lname.toLowerCase().indexOf(val.toLowerCase()));
}
else
return true;
};
}
});
</script>
</dom-module>
<employee-list></employee-list>
</body>
</html>
I will really appreciate any help here. Here is the plunker: http://plnkr.co/edit/Qy6LeAfe93u4CK2G43eX?p=preview
Thank you
I've found a number of problems with your sample:
The polymer.html import breaks your plunk (see all the errors from registerElement). That's because the other imports try to import Polymer from different URLs
The dom-if is inside the dom-repeat
Observing items won't work because the items property isn't changed when you use filter.
What does change is renderedItemCount property, which you can observe and use to control the dom-if. The property is updated whenever filter fires or items array changes.
To sum up:
Remove Polymer import
Move dom-if outside repeater
Add binding to dom-repeat: rendered-item-count="{{renderedCount}}"
Change the if property to use the actually rendered item count: if="{{!renderedCount}}"
Here's how the element's template can look:
<paper-input value="{{filterValue}}" label="Search For a Company" floatingLabel id="searchCompany"></paper-input>
<paper-listbox >
<template is="dom-repeat" items="{{items}}" filter="{{Filter(filterValue)}}" rendered-item-count="{{renderedCount}}">
<div class="row">
<div class="col-sm-12" style="font-size:15px;font-family:'Open Sans'">
{{item.fname}} - {{item.lname}}
</div>
<hr />
</div>
</template>
<template is="dom-if" if="{{!renderedCount}}">
The array is empty!
</template>
</paper-listbox>
This is untested, but I think.
<template is="dom-if" if="{{!items}}">
should be
<template is="dom-if" if="{{itemsEmpty}}">

How can I "refresh" iron-swipeable-container after a change of data in firebase

My code with swipable-container and dom-repeat of firebase-data
<iron-swipeable-container id="teamchat">
<paper-card class="swipe item blue">
<template is="dom-repeat" items="[[tcmmessages]]" as="tcmmessage">
<div class="card-content">
<b>[[tcmmessage.teamname]]</b><br>
[[tcmmessage.beitrag]]<br>
<span class="chatmetadata">von [[tcmmessage.username]]
• [[tcmmessage.update]] • [[tcmmessage.uptime]] </span>
</div>
</template>
</paper-card>
</iron-swipeable-container>
I have defined a listener
listeners: {
'teamchat.iron-swipe': '_onTeamChatSwipe'
},
and remove the firebase-data with '_onTeamChatSwipe'.
That work fine!
But when there is new firebase-data, how can I bring the iron-swipeable-container back again without refreshing the entire page? I don't find a solution.
It looks like you've created just one paper-card that contains multiple messages. When the card is swiped away, your code has no template to refill the container.
Did you actually mean to create a card for each message? That would require moving paper-card inside the template repeater like this:
<iron-swipeable-container id="teamchat">
<template is="dom-repeat" items="[[tcmmessages]]" as="tcmmessage">
<paper-card class="swipe item blue">
<div class="card-content">
<b>[[tcmmessage.teamname]]</b><br>
[[tcmmessage.beitrag]]<br>
<span class="chatmetadata">von [[tcmmessage.username]]
• [[tcmmessage.update]] • [[tcmmessage.uptime]] </span>
</div>
</paper-card>
</template>
</iron-swipeable-container>
When tcmmessages refills (via Firebase), the iron-swipeable-container automatically repopulates with a paper-card per message.
Here's a demo of similar code that shows that behavior:
<head>
<base href="https://polygit.org/polymer+1.4.0/components/">
<script src="webcomponentsjs/webcomponents-lite.min.js"></script>
<link rel="import" href="polymer/polymer.html">
<link rel="import" href="paper-card/paper-card.html">
<link rel="import" href="iron-swipeable-container/iron-swipeable-container.html">
<link rel="import" href="iron-flex-layout/iron-flex-layout-classes.html">
</head>
<body>
<x-foo></x-foo>
<dom-module id="x-foo">
<style include="iron-flex">
paper-card {
margin-bottom: 16px;
}
</style>
<template>
<iron-swipeable-container class="container">
<template is="dom-repeat" items="[[items]]">
<paper-card heading="{{item}}" class="layout vertical">
<div class="card-content">
Swipe me left or right
</div>
</paper-card>
</template>
</iron-swipeable-container>
</template>
<script>
HTMLImports.whenReady(function() {
Polymer({
is: 'x-foo',
properties : {
items: {
type: Array,
value: function() {
return [1,2,3];
}
}
},
_clearListAfterDelay: function(delay) {
this.async(function() {
this.set('items', []);
}, delay);
},
_refillListAfterDelay: function(delay) {
this.async(function() {
this.push('items', 4);
this.push('items', 5);
}, delay);
},
ready: function() {
this._clearListAfterDelay(1000);
this._refillListAfterDelay(2000);
}
});
});
</script>
</dom-module>
</body>
codepen

Polymer iron-media-query not working as previous versions

The following component used to work in 0.5, but doesn't work in 1.0.
If I uncomment the h1 tag to display the values returned by iron-media-query, then the output is just ",,"; Meaning of course the values are just blank.
<dom-module id="app-image">
<!-- Should select correct image based on size -->
<style>
:host {
display: inline-block;
position: relative;
overflow: hidden;
}
:host > ::content img { display: block; }
</style>
<template>
<iron-media-query query="(min-width: 422px)"
queryMatches="{{ isSmall }}"></iron-media-query>
<iron-media-query query="(min-width: 642px)"
queryMatches="{{ isMedium }}"></iron-media-query>
<iron-media-query query="(min-width: 1202px)"
queryMatches="{{ isLarge }}"></iron-media-query>
<!--
Note: The following is just to see what the actual values are for
the specific variables. Unfortunately, it's all just plain blank :(
<h1><span>{{ isSmall }}</span>, <span>{{ isMedium }}</span>, <span>{{ isLarge }}</span></h1>
-->
<template is="dom-if" if="{{ !isSmall }}">
<content select="[tiny]"></content>
</template>
<template is="dom-if" if="{{ isSmall && !isMedium }}">
<content select="[small]"></content>
</template>
<template is="dom-if" if="{{ isMedium && !isLarge }}">
<content select="[medium]"></content>
</template>
<template is="dom-if" if="{{ isLarge }}">
<content select="[large]"></content>
</template>
</template>
</dom-module>
<script>
Polymer({
is: "app-image",
ready: function() {
console.log(this.isSmall);
console.log(this.isMedium);
console.log(this.isLarge);
}
});
</script>
The 3 console.log statements all return undefined, when I'm expecting boolean values. This leads me to believe that the iron-media-query element doesn't actually update the property.
Should iron-media-query be used differently than it's previous incarnation, core-media-query? Is the documentation correct?
ta,
Rewrite queryMatches to query-matches.

Polymer How can I get core-item>core-button to access parent core-list model

I'm learning polymer and I've run into an issue where I have a core-list iterating on a datasource templating a paper-item that houses a core-icon-button.
<core-list id="list" fit>
<template>
<paper-item flex class="row {{ {selected: selected} | tokenList }}">
<core-icon-button id="settings"
icon="settings"
on-tap={{settingTap}}>
</core-icon-button>
{{model.Name}}
</paper-item>
</template>
</core-list>
The problem I am having is that on my buttons' on-tap function I need the parent items' model. Is there a "polymer way" to do what I am describing? Or do I have to extend
<polymer-element name='cust-list'>
<template>
<style>
</style>
<core-list id="list" fit data={{models}}>
<template>
<paper-item flex class="row {{ {selected: selected} | tokenList }}">
<core-icon-button id="settings" icon="settings" on-tap={{settingTap}}></core-icon-button>
{{model.name}}
</paper-item>
</template>
</core-list>
</template>
<script>
Polymer('cust-list', {
models: [{name: "test1"}, {name: "test2"}],
settingTap: function(e,d,s) {
console.log(s.templateInstance.model.model);
}
});
</script>
</polymer-element>
<cust-list></cust-list>