Skip to content

Commit

Permalink
onRowClick feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Tony Baeg committed Nov 5, 2015
1 parent 41c229c commit b442d95
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
13 changes: 12 additions & 1 deletion src/BootstrapTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ class BootstrapTable extends React.Component {
sortName={this.props.options.sortName}
sortOrder={this.props.options.sortOrder}
onSort={this.handleSort.bind(this)}
onRowClick={this.handleRowClick.bind(this)}
onSelectAllRow={this.handleSelectAllRow.bind(this)}
bordered={this.props.bordered}>
{this.props.children}
Expand All @@ -184,7 +185,9 @@ class BootstrapTable extends React.Component {
selectRow={this.props.selectRow}
cellEdit={this.props.cellEdit}
selectedRowKeys={this.state.selectedRowKeys}
onSelectRow={this.handleSelectRow.bind(this)}/>
onRowClick={this.handleRowClick.bind(this)}
onSelectRow={this.handleSelectRow.bind(this)}
/>
{tableFilter}
{pagination}
</div>
Expand Down Expand Up @@ -223,6 +226,12 @@ class BootstrapTable extends React.Component {
});
}

handleRowClick(row) {
if (this.props.selectRow.onRowClick) {
this.props.selectRow.onRowClick(row);
}
}

handleSelectAllRow(e) {
var isSelected = e.currentTarget.checked;
let selectedRowKeys = [];
Expand Down Expand Up @@ -503,6 +512,7 @@ BootstrapTable.propTypes = {
mode: React.PropTypes.string,
bgColor: React.PropTypes.string,
selected: React.PropTypes.array,
onRowClick: React.PropTypes.func,
onSelect: React.PropTypes.func,
onSelectAll: React.PropTypes.func,
clickToSelect: React.PropTypes.bool,
Expand Down Expand Up @@ -548,6 +558,7 @@ BootstrapTable.defaultProps = {
mode: Const.ROW_SELECT_NONE,
bgColor: Const.ROW_SELECT_BG_COLOR,
selected: [],
onRowClick: undefined,
onSelect: undefined,
onSelectAll: undefined,
clickToSelect: false,
Expand Down
13 changes: 13 additions & 0 deletions src/TableBody.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ class TableBody extends React.Component{
<TableRow isSelected={selected} key={r} className={trClassName}
selectRow={isSelectRowDefined?this.props.selectRow:undefined}
enableCellEdit={this.props.cellEdit.mode !== Const.CELL_EDIT_NONE}
onRowClick={this.handleRowClick.bind(this)}
onSelectRow={this.handleSelectRow.bind(this)}>
{selectRowColumn}
{tableColumns}
Expand Down Expand Up @@ -158,6 +159,17 @@ class TableBody extends React.Component{
)
}

handleRowClick(rowIndex){
var key, selectedRow;
this.props.data.forEach(function(row, i){
if(i == rowIndex-1){
key = row[this.props.keyField];
selectedRow = row;
}
}, this);
this.props.onRowClick(selectedRow);
}

handleSelectRow(rowIndex, isSelected){
var key, selectedRow;
this.props.data.forEach(function(row, i){
Expand Down Expand Up @@ -234,6 +246,7 @@ TableBody.propTypes = {
condensed: React.PropTypes.bool,
keyField: React.PropTypes.string,
selectedRowKeys: React.PropTypes.array,
onRowClick: React.PropTypes.func,
onSelectRow: React.PropTypes.func
};
export default TableBody;
6 changes: 4 additions & 2 deletions src/TableRow.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ class TableRow extends React.Component{

rowClick(e){
if(e.target.tagName !== "INPUT")
this.props.onSelectRow(e.currentTarget.rowIndex, !this.props.isSelected);
if (this.props.selectRow.clickToSelect) this.props.onSelectRow(e.currentTarget.rowIndex, !this.props.isSelected);
if (this.props.selectRow.onRowClick) this.props.onRowClick(e.currentTarget.rowIndex);
}

render(){
Expand All @@ -18,7 +19,7 @@ class TableRow extends React.Component{
};

if(this.props.selectRow && !this.props.enableCellEdit &&
(this.props.selectRow.clickToSelect || this.props.selectRow.clickToSelectAndEditCell)){
(this.props.selectRow.clickToSelect || this.props.selectRow.clickToSelectAndEditCell) || this.props.selectRow.onRowClick){
return(
<tr {...trCss} onClick={this.rowClick.bind(this)}>{this.props.children}</tr>
)
Expand All @@ -32,6 +33,7 @@ class TableRow extends React.Component{
TableRow.propTypes = {
isSelected: React.PropTypes.bool,
enableCellEdit: React.PropTypes.bool,
onRowClick: React.PropTypes.func,
onSelectRow: React.PropTypes.func
};
export default TableRow;

0 comments on commit b442d95

Please sign in to comment.