king
2022-05-18 0011ec870d3d1fe9d77a4941358c84acf8632e5e
src/tabviews/custom/components/share/normalTable/index.jsx
@@ -3,9 +3,11 @@
import md5 from 'md5'
import { connect } from 'react-redux'
import { is, fromJS } from 'immutable'
import { Table, Typography, Icon, Col, Switch, message } from 'antd'
import { Table, Typography, Col, Switch, message } from 'antd'
import asyncComponent from '@/utils/asyncComponent'
import { getMark } from '@/utils/utils.js'
import MkIcon from '@/components/mk-icon'
import MKEmitter from '@/utils/events.js'
import zhCN from '@/locales/zh-CN/main.js'
import enUS from '@/locales/en-US/main.js'
@@ -27,6 +29,10 @@
  render() {
    let { lineMarks, onDoubleClick, data, className, ...resProps } = this.props
    let style = {}
    if (data && data.$disabled) {
      className = className + ' mk-disabled '
    }
    if (lineMarks && lineMarks.length > 0) {
      lineMarks.some(mark => {
@@ -78,7 +84,7 @@
            if (mark.fontColor) {
              style.color = mark.fontColor
            }
            className += className + ' background'
            className = className + ' background'
          } else if (mark.signType[0] === 'underline') {
            style.textDecoration = 'underline'
            style.color = mark.color
@@ -97,77 +103,6 @@
}
class BodyCell extends React.Component {
  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 !nextProps.record || !is(fromJS(this.props.record), fromJS(nextProps.record))
  }
@@ -194,8 +129,17 @@
      }
      if (col.marks) {
        style = style || {}
        content = this.getMark(record, col.marks, style, content)
        let mark = getMark(col.marks, record, style)
        style = mark.style
        if (mark.icon) {
          if (mark.position === 'front') {
            content = <span><MkIcon style={{color: mark.color}} type={mark.icon} /> {content}</span>
          } else {
            content = <span>{content} <MkIcon style={{color: mark.color}} type={mark.icon} /></span>
          }
        }
      }
      if (col.blur) {
        content = md5(content)
@@ -205,7 +149,7 @@
        resProps.rowSpan = record['$$' + col.field]
      }
      
      if (col.linkThdMenu || col.linkurl) {
      if (!record.$disabled && (col.linkThdMenu || col.linkurl)) {
        content = (
          <div>
            <div className="link-menu" onDoubleClick={(e) => triggerLink(e, col, record)}></div>
@@ -231,6 +175,8 @@
        if (col.format === 'percent') {
          content = content * 100
          decimal = decimal > 2 ? decimal - 2 : 0
        } else if (col.format === 'abs') {
          content = Math.abs(content)
        }
  
        content = content.toFixed(decimal)
@@ -239,12 +185,21 @@
          content = content.replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,')
        }
  
        content = col.prefix + content + col.postfix
        content = (col.prefix || '') + content + (col.postfix || '')
      }
      if (col.marks) {
        style = style || {}
        content = this.getMark(record, col.marks, style, content)
        let mark = getMark(col.marks, record, style)
        style = mark.style
        if (mark.icon) {
          if (mark.position === 'front') {
            content = <span><MkIcon style={{color: mark.color}} type={mark.icon} /> {content}</span>
          } else {
            content = <span>{content} <MkIcon style={{color: mark.color}} type={mark.icon} /></span>
          }
        }
      }
      if (col.blur) {
@@ -255,7 +210,7 @@
        resProps.rowSpan = record['$$' + col.field]
      }
      if (col.linkThdMenu || col.linkurl) {
      if (!record.$disabled && (col.linkThdMenu || col.linkurl)) {
        content = (
          <div>
            <div className="link-menu" onDoubleClick={(e) => triggerLink(e, col, record)}></div>
@@ -269,7 +224,12 @@
      let photos = ''
      if (record[col.field]) {
        photos = `${record[col.field]}`
        photos = photos.split(',')
      }
      if (/^data:image/.test(photos)) {
        photos = [photos]
      } else {
        photos = photos.split(',').filter(Boolean)
      }
      let cols = 24 / (col.picSort || 1)
@@ -299,7 +259,7 @@
      }
      if (content) {
        content = col.prefix + content + col.postfix
        content = (col.prefix || '') + content + (col.postfix || '')
      }
      if (col.blur) {
@@ -346,6 +306,10 @@
        content = md5(content)
      }
      if (_href && /^https/.test(window.location.protocol)) { // https转换
        _href = _href.replace(/^http:/ig, 'https:')
      }
      resProps.children = (
        <div>
          {content && _href ? <a href={_href} target="_blank" rel="noopener noreferrer">{content}</a> : null }
@@ -379,8 +343,17 @@
      }
      if (col.marks) {
        style = style || {}
        content = this.getMark(record, col.marks, style, content)
        let mark = getMark(col.marks, record, style)
        style = mark.style
        if (mark.icon) {
          if (mark.position === 'front') {
            content = <span><MkIcon style={{color: mark.color}} type={mark.icon} /> {content}</span>
          } else {
            content = <span>{content} <MkIcon style={{color: mark.color}} type={mark.icon} /></span>
          }
        }
      }
      resProps.children = content
@@ -414,7 +387,6 @@
    lineMarks: PropTypes.array,      // 行标记
    fields: PropTypes.array,         // 组件字段集
    ContainerId: PropTypes.any,      // 标签页外层Id
    BData: PropTypes.any,            // 主表数据
    data: PropTypes.any,             // 表格数据
    total: PropTypes.any,            // 总数
    loading: PropTypes.bool,         // 表格加载中
@@ -432,7 +404,8 @@
    activeIndex: null,    // 标记当前选中行
    rowspans: null,       // 行合并字段信息
    pickup: false,        // 收起未选择项
    orderfields: {}       // 排序id与field转换
    orderfields: {},      // 排序id与field转换
    pageOptions: []
  }
  UNSAFE_componentWillMount () {
@@ -512,18 +485,34 @@
      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)
    // 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)
    // }
    let size = (setting.pageSize || 10) + ''
    let pageOptions = ['10', '25', '50', '100', '500', '1000']
    if (!pageOptions.includes(size)) {
      pageOptions.push(size)
      pageOptions = pageOptions.sort((a, b) => a - b)
    }
    this.setState({
      pageSize: setting.pageSize || 10,
      pageOptions,
      columns: _columns,
      rowspans,
      tableId,
      orderfields
    }, () => {
      const element = document.getElementById(tableId)
      element && element.style.setProperty('--mk-table-border-color', setting.borderColor || '#e8e8e8')
      element && element.style.setProperty('--mk-table-color', setting.color || 'rgba(0, 0, 0, 0.65)')
      element && element.style.setProperty('--mk-table-font-size', setting.fontSize || '14px')
      element && element.style.setProperty('--mk-table-font-weight', setting.fontWeight || 'normal')
    })
  }
@@ -549,17 +538,27 @@
    MKEmitter.removeListener('resetTable', this.resetTable)
  }
  mkCheckTopLine = (menuId) => {
  mkCheckTopLine = (menuId, id) => {
    const { MenuID, data, setting } = this.props
    if (MenuID !== menuId || !data || data.length === 0) return
    this.changedata(0)
    this.setState({ selectedRowKeys: [0], activeIndex: 0 })
    this.props.chgSelectData([data[0]])
    let index = 0
    if (id) {
      index = data.findIndex(item => item.$$uuid === id)
      if (index === -1) {
        index = 0
      }
    }
    if (data[index].$disabled) return
    this.changedata(index)
    this.setState({ selectedRowKeys: [index], activeIndex: index })
    this.props.chgSelectData([data[index]])
    if (setting.$hasSyncModule) {
      MKEmitter.emit('syncBalconyData', MenuID, [data[0]], data.length === 1)
      MKEmitter.emit('syncBalconyData', MenuID, [data[index]], data.length === 1)
    }
  }
@@ -676,7 +675,7 @@
    this.setState({ selectedRowKeys, activeIndex: _activeIndex })
    let selects = this.props.data.filter((item, _index) => selectedRowKeys.includes(_index))
    let selects = this.props.data.filter((item, _index) => selectedRowKeys.includes(_index) && !item.$disabled)
    this.props.chgSelectData(selects)
    if (setting.$hasSyncModule) {
@@ -691,6 +690,7 @@
    const { setting, MenuID, data } = this.props
    if (!setting.tableType || this.state.pickup) return
    if (record.$disabled) return
    
    let newkeys = fromJS(this.state.selectedRowKeys).toJS()
@@ -715,7 +715,7 @@
      this.setState({ selectedRowKeys: newkeys, activeIndex: _index !== '' ? _index : null })
    }
    let selects = this.props.data.filter((item, _index) => newkeys.includes(_index))
    let selects = this.props.data.filter((item, _index) => newkeys.includes(_index) && !item.$disabled)
    
    this.props.chgSelectData(selects)
@@ -741,15 +741,17 @@
  }
  changedata = (index) => {
    const { MenuID, data, setting } = this.props
    const { MenuID, data } = this.props
    let _id = ''
    let _data = ''
    if (data && data.length > 0 && index !== '') {
      _id = data[index][setting.primaryKey] || ''
      _id = data[index].$$uuid || ''
      _data = data[index] || ''
    }
    if (_data && _data.$disabled) return
    MKEmitter.emit('resetSelectLine', MenuID, _id, _data)
  }
@@ -839,13 +841,14 @@
    const { setting } = this.props
    if (!setting.doubleClick) return
    if (record.$disabled) return
    MKEmitter.emit('triggerBtnId', setting.doubleClick, [record])
  }
  render() {
    const { setting, statFValue, lineMarks, data } = this.props
    const { selectedRowKeys, activeIndex, pickup, tableId } = this.state
    const { selectedRowKeys, activeIndex, pickup, tableId, pageOptions } = this.state
    // 设置表格选择属性:单选、多选、不可选
    let rowSelection = null
@@ -878,7 +881,7 @@
      _pagination = {
        current: this.state.pageIndex,
        pageSize: this.state.pageSize,
        pageSizeOptions: ['10', '25', '50', '100', '500', '1000'],
        pageSizeOptions: pageOptions,
        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']}`
@@ -900,7 +903,7 @@
        }
        <Table
          components={components}
          style={setting.style}
          // style={setting.style}
          size={setting.size || 'middle'}
          bordered={setting.bordered !== 'false'}
          rowSelection={rowSelection}