Skip to content

Commit 4728325

Browse files
Merge pull request #29 from herojobs/master
Porting clickCallback from PivotTable.js
2 parents 0c7ef03 + 6f6e282 commit 4728325

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

examples/App.jsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ class PivotTableUISmartWrapper extends React.PureComponent {
2525
render() {
2626
return <PivotTableUI
2727
renderers={Object.assign({}, TableRenderers, createPlotlyRenderers(Plot))}
28+
rendererOptions = {{
29+
table: {
30+
clickCallback: function(e, value, filters, pivotData){
31+
var names = [];
32+
pivotData.forEachMatchingRecord(filters,
33+
function(record){ names.push(record.Meal); });
34+
alert(names.join("\n"));
35+
}
36+
}
37+
}}
2838
{...this.state.pivotState} onChange={s => this.setState({pivotState: s})}
2939
unusedOrientationCutoff={Infinity}
3040
/>;

src/TableRenderers.jsx

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,36 @@ function makeRenderer(opts = {}) {
106106
}
107107
}
108108

109+
// PutClickHandler
110+
// this.props.rendererOptions.table.clickCallback
111+
const getClickHandler =
112+
this.props.rendererOptions &&
113+
this.props.rendererOptions.table &&
114+
this.props.rendererOptions.table.clickCallback
115+
? (value, rowValues, colValues) => {
116+
const filters = {};
117+
for (const i of Object.keys(colAttrs || {})) {
118+
const attr = colAttrs[i];
119+
if (colValues[i] !== null) {
120+
filters[attr] = colValues[i];
121+
}
122+
}
123+
for (const i of Object.keys(rowAttrs || {})) {
124+
const attr = rowAttrs[i];
125+
if (rowValues[i] !== null) {
126+
filters[attr] = rowValues[i];
127+
}
128+
}
129+
return e =>
130+
this.props.rendererOptions.table.clickCallback(
131+
e,
132+
value,
133+
filters,
134+
pivotData
135+
);
136+
}
137+
: null;
138+
109139
return (
110140
<table className="pvtTable">
111141
<thead>
@@ -199,6 +229,10 @@ function makeRenderer(opts = {}) {
199229
<td
200230
className="pvtVal"
201231
key={`pvtVal${i}-${j}`}
232+
onClick={
233+
getClickHandler &&
234+
getClickHandler(aggregator.value(), rowKey, colKey)
235+
}
202236
style={valueCellColors(
203237
rowKey,
204238
colKey,
@@ -211,6 +245,10 @@ function makeRenderer(opts = {}) {
211245
})}
212246
<td
213247
className="pvtTotal"
248+
onClick={
249+
getClickHandler &&
250+
getClickHandler(totalAggregator.value(), rowKey, [null])
251+
}
214252
style={colTotalColors(totalAggregator.value())}
215253
>
216254
{totalAggregator.format(totalAggregator.value())}
@@ -233,14 +271,24 @@ function makeRenderer(opts = {}) {
233271
<td
234272
className="pvtTotal"
235273
key={`total${i}`}
274+
onClick={
275+
getClickHandler &&
276+
getClickHandler(totalAggregator.value(), [null], colKey)
277+
}
236278
style={rowTotalColors(totalAggregator.value())}
237279
>
238280
{totalAggregator.format(totalAggregator.value())}
239281
</td>
240282
);
241283
})}
242284

243-
<td className="pvtGrandTotal">
285+
<td
286+
onClick={
287+
getClickHandler &&
288+
getClickHandler(grandTotalAggregator.value(), [null], [null])
289+
}
290+
className="pvtGrandTotal"
291+
>
244292
{grandTotalAggregator.format(grandTotalAggregator.value())}
245293
</td>
246294
</tr>
@@ -254,6 +302,7 @@ function makeRenderer(opts = {}) {
254302
TableRenderer.propTypes = PivotData.propTypes;
255303
TableRenderer.defaultProps.tableColorScaleGenerator = redColorScaleGenerator;
256304
TableRenderer.propTypes.tableColorScaleGenerator = PropTypes.func;
305+
TableRenderer.propTypes.rendererOptions = PropTypes.object;
257306
return TableRenderer;
258307
}
259308

0 commit comments

Comments
 (0)