2017-10-12 16:54:17 +08:00
"use strict" ;
2017-09-27 10:59:18 +08:00
Object . defineProperty ( exports , "__esModule" , {
2017-10-12 16:54:17 +08:00
value : true
2017-09-27 10:59:18 +08:00
} ) ;
2017-10-12 16:54:17 +08:00
var _extends = Object . assign || function ( target ) { for ( var i = 1 ; i < arguments . length ; i ++ ) { var source = arguments [ i ] ; for ( var key in source ) { if ( Object . prototype . hasOwnProperty . call ( source , key ) ) { target [ key ] = source [ key ] ; } } } return target ; } ;
2017-11-29 16:51:07 +08:00
exports [ "default" ] = sum ;
2017-10-12 16:54:17 +08:00
var _react = require ( "react" ) ;
2017-09-27 10:59:18 +08:00
var _react2 = _interopRequireDefault ( _react ) ;
function _interopRequireDefault ( obj ) { return obj && obj . _ _esModule ? obj : { "default" : obj } ; }
function _defaults ( obj , defaults ) { var keys = Object . getOwnPropertyNames ( defaults ) ; for ( var i = 0 ; i < keys . length ; i ++ ) { var key = keys [ i ] ; var value = Object . getOwnPropertyDescriptor ( defaults , key ) ; if ( value && value . configurable && obj [ key ] === undefined ) { Object . defineProperty ( obj , key , value ) ; } } return obj ; }
function _classCallCheck ( instance , Constructor ) { if ( ! ( instance instanceof Constructor ) ) { throw new TypeError ( "Cannot call a class as a function" ) ; } }
function _possibleConstructorReturn ( self , call ) { if ( ! self ) { throw new ReferenceError ( "this hasn't been initialised - super() hasn't been called" ) ; } return call && ( typeof call === "object" || typeof call === "function" ) ? call : self ; }
function _inherits ( subClass , superClass ) { if ( typeof superClass !== "function" && superClass !== null ) { throw new TypeError ( "Super expression must either be null or a function, not " + typeof superClass ) ; } subClass . prototype = Object . create ( superClass && superClass . prototype , { constructor : { value : subClass , enumerable : false , writable : true , configurable : true } } ) ; if ( superClass ) Object . setPrototypeOf ? Object . setPrototypeOf ( subClass , superClass ) : _defaults ( subClass , superClass ) ; }
2017-11-29 16:51:07 +08:00
function sum ( Table ) {
2017-10-12 16:54:17 +08:00
return function ( _React$Component ) {
_inherits ( SumTable , _React$Component ) ;
//无状态
function SumTable ( props ) {
_classCallCheck ( this , SumTable ) ;
2018-04-18 16:10:02 +08:00
//array , tree
2017-10-12 16:54:17 +08:00
var _this = _possibleConstructorReturn ( this , _React$Component . call ( this , props ) ) ;
2018-04-18 16:10:02 +08:00
_this . getNodeItem = function ( array , newArray ) {
array . forEach ( function ( da , i ) {
if ( da . children ) {
_this . getNodeItem ( da . children , newArray ) ;
} else {
newArray . push ( da ) ;
}
} ) ;
} ;
_this . getTableType = function ( ) {
var columns = _this . props . columns ;
var type = "array" ;
columns . find ( function ( da , i ) {
if ( da . children ) {
type = "tree" ;
return type ;
}
} ) ;
return type ;
} ;
2018-12-16 22:22:45 +08:00
_this . addSumData = function ( ) {
var _this$props = _this . props ,
_this$props$data = _this$props . data ,
data = _this$props$data === undefined ? [ ] : _this$props$data ,
_this$props$columns = _this$props . columns ,
columns = _this$props$columns === undefined ? [ ] : _this$props$columns ;
var sumdata = { } ,
newColumns = [ ] ,
newData = [ ] ;
2018-04-18 16:10:02 +08:00
if ( ! Array . isArray ( columns ) ) {
2018-12-16 22:22:45 +08:00
console . log ( "columns type is error !" ) ; return ;
2018-04-18 16:10:02 +08:00
}
var type = _this . getTableType ( ) ;
2018-12-16 22:22:45 +08:00
if ( type == 'tree' ) {
_this . getNodeItem ( columns , newColumns ) ;
2018-04-18 16:10:02 +08:00
} else {
2018-12-16 22:22:45 +08:00
newColumns = columns ;
2018-04-18 16:10:02 +08:00
}
2018-12-16 22:22:45 +08:00
//返回一个新的数据
newData = data . slice ( ) ;
newColumns . forEach ( function ( column , index ) {
sumdata [ column . dataIndex ] = "" ;
if ( column . sumCol ) {
var count = 0 ;
data . forEach ( function ( da , i ) {
var _num = parseInt ( da [ column . key ] ) ;
//排查字段值为NAN情况
if ( _num === _num ) {
count += _num ;
}
} ) ;
sumdata [ column . dataIndex ] = count ;
}
if ( index == 0 ) {
sumdata [ column . dataIndex ] = "合计 " + sumdata [ column . dataIndex ] ;
}
} ) ;
newData . push ( sumdata ) ;
return newData ;
2018-04-18 16:10:02 +08:00
} ;
_this . tableType = "array" ;
2017-10-12 16:54:17 +08:00
return _this ;
}
2018-04-24 10:40:10 +08:00
2018-04-18 16:10:02 +08:00
/ * *
* 获取当前的表格类型 。
*
* /
2017-10-12 16:54:17 +08:00
SumTable . prototype . render = function render ( ) {
2017-12-24 21:42:58 +08:00
return _react2 [ "default" ] . createElement ( Table , _extends ( { } , this . props , {
2017-10-12 16:54:17 +08:00
columns : this . props . columns ,
2018-12-16 22:22:45 +08:00
showSum : true ,
data : this . addSumData ( )
2017-12-24 21:42:58 +08:00
} ) ) ;
2017-10-12 16:54:17 +08:00
} ;
2017-09-27 10:59:18 +08:00
return SumTable ;
2017-10-12 16:54:17 +08:00
} ( _react2 [ "default" ] . Component ) ;
2017-12-24 21:42:58 +08:00
}
2017-10-12 16:54:17 +08:00
module . exports = exports [ "default" ] ;