-
Notifications
You must be signed in to change notification settings - Fork 37
How to run time add classes, attributes, and styles for cells? #34
Comments
I couldn't quite get your code to work as you explain it, but I'll explain how I would do it and hopefully it works for your use-case. Assigning to <template id="example2" bind>
<sortable-table rowTemplate="rt" data="{{myData}}" args="{{myArgs}}">
<template repeat="{{myColumns}}">
<sortable-column class="{{color}}">{{title}}</sortable-column>
</template>
<template id="rt">
<template repeat="{{column,i in columns}}">
<td><input type="{{args[i].type}}" required?="{{args[i].required}}" value="{{record.row[i]}}" style="text-align:{{args[i].type == 'number' ? 'right' : 'left'}};"></td>
</template>
</template>
</sortable-table>
<json-stringify data="{{myData}}" id="dump"></json-stringify><button onclick="document.getElementById('dump').dataChanged();">Refresh</button>
</template>
<script>
window.addEventListener('polymer-ready', function(){
document.getElementById('example2').model = {
myColumns:[
{title:"fruit"},
{title:"alice"},
{title:"bill"},
{title:"casey"}
],
myArgs:[
{type:"text",color:"red",required:true},
{type:"number",color:"blue",required:true},
{type:"number",color:"yellow"},
{type:"number",color:"green"}
],
myData:[
["apple",4,10,2],["banana",0,4,0],["grape",2,3,5],["pear",4,2,8],["strawberry",0,14,0]
]
};
});
</script> I was working on In my example I renamed <template id="example2" bind>
<sortable-table rowTemplate="rt" data="{{data}}" args="{{columns}}">
<template repeat="{{columns}}">
<sortable-column class="{{color}}">{{title}}</sortable-column>
</template>
<template id="rt">
<template repeat="{{column,i in columns}}">
<td><input type="{{args[i].type}}" required?="{{args[i].required}}" value="{{record.row[i]}}" style="text-align:{{args[i].type == 'number' ? 'right' : 'left'}};"></td>
</template>
</template>
</sortable-table>
<json-stringify data="{{data}}" id="dump"></json-stringify><button onclick="document.getElementById('dump').dataChanged();">Refresh</button>
</template>
<script>
window.addEventListener('polymer-ready', function(){
document.getElementById('example2').model = {
columns:[
{title:"fruit",type:"text",color:"red",required:true}
,{title:"alice",type:"number",color:"blue",required:true}
,{title:"bill",type:"number",color:"yellow"}
,{title:"casey",type:"number",color:"green"}
]
,data:[
["apple",4,10,2],["banana",0,4,0],["grape",2,3,5],["pear",4,2,8],["strawberry",0,14,0]
]
};
});
</script> I'm not sure what you mean about breaking 2-way binding... the underlying arrays are passed by reference so you are likely working with the same data but it's just not triggering a data refresh. By default only indexes are observed for changes ( |
Thank you very much for taking time explaining! I learn much from your kind help. |
Class <template repeat="{{columns}}">
<sortable-column class="my-class">{{title}}</sortable-column>
</template> What is the correct way to inject classes into |
Never mind! <sortable-column class="my-class">fruit</sortable-column>
<sortable-column class="my-class">alice</sortable-column>
<sortable-column class="my-class">bill</sortable-column>
<sortable-column class="my-class">casey</sortable-column> while it displays empty child node under <template repeat="{{columns}}">
#document-fragment
</template> Perhaps I am misusing property |
If the class to be defined is constant, you can set an ID on the sortable-table and apply your style using the CSS rules through the Shadow DOM. |
Ooops! I closed my question too soon! My last question remains effective - How to dynamically add classes to element The following code <template repeat="{{columns}}">
<sortable-column class="{{class}}">{{title}}</sortable-column>
</template> now creates child nodes within <template repeat="{{columns}}">
<sortable-column class="red">fruit</sortable-column>
<sortable-column class="blue">alice</sortable-column>
<sortable-column class="yellow">bill</sortable-column>
<sortable-column class="black">casey</sortable-column>
</template> However, this correct result is not quite useful. What I need is to <thead>
<tr>
<th class="red">fruit</th>
<th class="blue">alice</th>
<th class="yellow">bill</th>
<th class="blck">casey</th>
</tr>
</thead> @LM450N: The classes to be added are not constants. Thank you! |
I did not tried this but I think, if the |
I left out support for setting Web Components "best practices" are non-existent, I probably should add support for it because it looks like it could help some of you out. |
Please close this issue for me after implementing this feature to which I am looking forward. |
I seem to have no ending questions :-( Running the above two examples you kindly provided, I change numeric value in Original data: [
["apple",4,10,2],
["banana",0,4,0],
["grape",2,3,5],
["pear",4,2,8],
["strawberry",0,14,0]
] After changing value in [
["apple","5",10,2],
["banana",0,4,0],
["grape",2,3,5],
["pear",4,2,8],
["strawberry",0,14,0]
] The |
Adding attribute myColumns:[
{type:"text",title:"fruit"},
{type:"number",title:"alice"},
{type:"number",title:"bill"},
{type:"number",title:"casey"}
] <template repeat="{{myColumns}}">
<sortable-column datatype="{{type}}">{{title}}</sortable-column>
</template> |
Standard I'm not sure what the best approach for this would be, it likely would be to inherit a new control from the standard |
Then maybe I will manipulate the data types of the result model using Number() or the like before sending it to server. :-) |
Behaviors of
Enlightenment will be greatly appreciated! |
I need to dynamically add classes, attributes, and styles for cells. According to examples
row-template.html
anddom-elements.html
, it seems to me that sortable-table requires occurences of and<td></td>
to be staticly coded like this:My problem is that these occurences are unknown in design time. They are determined by input JSON data. Hence I have this clumsy design:
Notice that I avoid adding column names in input JSON data in order to save some bandwidth.
My code has one big issue - the value entered by user in
<input></input>
loses 2-way data binding!What is the correct design patten in my case?
Thank you!
The text was updated successfully, but these errors were encountered: