I am trying to create a simple Polymer component that renders a table from an array of data. Example of the intended usage of said component would be the following:
<my-table data="{{someArray}}">
<my-column header="Id"><template>{{item.id}}</template></my-column>
<my-column header="Name"><template>{{item.name}}</template></my-column>
</my-table>
And the render should look like this:
However, upon creating a semi-working prototype, things get complicated. The prototype can be found here: http://jsbin.com/sirutusupu/edit?html,console,output. Disclaimer: it doesn't work unless you download it an run it through a local http-server
.
My first question: why does the prototype only work via local http-server
?
My second question: when running locally and when I wrap the custom element with a dom-bind
, it also stops working. Local code (that is also not working):
<template is="dom-bind">
<my-table>
<my-column header="Id"><template>{{item.id}}</template></my-column>
<my-column header="Name"><template>{{item.name}}</template></my-column>
</my-table>
</template>
My third question: using functions to format output doesn't work. Consider this extended example:
<script>
function concat(a, b) {
return a + "-" + b;
}
</script>
<my-table>
<my-column header="Id"><template>{{item.id}}</template></my-column>
<my-column header="Name"><template>{{item.name}}</template></my-column>
<my-column header="Computed"><template>{{concat(item.id, item.name)}}</template></my-column>
</my-table>
The resulting error is polymer.html:1660 [undefined::_annotatedComputationEffect]: compute method 'concat' not defined
.
Is there a way to get around this without defining Computed bindings? Otherwise the custom formatting of cell values is not possible.
I think one problem you have is the definition of
columns
. It is OUTSIDE of theproperties
objectIt should look like this
The second issue is the use of
<template>
inside the<template is="dom-bind">
which is why it stops working. Not sure why you are doing that.When you say local http-server how is that configured?. It is often necessary to make sure that the imports are referencing the right place when then have hrefs like
../polymer/polymer.html
The tools that Polymer provides (such as polyserve and web-component-tester) all map bower_components is special ways so you can refer to the elements with the../
style. Are you using one of their local http-server or something you crafted? You can do it with other web servers (like apache or ngnix) you just have to realise that you need to change the url mapping to file directory mapping so it works.EDIT
Right at the bottom of your jsbin you do this
However your
<my-table>
element doesn't use the<content>
tag which is what will pull that content in. I am not sure its necessary - I thought your top level element template for<my-table>
already contains the columns.The solution for question #2 (data binding does not work when the component is wrapped by
dom-bind
) is the following. You need to implement the following methods, as defined by the Templatizer behaviour:_forwardParentProp: function(prop, value)
_forwardParentPath: function(path, value)
_showHideChildren: function(shouldHide)
You also need to set the
_instanceProps
to define any instance-related properties.You have an interesting construction here!
rawgit
, I usedpolygit
instead.ctor.prototype
after Templatizing.dom.bind
is going to mess up the interiortemplate
, I would avoid it.This one seems to work: