Skip to content
This repository was archived by the owner on Mar 10, 2026. It is now read-only.

Commit 4a59fec

Browse files
author
Justin Slattery
committed
Add rowContext and rowCssClass. Update Readme.md
- Implement row context feature to be used for reducing duplicate computations. - Implement rowCssClass/elementCssClass for more control over styling. - Update Readme.md
1 parent 76710db commit 4a59fec

3 files changed

Lines changed: 38 additions & 8 deletions

File tree

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ tabletable has the following component props:
4141

4242
**filterValue [string]** - text to display in search filter.
4343

44+
**rowContext [func(row: object, index: number): Object]** - callback function invoked once per row with row and index arguments and returns an object. This object will be passed to the column definition object. It is intended to alleviate situations where identical expensive computations need to be performed for more than one column.
45+
46+
**rowCssClass [string]** - CSS class(es) to use for row (tr) element.
47+
4448
### Column definition options
4549
Column definitions are a flexible way to get some fairly complex behaviors into the table while also allowing the *shape* of the data to be however you prefer. Each column defines a function that receives the row data and index as arguments and returns a React component that displays the content. This means that you can drive complex behaviors from the state and props of the parent component. The properties available in the definition objects are:
4650

@@ -52,7 +56,7 @@ Column definitions are a flexible way to get some fairly complex behaviors into
5256

5357
**headerCssClass [string]** - CSS class(es) to use for header column (th) element.
5458

55-
**rowCssClass [string]** - CSS class(es) to use for row column (td) element.
59+
**elementCssClass [string]** - CSS class(es) to use for row column (td) element.
5660

5761
**visible [bool]** - show/hide the column.
5862

@@ -62,7 +66,7 @@ Column definitions are a flexible way to get some fairly complex behaviors into
6266
display: 'Index',
6367
headerCssClass: 'col-sm-1',
6468
visible: true,
65-
data: (row,index) => <div>{index}</div>,
69+
data: (row,index,context) => <div>{index}</div>,
6670
},
6771
name: {
6872
display: 'Name',

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tabletable",
3-
"version": "0.2.0",
3+
"version": "0.2.1",
44
"description": "A simple and extremely flexible table component written in React.",
55
"main": "dist/entry.js",
66
"scripts": {
@@ -30,7 +30,7 @@
3030
"autobind-decorator": "^1.3.2",
3131
"classnames": "^2.2.0",
3232
"immutable": "^3.7.5",
33-
"react": "^0.14.1",
34-
"react-addons-shallow-compare": "^0.14.1"
33+
"react": "^0.14.2",
34+
"react-addons-shallow-compare": "^0.14.2"
3535
}
3636
}

src/Container.jsx

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,43 @@ export default class TabletableContainer extends React.Component {
4646
let takeRows = Math.min(this.props.rowsPerPage, this.props.data.count() - skipRows);
4747

4848
let rows = this.props.data.skip(skipRows).take(takeRows).map((row,index) => {
49-
let rowComponents = [];
49+
// Create row context if required. Make it an immutable so nobody tries to abuse it by shoving stuff into it
50+
// during a column step. We will re-project from the Immutable each time it is used
51+
const context = Immutable.fromJS(this.props.rowContext ? this.props.rowContext(row,index) : {});
52+
53+
// Assign any row classes
54+
let rowCssClass = '';
55+
if (typeof this.props.rowCssClass === 'function') {
56+
rowCssClass = this.props.rowCssClass(row,index,context.toObject());
57+
if (typeof rowCssClass !== 'string') console.error('rowCssClass function must return a string value. Was ' + typeof rowCssClass);
58+
}
59+
else {
60+
rowCssClass = this.props.rowCssClass;
61+
}
5062

63+
// Build out components for the row
64+
let rowComponents = [];
5165
Object.keys(this.props.columns).forEach(k => {
5266
// If visible is false, hide the column. If visible is not defined, default to showing column
5367
if (typeof this.props.columns[k].visible === 'undefined' || this.props.columns[k].visible) {
68+
// elementCssClass can either be a string or a function that returns a string
69+
let elementCssClass = '';
70+
if (typeof this.props.columns[k].elementCssClass === 'function') {
71+
elementCssClass = this.props.columns[k].elementCssClass(row,index,context.toObject());
72+
if (typeof elementCssClass !== 'string') console.error('elementCssClass function must return a string value. Was ' + typeof elementCssClass);
73+
}
74+
else {
75+
elementCssClass = this.props.columns[k].elementCssClass;
76+
}
77+
5478
rowComponents.push(
55-
<td key={`${index}-${k}`} className={this.props.columns[k].rowCssClass}>{this.props.columns[k].data(row,index)}</td>
79+
<td key={`${index}-${k}`} className={elementCssClass}>{this.props.columns[k].data(row,index,context.toObject())}</td>
5680
);
5781
}
5882
});
5983

6084
return (
61-
<tr key={index}>
85+
<tr key={index} className={rowCssClass}>
6286
{rowComponents}
6387
</tr>
6488
);
@@ -160,6 +184,8 @@ TabletableContainer.propTypes = {
160184
showPager: React.PropTypes.bool.isRequired,
161185
showFilter: React.PropTypes.bool.isRequired,
162186
// Optional
187+
rowContext: React.PropTypes.func,
188+
rowCssClass: React.PropTypes.oneOfType([React.PropTypes.string, React.PropTypes.func]),
163189
pager: React.PropTypes.func,
164190
onFilterAction: React.PropTypes.func,
165191
filterValue: React.PropTypes.string,

0 commit comments

Comments
 (0)