@@ -7,6 +7,8 @@ import Header from './header';
77import Body from './body' ;
88import Store from './store/base' ;
99import PropsBaseResolver from './props-resolver' ;
10+ import Const from './const' ;
11+ import _ from './utils' ;
1012
1113class BootstrapTable extends PropsBaseResolver ( Component ) {
1214 constructor ( props ) {
@@ -16,8 +18,15 @@ class BootstrapTable extends PropsBaseResolver(Component) {
1618 this . store = ! store ? new Store ( props ) : store ;
1719
1820 this . handleSort = this . handleSort . bind ( this ) ;
21+ this . startEditing = this . startEditing . bind ( this ) ;
22+ this . escapeEditing = this . escapeEditing . bind ( this ) ;
23+ this . completeEditing = this . completeEditing . bind ( this ) ;
1924 this . state = {
20- data : this . store . get ( )
25+ data : this . store . get ( ) ,
26+ currEditCell : {
27+ ridx : null ,
28+ cidx : null
29+ }
2130 } ;
2231 }
2332
@@ -39,6 +48,12 @@ class BootstrapTable extends PropsBaseResolver(Component) {
3948 'table-condensed' : condensed
4049 } ) ;
4150
51+ const cellEditInfo = this . resolveCellEditProps ( {
52+ onStart : this . startEditing ,
53+ onEscape : this . escapeEditing ,
54+ onComplete : this . completeEditing
55+ } ) ;
56+
4257 return (
4358 < div className = "react-bootstrap-table-container" >
4459 < table className = { tableClass } >
@@ -55,6 +70,7 @@ class BootstrapTable extends PropsBaseResolver(Component) {
5570 isEmpty = { this . isEmpty ( ) }
5671 visibleColumnSize = { this . visibleColumnSize ( ) }
5772 noDataIndication = { noDataIndication }
73+ cellEdit = { cellEditInfo }
5874 />
5975 </ table >
6076 </ div >
@@ -70,6 +86,39 @@ class BootstrapTable extends PropsBaseResolver(Component) {
7086 } ;
7187 } ) ;
7288 }
89+
90+ completeEditing ( row , column , newValue ) {
91+ const { cellEdit, keyField } = this . props ;
92+ const { beforeSaveCell, onEditing, afterSaveCell } = cellEdit ;
93+ const oldValue = _ . get ( row , column . dataField ) ;
94+ const rowId = _ . get ( row , keyField ) ;
95+ if ( _ . isFunction ( beforeSaveCell ) ) beforeSaveCell ( oldValue , newValue , row , column ) ;
96+ onEditing ( rowId , column . dataField , newValue ) ;
97+ if ( _ . isFunction ( afterSaveCell ) ) afterSaveCell ( oldValue , newValue , row , column ) ;
98+
99+ this . setState ( ( ) => {
100+ return {
101+ data : this . store . get ( ) ,
102+ currEditCell : { ridx : null , cidx : null }
103+ } ;
104+ } ) ;
105+ }
106+
107+ startEditing ( ridx , cidx ) {
108+ this . setState ( ( ) => {
109+ return {
110+ currEditCell : { ridx, cidx }
111+ } ;
112+ } ) ;
113+ }
114+
115+ escapeEditing ( ) {
116+ this . setState ( ( ) => {
117+ return {
118+ currEditCell : { ridx : null , cidx : null }
119+ } ;
120+ } ) ;
121+ }
73122}
74123
75124BootstrapTable . propTypes = {
@@ -81,7 +130,15 @@ BootstrapTable.propTypes = {
81130 striped : PropTypes . bool ,
82131 bordered : PropTypes . bool ,
83132 hover : PropTypes . bool ,
84- condensed : PropTypes . bool
133+ condensed : PropTypes . bool ,
134+ cellEdit : PropTypes . shape ( {
135+ mode : PropTypes . oneOf ( [ Const . CLICK_TO_CELL_EDIT , Const . DBCLICK_TO_CELL_EDIT ] ) . isRequired ,
136+ onEditing : PropTypes . func . isRequired ,
137+ blurToSave : PropTypes . bool ,
138+ beforeSaveCell : PropTypes . func ,
139+ afterSaveCell : PropTypes . func ,
140+ nonEditableRows : PropTypes . func
141+ } )
85142} ;
86143
87144BootstrapTable . defaultProps = {
0 commit comments