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 ) ;
2019-07-26 09:47:18 +08:00
var _utils = require ( "./utils" ) ;
2018-12-19 15:45:25 +08:00
2017-09-27 10:59:18 +08:00
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 ) {
2020-07-08 14:52:57 +08:00
var precision = arguments . length > 1 && arguments [ 1 ] !== undefined ? arguments [ 1 ] : 2 ;
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 ) {
2018-12-19 15:45:25 +08:00
var _num = parseFloat ( da [ column . key ] ) ;
2018-12-16 22:22:45 +08:00
//排查字段值为NAN情况
2018-12-19 16:32:42 +08:00
if ( _num == _num ) {
2018-12-16 22:22:45 +08:00
count += _num ;
}
} ) ;
2020-07-13 21:14:33 +08:00
var _sum = ( 0 , _utils . DicimalFormater ) ( count , precision ) ;
2020-10-14 15:25:17 +08:00
if ( column . sumThousandth ) {
_sum = _this . toThousands ( _sum ) ;
}
2020-07-13 21:14:33 +08:00
sumdata [ column . dataIndex ] = _sum ;
if ( column . sumRender && typeof column . sumRender == 'function' ) {
sumdata [ column . dataIndex ] = column . sumRender ( _sum ) ;
}
2018-12-16 22:22:45 +08:00
}
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
2019-07-11 10:45:29 +08:00
/ * *
* 获取当前的表格类型 。
2020-10-14 15:25:17 +08:00
*
2018-04-18 16:10:02 +08:00
* /
2020-10-14 15:25:17 +08:00
SumTable . prototype . toThousands = function toThousands ( num ) {
var result = '' ,
counter = 0 ;
num = ( num || 0 ) . toString ( ) ;
var numArr = num . split ( '.' ) ;
num = numArr [ 0 ] ;
for ( var i = num . length - 1 ; i >= 0 ; i -- ) {
counter ++ ;
result = num . charAt ( i ) + result ;
if ( ! ( counter % 3 ) && i != 0 ) {
result = ',' + result ;
}
}
return numArr . length === 1 ? result : result + '.' + numArr [ 1 ] ;
} ;
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" ] ;