Skip to content

Commit 6240238

Browse files
committed
Merge pull request #17 from kxmas/dateformat
custom renderer
2 parents 82e36d0 + b675f9c commit 6240238

File tree

4 files changed

+94
-8
lines changed

4 files changed

+94
-8
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ Attribute | Options | Default | Descri
139139
`data-choices` | *Array* | [] | options for select dropdown, in editing and searching.
140140
`hint` | *String* | '' | this text will be displayed at the column header for instruction.
141141
`searchplaceholder` | *String* | '' | this text will be displayed in search filter input box.
142+
`renderer` | *String* | undefined | function name to format the object being displayed
142143

143144

144145
## Browser Compatability

dist/aha-table.html

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -655,9 +655,13 @@
655655
}
656656
return classList.join(' ');
657657
},
658-
readContent: function(row, column) {
658+
readContent: function(row, column) {
659659
var datum = row[column.name];
660-
if (datum === null || datum === undefined
660+
if (column.renderer !== null && column.renderer !== undefined
661+
&& typeof column.renderer === 'string' && column.renderer.trim().length > 0) {
662+
datum = this._render(column.renderer, datum, row, column);
663+
}
664+
if (datum === null || datum === undefined
661665
|| typeof datum === 'string' && datum.trim().length === 0) {
662666
var options = column.options;
663667
var blank = column.placeholder;
@@ -1103,7 +1107,37 @@
11031107
e.target.classList.toggle('search');
11041108
e.target.classList.toggle('nosearch');
11051109
this.$$('#filter').classList.toggle('hide');
1106-
}
1110+
},
1111+
_render: function(name, datum, row, col) {
1112+
var args = [ datum, row, col, this ];
1113+
var displayValue = this._invokeRenderer(datum, name, args);
1114+
if (displayValue !== null) {
1115+
return displayValue;
1116+
}
1117+
displayValue = this._invokeRenderer(row, name, args);
1118+
if (displayValue !== null) {
1119+
return displayValue;
1120+
}
1121+
1122+
displayValue = this._invokeRenderer(this, name, args);
1123+
if (displayValue !== null) {
1124+
return displayValue;
1125+
}
1126+
1127+
displayValue = this._invokeRenderer(this.domHost, name, args);
1128+
if (displayValue === null || displayValue === undefined) {
1129+
displayValue = datum;
1130+
}
1131+
return displayValue;
1132+
},
1133+
_invokeRenderer: function (b, name, args) {
1134+
var fn = b[name];
1135+
if (fn) {
1136+
return fn.apply(this, args || Polymer.nar);
1137+
}
1138+
return null;
1139+
}
1140+
11071141
});
11081142
</script>
11091143

@@ -1188,7 +1222,11 @@
11881222
searchplaceholder: {
11891223
type: String,
11901224
value: ""
1191-
}
1225+
},
1226+
//renderer - function called to display value
1227+
renderer: {
1228+
type: String
1229+
}
11921230
}
11931231
});
11941232
</script>

index.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,12 @@ <h2>Advanced usage: themed, customized columns, customized attributes, customize
137137

138138
required
139139

140+
placeholder=""
141+
default=""></aha-column>
142+
<aha-column name="title"
143+
label="Renderer"
144+
type="string"
145+
renderer="customRenderer"
140146
placeholder=""
141147
default=""></aha-column>
142148
</aha-table>
@@ -190,6 +196,9 @@ <h2><a href="demo/performance.html">Performance Test</a></h2>
190196
themed.addEventListener('after-move-up', function(e) {
191197
table_log('moved one record up!');
192198
});
199+
themed.customRenderer = function(datum, row, col, ahaTable) {
200+
return datum.toUpperCase();
201+
}
193202
</script>
194203
</body>
195204
</html>

src/aha-table.html

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -655,9 +655,13 @@
655655
}
656656
return classList.join(' ');
657657
},
658-
readContent: function(row, column) {
658+
readContent: function(row, column) {
659659
var datum = row[column.name];
660-
if (datum === null || datum === undefined
660+
if (column.renderer !== null && column.renderer !== undefined
661+
&& typeof column.renderer === 'string' && column.renderer.trim().length > 0) {
662+
datum = this._render(column.renderer, datum, row, column);
663+
}
664+
if (datum === null || datum === undefined
661665
|| typeof datum === 'string' && datum.trim().length === 0) {
662666
var options = column.options;
663667
var blank = column.placeholder;
@@ -1103,7 +1107,37 @@
11031107
e.target.classList.toggle('search');
11041108
e.target.classList.toggle('nosearch');
11051109
this.$$('#filter').classList.toggle('hide');
1106-
}
1110+
},
1111+
_render: function(name, datum, row, col) {
1112+
var args = [ datum, row, col, this ];
1113+
var displayValue = this._invokeRenderer(datum, name, args);
1114+
if (displayValue !== null) {
1115+
return displayValue;
1116+
}
1117+
displayValue = this._invokeRenderer(row, name, args);
1118+
if (displayValue !== null) {
1119+
return displayValue;
1120+
}
1121+
1122+
displayValue = this._invokeRenderer(this, name, args);
1123+
if (displayValue !== null) {
1124+
return displayValue;
1125+
}
1126+
1127+
displayValue = this._invokeRenderer(this.domHost, name, args);
1128+
if (displayValue === null || displayValue === undefined) {
1129+
displayValue = datum;
1130+
}
1131+
return displayValue;
1132+
},
1133+
_invokeRenderer: function (b, name, args) {
1134+
var fn = b[name];
1135+
if (fn) {
1136+
return fn.apply(this, args || Polymer.nar);
1137+
}
1138+
return null;
1139+
}
1140+
11071141
});
11081142
</script>
11091143

@@ -1188,7 +1222,11 @@
11881222
searchplaceholder: {
11891223
type: String,
11901224
value: ""
1191-
}
1225+
},
1226+
//renderer - function called to display value
1227+
renderer: {
1228+
type: String
1229+
}
11921230
}
11931231
});
11941232
</script>

0 commit comments

Comments
 (0)