import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
import { is, fromJS } from 'immutable'
|
import { Table, Typography, Icon, Switch, Input, InputNumber } from 'antd'
|
|
import asyncComponent from '@/utils/asyncComponent'
|
import MKEmitter from '@/utils/events.js'
|
import zhCN from '@/locales/zh-CN/main.js'
|
import enUS from '@/locales/en-US/main.js'
|
import '@/assets/css/table.scss'
|
import './index.scss'
|
|
const { Paragraph } = Typography
|
const CardCellComponent = asyncComponent(() => import('@/tabviews/custom/components/card/cardcellList'))
|
|
class BodyRow extends React.Component {
|
shouldComponentUpdate (nextProps, nextState) {
|
return !is(fromJS(this.props.data), fromJS(nextProps.data))
|
}
|
|
render() {
|
let { lineMarks, data, ...resProps } = this.props
|
let style = {}
|
let className = ''
|
|
lineMarks && lineMarks.some(mark => {
|
let originVal = data[mark.field[0]] + ''
|
let contrastVal = ''
|
let result = false
|
|
if (mark.field[1] === 'static') {
|
contrastVal = mark.contrastValue + ''
|
} else {
|
contrastVal = data[mark.field[2]] + ''
|
}
|
|
if (mark.match === '=') {
|
result = originVal === contrastVal
|
} else if (mark.match === '!=') {
|
result = originVal !== contrastVal
|
} else if (mark.match === 'like') {
|
result = originVal.indexOf(contrastVal) > -1
|
} else if (mark.match === '>') {
|
try {
|
originVal = parseFloat(originVal)
|
contrastVal = parseFloat(contrastVal)
|
} catch (e) {
|
originVal = NaN
|
}
|
|
if (!isNaN(originVal) && !isNaN(contrastVal) && originVal > contrastVal) {
|
result = true
|
}
|
} else if (mark.match === '<') {
|
try {
|
originVal = parseFloat(originVal)
|
contrastVal = parseFloat(contrastVal)
|
} catch (e) {
|
originVal = NaN
|
}
|
|
if (!isNaN(originVal) && !isNaN(contrastVal) && originVal < contrastVal) {
|
result = true
|
}
|
}
|
|
if (result) {
|
if (mark.signType[0] === 'font') {
|
style.color = mark.color
|
} else if (mark.signType[0] === 'background') {
|
style.background = mark.color
|
if (mark.fontColor) {
|
style.color = mark.fontColor
|
}
|
className = 'background'
|
} else if (mark.signType[0] === 'underline') {
|
style.textDecoration = 'underline'
|
style.color = mark.color
|
} else if (mark.signType[0] === 'line-through') {
|
style.textDecoration = 'line-through'
|
style.color = mark.color
|
}
|
}
|
|
return result
|
})
|
|
return <tr {...resProps} className={className} style={style}/>
|
}
|
}
|
|
class BodyCell extends React.Component {
|
state = {
|
editing: false
|
}
|
|
getMark = (record, marks, style, content) => {
|
marks.some(mark => {
|
let originVal = record[mark.field[0]] + ''
|
let contrastVal = ''
|
let result = false
|
|
if (mark.field[1] === 'static') {
|
contrastVal = mark.contrastValue + ''
|
} else {
|
contrastVal = record[mark.field[2]] + ''
|
}
|
|
if (mark.match === '=') {
|
result = originVal === contrastVal
|
} else if (mark.match === '!=') {
|
result = originVal !== contrastVal
|
} else if (mark.match === 'like') {
|
result = originVal.indexOf(contrastVal) > -1
|
} else if (mark.match === '>') {
|
try {
|
originVal = parseFloat(originVal)
|
contrastVal = parseFloat(contrastVal)
|
} catch (e) {
|
originVal = NaN
|
}
|
|
if (!isNaN(originVal) && !isNaN(contrastVal) && originVal > contrastVal) {
|
result = true
|
}
|
} else if (mark.match === '<') {
|
try {
|
originVal = parseFloat(originVal)
|
contrastVal = parseFloat(contrastVal)
|
} catch (e) {
|
originVal = NaN
|
}
|
|
if (!isNaN(originVal) && !isNaN(contrastVal) && originVal < contrastVal) {
|
result = true
|
}
|
}
|
|
if (result) {
|
if (mark.signType[0] === 'font') {
|
style.color = mark.color
|
} else if (mark.signType[0] === 'background') {
|
style.background = mark.color
|
if (mark.fontColor) {
|
style.color = mark.fontColor
|
}
|
} else if (mark.signType[0] === 'underline') {
|
style.textDecoration = 'underline'
|
style.color = mark.color
|
} else if (mark.signType[0] === 'line-through') {
|
style.textDecoration = 'line-through'
|
style.color = mark.color
|
} else if (mark.signType[0] === 'icon') {
|
let icon = (<Icon style={{color: mark.color}} type={mark.signType[3]} />)
|
if (mark.signType[1] === 'front') {
|
content = <span>{icon} {content}</span>
|
} else {
|
content = <span>{content} {icon}</span>
|
}
|
}
|
}
|
return result
|
})
|
|
return content
|
}
|
|
shouldComponentUpdate (nextProps, nextState) {
|
return !is(fromJS(this.props.record), fromJS(nextProps.record)) || nextState.editing !== this.state.editing
|
}
|
|
componentDidMount () {
|
MKEmitter.addListener('tdFocus', this.tdFocus)
|
}
|
|
/**
|
* @description 组件销毁,清除state更新,清除快捷键设置
|
*/
|
componentWillUnmount () {
|
this.setState = () => {
|
return
|
}
|
MKEmitter.removeListener('tdFocus', this.tdFocus)
|
}
|
|
tdFocus = (id) => {
|
const { col, record } = this.props
|
if (id !== col.uuid + record.$Index) return
|
this.focus()
|
}
|
|
enterPress = () => {
|
const { col, record } = this.props
|
this.setState({editing: false})
|
if (col.enter === '$next') {
|
MKEmitter.emit('nextLine', col, record.$Index)
|
} else {
|
MKEmitter.emit('tdFocus', col.enter + record.$Index)
|
}
|
}
|
|
focus = () => {
|
const { col, record } = this.props
|
|
this.setState({editing: true, value: record[col.field] !== undefined ? record[col.field] : ''}, () => {
|
let node = document.getElementById(col.uuid + record.$Index)
|
node && node.select()
|
})
|
}
|
|
onBlur = () => {
|
this.setState({editing: false})
|
}
|
|
render() {
|
let { col, config, record, style, className } = this.props
|
const { editing, value } = this.state
|
|
let children = null
|
if (col.type === 'text') {
|
let content = ''
|
if (record[col.field] !== undefined) {
|
content = `${record[col.field]}`
|
}
|
|
if (content !== '') {
|
if (col.textFormat === 'YYYY-MM-DD' && /^[1-9]\d{3}(-|\/)(0[1-9]|1[0-2])(-|\/)(0[1-9]|[1-2][0-9]|3[0-1])/.test(content)) {
|
content = `${content.substr(0, 4)}-${content.substr(5, 2)}-${content.substr(8, 2)}`
|
} else if (col.textFormat === 'YYYY-MM-DD HH:mm:ss' && /^[1-9]\d{3}(-|\/)(0[1-9]|1[0-2])(-|\/)(0[1-9]|[1-2][0-9]|3[0-1]).([0-1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]/.test(content)) {
|
content = `${content.substr(0, 4)}-${content.substr(5, 2)}-${content.substr(8, 2)} ${content.substr(11, 2)}:${content.substr(14, 2)}:${content.substr(17, 2)}`
|
}
|
|
content = (col.prefix || '') + content + (col.postfix || '')
|
}
|
|
if (col.marks) {
|
style = style || {}
|
content = this.getMark(record, col.marks, style, content)
|
}
|
|
if (col.editable === 'true') {
|
if (editing) {
|
return (<td className="editing_table_cell">
|
<Input id={col.uuid + record.$Index} defaultValue={value} onChange={(e) => this.setState({value: e.target.value})} onPressEnter={this.enterPress} onBlur={this.onBlur}/>
|
</td>)
|
} else {
|
return (<td className={className + ' pointer'} style={style}><div className="mk-mask" onClick={this.focus}></div>{content}</td>)
|
}
|
} else {
|
children = content
|
}
|
} else if (col.type === 'number') {
|
let content = ''
|
try {
|
content = parseFloat(record[col.field])
|
if (isNaN(content)) {
|
content = ''
|
}
|
} catch (e) {
|
content = ''
|
}
|
|
if (content !== '') {
|
let decimal = col.decimal || 0
|
if (col.format === 'percent') {
|
content = content * 100
|
decimal = decimal > 2 ? decimal - 2 : 0
|
}
|
|
content = content.toFixed(decimal)
|
|
if (col.format === 'thdSeparator') {
|
content = content.replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,')
|
}
|
|
content = col.prefix + content + col.postfix
|
}
|
|
if (col.marks) {
|
style = style || {}
|
content = this.getMark(record, col.marks, style, content)
|
}
|
|
if (col.editable === 'true') {
|
if (editing) {
|
return (<td className="editing_table_cell">
|
<InputNumber id={col.uuid + record.$Index} defaultValue={value} onChange={(val) => this.setState({value: val})} onPressEnter={this.enterPress} onBlur={this.onBlur}/>
|
</td>)
|
} else {
|
return (<td className={className + ' pointer'} style={style}><div className="mk-mask" onClick={this.focus}></div>{content}</td>)
|
}
|
} else {
|
children = content
|
}
|
} else if (col.type === 'textarea') {
|
let content = ''
|
if (record[col.field] !== undefined) {
|
content = `${record[col.field]}`
|
}
|
|
if (content) {
|
content = col.prefix + content + col.postfix
|
}
|
|
children = (
|
<div>
|
{content ? <Paragraph copyable ellipsis={{ rows: 3, expandable: true }}>{content}</Paragraph> : null }
|
</div>
|
)
|
} else if (col.type === 'custom') {
|
style.padding = '0px'
|
if (col.style) {
|
style = {...style, ...col.style}
|
}
|
|
children = (
|
<CardCellComponent data={record} cards={config} elements={col.elements}/>
|
)
|
} else if (col.type === 'action') {
|
style.padding = '0px 5px'
|
children = (
|
<CardCellComponent data={record} cards={config} elements={col.elements}/>
|
)
|
}
|
|
return (<td className={className} style={style}>{children}</td>)
|
}
|
}
|
|
class NormalTable extends Component {
|
static propTpyes = {
|
statFValue: PropTypes.any, // 合计字段数据
|
MenuID: PropTypes.string, // 菜单Id
|
setting: PropTypes.object, // 表格全局设置:tableType(表格是否可选、单选、多选)、columnfixed(列固定)、actionfixed(按钮固定)
|
columns: PropTypes.array, // 表格列
|
lineMarks: PropTypes.any, // 行标记
|
fields: PropTypes.array, // 组件字段集
|
BData: PropTypes.any, // 主表数据
|
data: PropTypes.any, // 表格数据
|
total: PropTypes.any, // 总数
|
loading: PropTypes.bool, // 表格加载中
|
refreshdata: PropTypes.func, // 表格中排序列、页码的变化时刷新
|
}
|
|
state = {
|
dict: sessionStorage.getItem('lang') !== 'en-US' ? zhCN : enUS,
|
data: [],
|
tableId: '', // 表格ID
|
pageIndex: 1, // 初始页面索引
|
pageSize: 10, // 每页数据条数
|
columns: null, // 显示列
|
pickup: false, // 收起未选择项
|
orderfields: {} // 排序id与field转换
|
}
|
|
UNSAFE_componentWillMount () {
|
const { setting, fields, columns } = this.props
|
let orderfields = {}
|
let initEditLine = null
|
|
let _columns = columns.map(item => {
|
if (item.type === 'index') {
|
item.field = '$Index'
|
item.type = 'text'
|
}
|
if (!initEditLine && item.editable === 'true') {
|
initEditLine = item
|
}
|
|
if (item.marks && item.marks.length === 0) {
|
item.marks = ''
|
}
|
|
if (item.field) {
|
orderfields[item.uuid] = item.field
|
}
|
|
return {
|
align: item.Align,
|
dataIndex: item.uuid,
|
title: item.label,
|
sorter: item.field && item.IsSort === 'true',
|
width: item.Width || 120,
|
onCell: record => ({
|
record,
|
col: item,
|
config: item.type === 'custom' || item.type === 'action' ? {setting, columns: fields} : null,
|
})
|
}
|
})
|
|
let tableId = (() => {
|
let uuid = []
|
let _options = 'abcdefghigklmnopqrstuv'
|
for (let i = 0; i < 19; i++) {
|
uuid.push(_options.substr(Math.floor(Math.random() * 0x20), 1))
|
}
|
return uuid.join('')
|
}) ()
|
|
if (setting.borderColor) { // 边框颜色
|
let style = `#${tableId} table, #${tableId} tr, #${tableId} th, #${tableId} td {border-color: ${setting.borderColor}}`
|
let ele = document.createElement('style')
|
ele.innerHTML = style
|
document.getElementsByTagName('head')[0].appendChild(ele)
|
}
|
|
this.setState({
|
columns: _columns,
|
tableId,
|
orderfields,
|
initEditLine
|
})
|
}
|
|
shouldComponentUpdate (nextProps, nextState) {
|
return !is(fromJS(this.props), fromJS(nextProps)) || !is(fromJS(this.state), fromJS(nextState))
|
}
|
|
componentDidMount () {
|
MKEmitter.addListener('resetTable', this.resetTable)
|
MKEmitter.addListener('nextLine', this.nextLine)
|
}
|
|
/**
|
* @description 组件销毁,清除state更新
|
*/
|
componentWillUnmount () {
|
this.setState = () => {
|
return
|
}
|
MKEmitter.removeListener('resetTable', this.resetTable)
|
MKEmitter.removeListener('nextLine', this.nextLine)
|
}
|
|
UNSAFE_componentWillReceiveProps(nextProps) {
|
if (!is(fromJS(this.props.data), fromJS(nextProps.data))) {
|
this.setState({data: nextProps.data || []})
|
}
|
}
|
|
nextLine = (col, index) => {
|
const { data, initEditLine } = this.state
|
index = +index
|
|
if (index < data.length && initEditLine) {
|
MKEmitter.emit('tdFocus', initEditLine.uuid + (index + 1))
|
}
|
}
|
|
changeTable = (pagination, filters, sorter) => {
|
const { orderfields } = this.state
|
|
this.setState({
|
pageIndex: pagination.current,
|
pageSize: pagination.pageSize,
|
pickup: false
|
})
|
|
sorter.field = orderfields[sorter.field] || ''
|
|
this.props.refreshdata(pagination, filters, sorter)
|
}
|
|
resetTable = (id, repage) => {
|
const { MenuID } = this.props
|
|
if (id !== MenuID) return
|
|
if (repage === 'false') {
|
this.setState({
|
pickup: false
|
})
|
} else {
|
this.setState({
|
pageIndex: 1,
|
pickup: false
|
})
|
}
|
}
|
|
pickupChange = () => {
|
this.setState({
|
pickup: !this.state.pickup
|
})
|
}
|
|
render() {
|
const { setting, statFValue, lineMarks } = this.props
|
const { pickup, tableId, data } = this.state
|
|
const components = {
|
body: {
|
row: BodyRow,
|
cell: BodyCell
|
}
|
}
|
|
// 数据收起时,过滤已选数据
|
let _data = data || []
|
|
if (pickup) {
|
_data = _data.filter(item => !item.$deleted)
|
}
|
|
let _pagination = false
|
if (setting.laypage !== 'false' && setting.laypage !== false) {
|
_pagination = {
|
current: this.state.pageIndex,
|
pageSize: this.state.pageSize,
|
pageSizeOptions: ['10', '25', '50', '100', '500', '1000'],
|
showSizeChanger: true,
|
total: this.props.total || 0,
|
showTotal: (total, range) => `${range[0]}-${range[1]} ${this.state.dict['main.pagination.of']} ${total} ${this.state.dict['main.pagination.items']}`
|
}
|
}
|
|
let _footer = ''
|
|
if (statFValue && statFValue.length > 0) {
|
_footer = statFValue.map(f => `${f.label}(合计):${f.value}`).join(';')
|
}
|
|
let height = setting.height || false
|
|
return (
|
<div className={`edit-custom-table ${pickup ? 'editable' : ''} ${setting.tableHeader || ''} ${height ? 'fixed-height' : ''} ${setting.mode || ''}`} id={tableId}>
|
<Switch title="编辑" className="main-pickup" checkedChildren="开" unCheckedChildren="关" checked={pickup} onChange={this.pickupChange} />
|
<Table
|
components={components}
|
style={setting.style}
|
size={setting.size || 'middle'}
|
bordered={setting.bordered !== 'false'}
|
columns={this.state.columns}
|
dataSource={_data}
|
loading={this.props.loading}
|
scroll={{ x: '100%', y: height }}
|
onRow={(record, index) => {
|
return {
|
lineMarks,
|
data: record
|
}
|
}}
|
onChange={this.changeTable}
|
pagination={_pagination}
|
/>
|
{_footer ? <div className={'normal-table-footer ' + (_pagination ? 'pagination' : '')}>{_footer}</div> : null}
|
</div>
|
)
|
}
|
}
|
|
export default NormalTable
|