Skip to content

Commit

Permalink
Allow overriding default classes on th and td (#161).
Browse files Browse the repository at this point in the history
  • Loading branch information
aslagle committed Feb 19, 2015
1 parent d4ce0af commit 4594572
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 8 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ If you're updating to Meteor 0.8.0, note that reactiveTable is now a template wi
- [Setting columns](#setting-columns)
- [Setting column headers](#setting-column-headers)
- [Column Header CSS class](#column-header-css-class)
- [Cell CSS class](#cell-css-class)
- [Templates](#templates)
- [Virtual columns](#virtual-columns)
- [HTML](#html)
Expand Down Expand Up @@ -185,6 +186,21 @@ To set the css class for table header **<th>**, use the optional *headerCl
},
{ key: 'year', label: 'Year' }
] }

#### Cell CSS Class

To set the css class for the table cells in a column, add the *cellClass* key to the field settings. This attribute can be a String or a Function. The function arguments will be the value for this key, and the full row object.

{ fields: [
{ key: 'name', label: 'Name' , cellClass: 'col-md-4'}, // as String
{ key: 'location', label: 'Location',
cellClass: function (value, object) {
var css = 'col-md2';
'/*do some logic here */
return css;} // as Function
},
{ key: 'year', label: 'Year' }
] }

#### Templates

Expand Down
2 changes: 1 addition & 1 deletion examples/leaderboard/.meteor/versions
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
anti:[email protected]
[email protected]
aslagle:[email protected].6
aslagle:[email protected].9
[email protected]
[email protected]
[email protected]
Expand Down
8 changes: 4 additions & 4 deletions lib/reactive_table.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
{{#each fields}}
{{#if isVisible}}
{{#if isSortKey}}
<th class="{{getKey}} sortable {{getHeaderClass}}" fieldid="{{getFieldFieldId}}">
<th class="sortable {{getHeaderClass}}" fieldid="{{getFieldFieldId}}">
{{#if labelIsTemplate}}{{#with labelData}}{{> ../label}}{{/with}}{{else}}{{getLabel}}{{/if}}&nbsp;&nbsp;
{{#if isAscending}}
{{#if ../useFontAwesome}}
Expand All @@ -68,9 +68,9 @@
</th>
{{else}}
{{#if isSortable}}
<th class="{{getKey}} {{getHeaderClass}} sortable" fieldid="{{getFieldFieldId}}">{{#if labelIsTemplate}}{{#with labelData}}{{> ../label}}{{/with}}{{else}}{{getLabel}}{{/if}}</th>
<th class="{{getHeaderClass}} sortable" fieldid="{{getFieldFieldId}}">{{#if labelIsTemplate}}{{#with labelData}}{{> ../label}}{{/with}}{{else}}{{getLabel}}{{/if}}</th>
{{else}}
<th class="{{getKey}} {{getHeaderClass}}" fieldid="{{getFieldFieldId}}">{{#if labelIsTemplate}}{{#with labelData}}{{> ../label}}{{/with}}{{else}}{{getLabel}}{{/if}}</th>
<th class="{{getHeaderClass}}" fieldid="{{getFieldFieldId}}">{{#if labelIsTemplate}}{{#with labelData}}{{> ../label}}{{/with}}{{else}}{{getLabel}}{{/if}}</th>
{{/if}}
{{/if}}
{{/if}}
Expand All @@ -82,7 +82,7 @@
<tr class="{{../rowClass this}}">
{{#each ../fields}}
{{#if isVisible}}
<td class="{{key}}">{{#if tmpl}}{{#with ..}}{{> ../tmpl}}{{/with}}{{else}}{{getField ..}}{{/if}}</td>
<td class="{{getCellClass ..}}">{{#if tmpl}}{{#with ..}}{{> ../tmpl}}{{/with}}{{else}}{{getField ..}}{{/if}}</td>
{{/if}}
{{/each}}
</tr>
Expand Down
18 changes: 16 additions & 2 deletions lib/reactive_table.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,8 @@ Template.reactiveTable.helpers({
},

'getHeaderClass': function () {
if (!(this.headerClass)) {
return '';
if (_.isUndefined(this.headerClass)) {
return this.key;
}
var css;
if (_.isFunction(this.headerClass)) {
Expand All @@ -341,6 +341,20 @@ Template.reactiveTable.helpers({
return css;
},

'getCellClass': function (object) {
if (_.isUndefined(this.cellClass)) {
return this.key;
}
var css;
if (_.isFunction(this.cellClass)) {
var value = get(object, this.key);
css = this.cellClass(value, object);
} else {
css = this.cellClass;
}
return css;
},

'labelIsTemplate': function () {
return this.label && _.isObject(this.label) && this.label instanceof Blaze.Template;
},
Expand Down
2 changes: 1 addition & 1 deletion package.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package.describe({
summary: "A reactive table designed for Meteor",
version: "0.6.8",
version: "0.6.9",
name: "aslagle:reactive-table",
git: "https://github.com/aslagle/reactive-table.git"
});
Expand Down
38 changes: 38 additions & 0 deletions test/test_fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,44 @@ Tinytest.add('Fields - header class function', function (test) {
);
});

Tinytest.add('Fields - cell class string', function (test) {
testTable(
{
collection: rows,
settings: {
fields: [
{key: 'name', label: 'Name', cellClass: 'name-class'},
{key: 'score', label: 'Score', cellClass: 'score-class' }
]
}
},
function () {
test.length($('.reactive-table th'), 2, "two columns should be rendered");
test.length($('.reactive-table td.name-class'), 6, "first column class");
test.length($('.reactive-table td.score-class'), 6, "second column class");
}
);
});

Tinytest.add('Fields - cell class function', function (test) {
testTable(
{
collection: [{name: 'test', score: 5}],
settings: {
fields: [
{key: 'name', label: 'Name', cellClass: function (value, object) { return value + object.score; }},
{key: 'score', label: 'Score', cellClass: function () { return 'score-class'; }}
]
}
},
function () {
test.length($('.reactive-table th'), 2, "two columns should be rendered");
test.length($('.reactive-table td.test5'), 1, "first column class");
test.length($('.reactive-table td.score-class'), 1, "second column class");
}
);
});


Tinytest.add('Fields - tmpl', function (test) {
testTable(
Expand Down

0 comments on commit 4594572

Please sign in to comment.