123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334 |
- import React, { PureComponent, Fragment, Component } from 'react';
- import { Table, Alert, Input, Select } from 'antd';
- import { sldEmptyHandle1,sldComLanguage } from '@/utils/utils';
- import styles from './index.less';
- import global from '@/global.less';
- import { Scrollbars } from 'react-custom-scrollbars';
- import { Resizable } from 'react-resizable';
- import SldRowMoreColumnLR from '@/components/SldRowMoreColumnLR';
- const InputGroup = Input.Group;
- const Option = Select.Option;
- function initTotalList(columns) {
- const totalList = [];
- columns.forEach(column => {
- if (column.needTotal) {
- totalList.push({ ...column, total: 0 });
- }
- });
- return totalList;
- }
- const ResizeableTitle = (props) => {
- const { onResize, width, ...restProps } = props;
- if (!width) {
- return <th {...restProps} />;
- }
- return (
- <Resizable width={width ? width : 100} height={0} onResize={onResize}>
- <th {...restProps} />
- </Resizable>
- );
- };
- class StandardTable extends Component {
- constructor(props) {
- super(props);
- const { columns } = props;
- const needTotalList = initTotalList(columns);
- this.state = {
- selectedRowKeys: [],
- needTotalList,
- rowId: '',// 选中行的id
- preScrollBarToTop: 0,// Scrollbars滚动条是否返回顶部的标识,默认为0,不返回,逐渐加1——上一次
- scrollBarToTop: 0,// Scrollbars滚动条是否返回顶部的标识,默认为0,不返回,逐渐加1——当前
- };
- }
- static getDerivedStateFromProps(nextProps,prevState) {
- if(nextProps.scrollBarToTop != undefined && nextProps.scrollBarToTop > 0 && (nextProps.scrollBarToTop - prevState.preScrollBarToTop > 0)){
- return {scrollBarToTop: nextProps.scrollBarToTop, preScrollBarToTop: nextProps.scrollBarToTop - 1 };
- }
- // clean state
- if (nextProps.selectedRows != undefined && nextProps.selectedRows.length === 0) {
- const needTotalList = initTotalList(nextProps.columns);
- return {
- selectedRowKeys: [],
- needTotalList,
- };
- } else {
- if (nextProps.flag_show_sele_data != undefined && nextProps.flag_show_sele_data) {
- return {
- selectedRowKeys: nextProps.selectedRowKeys,
- };
- }
- }
- return null;
- }
- componentDidUpdate(prevProps, prevState, snapshot) {
- const {scrollBarToTop,preScrollBarToTop} = this.state;
- if(scrollBarToTop > 0 && scrollBarToTop - preScrollBarToTop > 0){
- if(this.refs!=undefined&&this.refs.scrollRef!=undefined&&this.refs.scrollRef.getScrollTop()>0){
- this.refs.scrollRef.scrollToTop();
- this.setState({preScrollBarToTop:scrollBarToTop})
- }
- }
- }
- handleRowSelectChange = (selectedRowKeys, selectedRows) => {
- let { needTotalList } = this.state;
- needTotalList = needTotalList.map(item => ({
- ...item,
- total: selectedRows.reduce((sum, val) => sum + parseFloat(val[item.dataIndex], 10), 0),
- }));
- const { onSelectRow } = this.props;
- if (onSelectRow) {
- onSelectRow(selectedRows, selectedRowKeys);
- }
- this.setState({ selectedRowKeys, needTotalList });
- };
- handleTableChange = (pagination, filters, sorter) => {
- const { onChange } = this.props;
- if (onChange) {
- onChange(pagination, filters, sorter);
- }
- };
- cleanSelectedKeys = () => {
- this.handleRowSelectChange([], []);
- };
- //表格单行点击事件
- sldHandleRow = (record, index, rowKey) => {
- this.props.onSldHandleSeleRow(record);
- let cur_id = '';
- if (rowKey == 'id') {
- cur_id = record.id;
- } else if (rowKey == 'upTime') {
- cur_id = record.upTime;
- } else if (rowKey == 'specId') {
- cur_id = record.specId;
- } else if (rowKey == 'brandId') {
- cur_id = record.brandId;
- } else if (rowKey == 'goodsId') {
- cur_id = record.goodsId;
- } else if (rowKey == 'categoryId') {
- cur_id = record.categoryId;
- } else if (rowKey == 'decoId') {
- cur_id = record.decoId;
- } else if (rowKey == 'seckillId') {
- cur_id = record.seckillId;
- } else if (rowKey == 'labelId') {
- cur_id = record.labelId;
- } else if (rowKey == 'integralGoodsId') {
- cur_id = record.integralGoodsId;
- } else if (rowKey == 'couponId') {
- cur_id = record.couponId;
- } else if (rowKey == 'attributeId') {
- cur_id = record.attributeId;
- } else if (rowKey == 'drawId') {
- cur_id = record.drawId;
- }
- this.setState({
- rowId: cur_id,
- });
- };
- //设置选中行的背景色
- setRowClassName = (rowKey, record) => {
- if (this.props.onSldHandleSeleRow) {
- let cur_id = '';
- if (rowKey == 'id') {
- cur_id = record.id;
- }else if (rowKey == 'specId') {
- cur_id = record.specId;
- } else if (rowKey == 'brandId') {
- cur_id = record.brandId;
- } else if (rowKey == 'goodsId') {
- cur_id = record.goodsId;
- }else if (rowKey == 'categoryId') {
- cur_id = record.categoryId;
- }else if (rowKey == 'decoId') {
- cur_id = record.decoId;
- }else if (rowKey == 'seckillId') {
- cur_id = record.seckillId;
- } else if (rowKey == 'labelId') {
- cur_id = record.labelId;
- } else if (rowKey == 'integralGoodsId') {
- cur_id = record.integralGoodsId;
- } else if (rowKey == 'couponId') {
- cur_id = record.couponId;
- } else if (rowKey == 'attributeId') {
- cur_id = record.attributeId;
- } else if (rowKey == 'drawId') {
- cur_id = record.drawId;
- }
- let showClassName = '';
- if (this.props.showMarkColor && record.mark_color) {
- //去掉#
- let color = record.mark_color.replace('#', '');
- //小写转为大写,方便获取类名
- showClassName = 'row_sld_color_' + color.toUpperCase();
- }
- if (cur_id === this.state.rowId) {
- showClassName = 'seleSingle';
- }
- return showClassName;
- } else {
- return '';
- }
- };
- components = {
- header: {
- cell: ResizeableTitle,
- },
- };
- handleResize = index => (e, { size }) => {
- if (this.props.resizeTable) this.props.resizeTable(index, size);
- };
- onExpand = (expanded, record) => {
- if (this.props.onExpand) {
- this.props.onExpand(expanded, record);
- }
- };
- render() {
- const { selectedRowKeys, needTotalList } = this.state;
- const {
- data: { list, pagination },
- rowKey, isCheck, sldSeleSingleRow, sldpagination, scroll, isColumnResize,
- ...rest
- } = this.props;
- const paginationProps = {
- showSizeChanger: true,
- showQuickJumper: true,
- ...pagination,
- };
- const rowSelection = {
- type: this.props.sel_type != undefined ? this.props.sel_type : 'checkbox',
- selectedRowKeys:this.props.selectedRowKeys!=undefined?this.props.selectedRowKeys:selectedRowKeys,
- onChange: this.handleRowSelectChange,
- getCheckboxProps: record => ({
- disabled: (record.disabled == 'true' || record.disabled == true) ? true : false,
- }),
- };
- const columns = isColumnResize ? this.props.columns.map((col, index) => ({
- ...col,
- onHeaderCell: column => ({
- width: column.width,
- onResize: this.handleResize(index),
- }),
- })) : this.props.columns;
- let showData = <div className={styles.standardTableSlo_don}
- style={{ width: this.props.width != undefined ? this.props.width : '100%' }}>
- {this.props.expandedRowKeys != undefined && this.props.expandData == undefined && <Table
- expandedRowKeys={this.props.expandedRowKeys}
- showHeader={this.props.showHeader != undefined ? this.props.showHeader : true}
- bordered={this.props.border!=undefined?this.props.border:true}
- components={this.components}
- size={'small'}
- scroll={typeof (scroll) != 'undefined' ? scroll : {}}
- rowClassName={(record) => this.setRowClassName(rowKey, record)}
- onRow={(record, index) => {
- return {
- onClick: () => {
- if (this.props.onSldHandleSeleRow) {
- this.sldHandleRow(record, index, rowKey);
- }
- },
- };
- }}
- indentSize={this.props.indentSize}
- rowKey={rowKey || 'key'}
- rowSelection={isCheck ? rowSelection : null}
- dataSource={list}
- pagination={sldpagination != undefined ? (sldpagination ? paginationProps : false) : paginationProps}
- onChange={this.handleTableChange}
- onExpand={this.onExpand}
- {...rest}
- columns={columns}
- />}
- {this.props.expandedRowKeys == undefined && this.props.expandData != undefined && <Table
- expandedRowRender={(record) => {
- let expand_data = [];
- for (let i in this.props.expandData) {
- expand_data.push({
- lval: this.props.expandData[i].key,
- rval: sldEmptyHandle1(record[this.props.expandData[i].val]),
- });
- }
- return <SldRowMoreColumnLR item_width={400} data={expand_data}/>;
- }}
- showHeader={this.props.showHeader != undefined ? this.props.showHeader : true}
- bordered={this.props.border!=undefined?this.props.border:true}
- components={this.components}
- size={'small'}
- scroll={typeof (scroll) != 'undefined' ? scroll : {}}
- rowClassName={(record) => this.setRowClassName(rowKey, record)}
- onRow={(record, index) => {
- return {
- onClick: () => {
- if (this.props.onSldHandleSeleRow) {
- this.sldHandleRow(record, index, rowKey);
- }
- },
- };
- }}
- indentSize={this.props.indentSize}
- rowKey={rowKey || 'key'}
- rowSelection={isCheck ? rowSelection : null}
- dataSource={list}
- pagination={sldpagination != undefined ? (sldpagination ? paginationProps : false) : paginationProps}
- onChange={this.handleTableChange}
- {...rest}
- columns={columns}
- />}
- {this.props.expandedRowKeys == undefined && this.props.expandData == undefined && <Table
- showHeader={this.props.showHeader != undefined ? this.props.showHeader : true}
- bordered={this.props.border!=undefined?this.props.border:true}
- components={this.components}
- size={'small'}
- scroll={typeof (scroll) != 'undefined' ? scroll : {}}
- rowClassName={(record) => this.setRowClassName(rowKey, record)}
- onRow={(record, index) => {
- return {
- onClick: () => {
- if (this.props.onSldHandleSeleRow) {
- this.sldHandleRow(record, index, rowKey);
- }
- },
- };
- }}
- indentSize={this.props.indentSize}
- rowKey={rowKey || 'key'}
- rowSelection={isCheck ? rowSelection : null}
- dataSource={list}
- pagination={sldpagination != undefined ? (sldpagination ? paginationProps : false) : paginationProps}
- onChange={this.handleTableChange}
- {...rest}
- columns={columns}
- />}
- </div>;
- return (
- <Fragment>
- {this.props.showScrollbar!=undefined&&!this.props.showScrollbar
- ?showData
- :<Scrollbars autoHeight
- autoHeightMax={this.props.totalHeight != undefined ? this.props.totalHeight : document.body.clientHeight - 200} ref={'scrollRef'}>
- {showData}
- </Scrollbars>
- }
- </Fragment>
- );
- }
- }
- export default StandardTable;
|