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 ; } ;
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-02 14:01:03 +08:00
// import clonedeep from "lodash.clonedeep";
2017-10-12 16:54:17 +08:00
//创建新列存放 “合计” 字段
var columns2 = {
title : "合计" ,
key : "showSum" ,
dataIndex : "showSum"
} ;
2017-09-27 10:59:18 +08:00
2017-10-12 16:54:17 +08:00
var sum = function sum ( Table ) {
return function ( _React$Component ) {
_inherits ( SumTable , _React$Component ) ;
//无状态
function SumTable ( props ) {
_classCallCheck ( this , SumTable ) ;
var _this = _possibleConstructorReturn ( this , _React$Component . call ( this , props ) ) ;
_this . currentFooter = function ( ) {
var data _2 = _this . props . data ;
2017-11-02 14:01:03 +08:00
var columns _sum = _this . props . columns . concat ( ) ;
2017-10-12 16:54:17 +08:00
var sumCol _index = void 0 ;
//用一个对象存储合计数据,这里合计对象的属性对应每列字段
for ( var i = 0 ; i < columns _sum . length ; i ++ ) {
if ( columns _sum [ i ] . sumCol ) {
sumCol _index = columns _sum [ i ] . dataIndex ;
break ;
}
}
var obj = { } ;
obj [ sumCol _index ] = 0 ;
if ( Array . isArray ( data _2 ) ) {
for ( var _i = 0 ; _i < data _2 . length ; _i ++ ) {
if ( typeof data _2 [ _i ] [ sumCol _index ] == "number" ) {
obj [ sumCol _index ] += data _2 [ _i ] [ sumCol _index ] ;
} else {
obj [ sumCol _index ] = "" ;
}
}
}
obj . key = "sumData" ;
obj . showSum = "合计" ;
obj = [ obj ] ;
//将设置的和用户传入的合并属性
// if (columns_sum[0].dataIndex === "checkbox") {
// columns_sum[1] = Object.assign({}, columns_sum[1], columns2);
// } else {
columns _sum [ 0 ] = _extends ( { } , columns _sum [ 0 ] , columns2 ) ;
// }
//除去列为特殊渲染的, 避免像a标签这种html代码写入到合计中
columns _sum . map ( function ( item , index ) {
if ( typeof item . render == "function" ) {
item . render = "" ;
}
return item ;
} ) ;
return _react2 [ "default" ] . createElement ( Table , { showHeader : false , columns : columns _sum , data : obj } ) ;
} ;
return _this ;
}
//合计数字列,并将计算所得数据存储到一个obj对象中
SumTable . prototype . render = function render ( ) {
return _react2 [ "default" ] . createElement ( Table , {
columns : this . props . columns ,
data : this . props . data ,
footer : this . currentFooter
} ) ;
} ;
2017-09-27 10:59:18 +08:00
return SumTable ;
2017-10-12 16:54:17 +08:00
} ( _react2 [ "default" ] . Component ) ;
2017-09-27 10:59:18 +08:00
} ;
2017-10-12 16:54:17 +08:00
exports [ "default" ] = sum ;
module . exports = exports [ "default" ] ;