king
2023-10-19 072227ec43346c50c93cda3bf21c346f463eb551
src/tabviews/custom/components/share/normalTable/index.jsx
@@ -6,15 +6,14 @@
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'
import Encrypts from '@/components/encrypts'
import '@/assets/css/table.scss'
import './index.scss'
const { Paragraph } = Typography
const Video = asyncComponent(() => import('@/components/video'))
const MkIcon = asyncComponent(() => import('@/components/mk-icon'))
const MkPicture = asyncComponent(() => import('@/components/mkPicture'))
const CardCellComponent = asyncComponent(() => import('@/tabviews/custom/components/card/cardcellList'))
const PicRadio = {
  '4:3': '75%', '3:2': '66.67%', '16:9': '56.25%', '2:1': '50%', '3:1': '33.33%', '4:1': '25%',
@@ -23,7 +22,7 @@
}
class BodyRow extends React.Component {
  shouldComponentUpdate (nextProps, nextState) {
    return !is(fromJS(this.props.data), fromJS(nextProps.data)) || !is(fromJS(this.props.className), fromJS(nextProps.className))
    return !is(fromJS(this.props.data), fromJS(nextProps.data)) || this.props.className !== nextProps.className
  }
  render() {
@@ -145,6 +144,14 @@
          }
        } else if (mark.innerStyle) {
          content = <span style={mark.innerStyle}>{content}</span>
        } else if (mark.space) {
          content = <><span dangerouslySetInnerHTML={{__html: mark.space}}></span>{content}</>
        } else if (mark.point) {
          if (mark.position === 'front') {
            content = <>{mark.point}{content}</>
          } else {
            content = <>{content}{mark.point}</>
          }
        }
      }
      if (col.blur) {
@@ -177,15 +184,17 @@
      }
      if (content !== '') {
        let decimal = col.decimal || 0
        if (col.round) {
          content = Math.round(content * col.round) / col.round
        }
        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)
        if (col.round) {
          content = content.toFixed(col.decimal)
        }
  
        if (col.format === 'thdSeparator') {
          content = content.replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,')
@@ -207,6 +216,14 @@
          }
        } else if (mark.innerStyle) {
          content = <span style={mark.innerStyle}>{content}</span>
        } else if (mark.space) {
          content = <><span dangerouslySetInnerHTML={{__html: mark.space}}></span>{content}</>
        } else if (mark.point) {
          if (mark.position === 'front') {
            content = <>{mark.point}{content}</>
          } else {
            content = <>{content}{mark.point}</>
          }
        }
      }
@@ -240,7 +257,6 @@
        photos = photos.split(',').filter(Boolean)
      }
      let cols = 24 / (col.picSort || 1)
      let paddingTop = '100%'
      let scale = col.scale === 'true'
@@ -250,14 +266,19 @@
      resProps.children = (
        <div>
          {photos && photos.map((url, i) => (
            <Col key={i} span={cols}>
              <div className={'ant-mk-picture' + (scale ? ' scale' : '')} onClick={() => {
                if (!scale) return
                MKEmitter.emit('mkImageScale', url, photos)
              }} style={{paddingTop, backgroundImage: `url('${url}')`}}></div>
          {photos.map((url, i) => (
            <Col key={i} span={col.span || 24}>
              <MkPicture style={{paddingTop, backgroundSize: col.backgroundSize || 'cover'}} scale={scale} url={url} urls={photos}/>
            </Col>
          ))}
        </div>
      )
    } else if (col.type === 'video') {
      let url = record[col.field] || ''
      resProps.children = (
        <div className="video-wrap">
          {url ? <Video card={col} value={url}/> : null}
        </div>
      )
    } else if (col.type === 'textarea') {
@@ -325,29 +346,46 @@
      )
    } else if (col.type === 'formula') {
      let content = col.formula
      Object.keys(record).forEach(key => {
        let reg = new RegExp('@' + key + '@', 'ig')
        content = content.replace(reg, record[key])
      })
      if (col.eval !== 'false') {
      if (col.eval === 'func') {
        try {
          // eslint-disable-next-line
          content = eval(content)
          let func = new Function('data', col.formula)
          content = func([record])
        } catch (e) {
          console.warn(e)
          content = ''
        }
      } else {
        Object.keys(record).forEach(key => {
          let reg = new RegExp('@' + key + '@', 'ig')
          content = content.replace(reg, record[key])
        })
        if (col.eval !== 'false') {
          try {
            // eslint-disable-next-line
            content = eval(content)
          } catch (e) {
            console.info(content)
            console.warn(e)
            content = ''
          }
          if (col.round && typeof(content) === 'number') {
            content = Math.round(content * col.round) / col.round
            content = content.toFixed(col.decimal)
          }
        }
      }
      content = content === undefined ? '' : content
      if (content !== '') {
      if (col.eval === 'func') {
        content = <span dangerouslySetInnerHTML={{__html: content}}></span>
      } else if (content !== '') {
        content = `${col.prefix || ''}${content}${col.postfix || ''}`
        if (col.eval === 'false') {
          content = content.replace(/\n/ig, '<br/>').replace(/\s/ig, '&nbsp;')
          content = <span dangerouslySetInnerHTML={{__html: content}}></span>
        }
        content = content.replace(/\n/ig, '<br/>').replace(/\s/ig, '&nbsp;')
        content = <span dangerouslySetInnerHTML={{__html: content}}></span>
      }
      if (col.marks) {
@@ -363,6 +401,14 @@
          }
        } else if (mark.innerStyle) {
          content = <span style={mark.innerStyle}>{content}</span>
        } else if (mark.space) {
          content = <><span dangerouslySetInnerHTML={{__html: mark.space}}></span>{content}</>
        } else if (mark.point) {
          if (mark.position === 'front') {
            content = <>{mark.point}{content}</>
          } else {
            content = <>{content}{mark.point}</>
          }
        }
      }
@@ -373,11 +419,6 @@
        style = {...style, ...col.style}
      }
      resProps.children = (
        <CardCellComponent data={record} cards={config} elements={col.elements}/>
      )
    } else if (col.type === 'action') {
      style.padding = '0px 5px'
      resProps.children = (
        <CardCellComponent data={record} cards={config} elements={col.elements}/>
      )
@@ -396,16 +437,15 @@
    columns: PropTypes.array,        // 表格列
    lineMarks: PropTypes.array,      // 行标记
    fields: PropTypes.array,         // 组件字段集
    ContainerId: PropTypes.any,      // 标签页外层Id
    data: PropTypes.any,             // 表格数据
    total: PropTypes.any,            // 总数
    loading: PropTypes.bool,         // 表格加载中
    refreshdata: PropTypes.func,     // 表格中排序列、页码的变化时刷新
    chgSelectData: PropTypes.func,   // 数据切换
    autoMatic: PropTypes.any
  }
  state = {
    dict: sessionStorage.getItem('lang') !== 'en-US' ? zhCN : enUS,
    tableId: '',          // 表格ID
    selectedRowKeys: [],  // 表格中选中行
    pageIndex: 1,         // 初始页面索引
@@ -425,7 +465,7 @@
    let rowspans = []
    let orderfields = {}
    if (window.GLOB.dataFormat && !window.GLOB.mkHS) {
    if (window.GLOB.dataFormat) {
      _format = true
      if (window.GLOB.memberLevel >= 30) {
@@ -435,52 +475,73 @@
      }
    }
    let getColumns = (cols) => {
      return cols.map(item => {
        let cell = null
        if (item.type === 'colspan') {
          cell = { title: item.label, align: item.Align }
          cell.children = getColumns(item.subcols)
        } else {
          if (item.rowspan === 'true') {
            rowspans.push(item.field)
          }
          if (item.type === 'index') {
            item.field = '$Index'
            item.type = 'text'
          } else if (_format && !Math.floor(Math.random() * radio)) {
            item.blur = true
          }
          if (item.marks && item.marks.length === 0) {
            item.marks = ''
          }
    let _columns = []
          if (item.field) {
            orderfields[item.uuid] = item.field
          }
          cell = {
            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,
              triggerLink: this.triggerLink
            })
          }
        }
    if (setting.tableMode !== 'fast') {
      let getColumns = (cols) => {
        return cols.map(item => {
          let cell = null
          if (item.type === 'colspan') {
            cell = { title: item.label, align: item.Align }
            cell.children = getColumns(item.subcols)
          } else {
            if (item.rowspan === 'true') {
              rowspans.push(item.field)
            }
            if (item.type === 'index') {
              item.field = '$Index'
              item.type = 'text'
            } else if (_format && !Math.floor(Math.random() * radio)) {
              item.blur = true
            }
            if (item.marks && item.marks.length === 0) {
              item.marks = ''
            }
  
        return cell
            if (item.field) {
              orderfields[item.uuid] = item.field
            } else if (item.sortField) {
              orderfields[item.uuid] = item.sortField
            }
            cell = {
              align: item.Align,
              dataIndex: item.uuid,
              title: item.label,
              sorter: (item.field || item.sortField) && item.IsSort === 'true',
              width: item.Width || 120,
              onCell: record => ({
                record,
                col: item,
                config: item.type === 'custom' ? {setting, columns: fields} : null,
                triggerLink: this.triggerLink
              })
            }
          }
          return cell
        })
      }
      _columns = getColumns(columns)
    } else {
      let fields = []
      columns.forEach(item => {
        if (!item.field || fields.includes(item.field)) return
        fields.push(item.field)
        _columns.push({
          align: item.Align,
          dataIndex: item.field,
          title: item.label,
          sorter: item.IsSort === 'true',
          width: item.Width || 120
        })
      })
    }
    let _columns = getColumns(columns)
      orderfields = null
    }
    if (rowspans.length === 0) {
      rowspans = null
@@ -494,13 +555,6 @@
      }
      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)
    // }
    let size = (setting.pageSize || 10) + ''
    let pageOptions = ['10', '25', '50', '100', '500', '1000']
@@ -531,9 +585,17 @@
  }
  componentDidMount () {
    MKEmitter.addListener('mkCheckTopLine', this.mkCheckTopLine)
    const { autoMatic } = this.props
    MKEmitter.addListener('mkCheckAll', this.mkCheckAll)
    MKEmitter.addListener('resetTable', this.resetTable)
    if (autoMatic === true) {
      MKEmitter.addListener('autoQueryData', this.autoQueryData)
      MKEmitter.addListener('autoSelectData', this.autoSelectData)
    }
    MKEmitter.addListener('mkCheckTopLine', this.mkCheckTopLine)
  }
  /**
@@ -543,15 +605,78 @@
    this.setState = () => {
      return
    }
    MKEmitter.removeListener('mkCheckTopLine', this.mkCheckTopLine)
    MKEmitter.removeListener('mkCheckAll', this.mkCheckAll)
    MKEmitter.removeListener('resetTable', this.resetTable)
    MKEmitter.removeListener('autoQueryData', this.autoQueryData)
    MKEmitter.removeListener('autoSelectData', this.autoSelectData)
    MKEmitter.removeListener('mkCheckTopLine', this.mkCheckTopLine)
  }
  mkCheckTopLine = (menuId, id) => {
  autoSelectData = (id, index) => {
    if (id !== this.props.MenuID) return
    const { pageSize, pageIndex } = this.state
    let i = index - (pageIndex - 1) * pageSize - 1
    if (this.props.data[i]) {
      this.changeRow(this.props.data[i], i)
      MKEmitter.emit('autoTransSelectData', this.props.MenuID, this.props.data[i])
    } else {
      MKEmitter.emit('autoMaticOver', this.props.MenuID)
    }
  }
  autoQueryData = (id, index) => {
    if (id !== this.props.MenuID) return
    const { total } = this.props
    const { pageSize } = this.state
    if (index !== 1 && (!total || index > total)) {
      MKEmitter.emit('autoMaticOver', this.props.MenuID)
      return
    }
    console.clear()
    let pageIndex = Math.ceil(index / pageSize)
    this.setState({
      pageIndex: pageIndex,
      selectedRowKeys: [],
      activeIndex: null
    })
    this.props.refreshdata({pageIndex})
  }
  mkCheckTopLine = (menuId, id, type) => {
    const { MenuID, data, setting } = this.props
    if (MenuID !== menuId || !data || data.length === 0) return
    if (type === 'sign') {
      let index = ''
      let keys = []
      let items = []
      data.forEach((item, i) => {
        if (!item.$disabled && item.selected === 'true') {
          items.push(item)
          keys.push(i)
          index = i
        }
      })
      this.changedata(index)
      this.setState({ selectedRowKeys: keys, activeIndex: index })
      this.props.chgSelectData(items)
      if (setting.$hasSyncModule) {
        MKEmitter.emit('syncBalconyData', MenuID, items, data.length === keys.length)
      }
      return
    }
    let index = 0
    if (id) {
@@ -624,11 +749,7 @@
      tabmenu.param = __param
      if (['linkage_navigation', 'linkage', 'menu_board'].includes(window.GLOB.navBar)) {
        MKEmitter.emit('modifyTabs', tabmenu, 'replace')
      } else {
        MKEmitter.emit('modifyTabs', tabmenu, 'plus', true)
      }
      MKEmitter.emit('modifyTabs', tabmenu, true)
    } else if (item.linkurl) {
      let src = item.linkurl
@@ -651,10 +772,7 @@
    }
  }
  /**
   *
   */
  onSelectChange = selectedRowKeys => {
  onSelectChange = (selectedRowKeys, e) => {
    const { setting, MenuID, data } = this.props
    let index = ''
@@ -713,7 +831,7 @@
    }
    let selects = this.props.data.filter((item, _index) => newkeys.includes(_index) && !item.$disabled)
    this.props.chgSelectData(selects)
    if (setting.$hasSyncModule) {
@@ -732,7 +850,9 @@
      pickup: false
    })
    sorter.field = orderfields[sorter.field] || ''
    if (orderfields) {
      sorter.field = orderfields[sorter.field] || ''
    }
    this.props.refreshdata(pagination, filters, sorter)
  }
@@ -753,13 +873,52 @@
    MKEmitter.emit('resetSelectLine', MenuID, _id, _data)
  }
  resetTable = (id, repage) => {
    const { MenuID } = this.props
  resetTable = (id, type, Index) => {
    const { MenuID, setting, data } = this.props
    if (id !== MenuID) return
    if (repage === 'false') {
    if (type === 'delete') {
      let selectKeys = fromJS(this.state.selectedRowKeys).toJS()
      let activeKey = this.state.activeIndex
      let trans = activeKey === Index
      let _item = null
      let selects = []
      if (setting.tableType) {
        selectKeys = selectKeys.filter(key => key !== Index)
        selectKeys = selectKeys.map(key => key > Index ? key - 1 : key)
        selects = selectKeys.map(key => data[key]).filter(Boolean)
        activeKey = selectKeys.length ? selectKeys[selectKeys.length - 1] : null
        if (trans && selects.length) {
          _item = selects[selects.length - 1]
        }
      }
      this.setState({ selectedRowKeys: selectKeys, activeIndex: activeKey })
      this.props.chgSelectData(selects)
      if (trans) {
        MKEmitter.emit('resetSelectLine', MenuID, (_item ? _item.$$uuid : ''), _item)
      }
      if (setting.$hasSyncModule) {
        MKEmitter.emit('syncBalconyData', MenuID, selects, data.length === selects.length)
      }
    } else if (type === 'false') {
      this.setState({
        selectedRowKeys: [],
        activeIndex: null,
        pickup: false
      })
    } else if (type === 'repage') {
      this.setState({
        pageIndex: Index,
        selectedRowKeys: [],
        activeIndex: null,
        pickup: false
@@ -845,7 +1004,7 @@
  render() {
    const { setting, statFValue, lineMarks, data } = this.props
    const { selectedRowKeys, activeIndex, pickup, tableId, pageOptions } = this.state
    const { selectedRowKeys, activeIndex, pickup, tableId, pageOptions, columns } = this.state
    // 设置表格选择属性:单选、多选、不可选
    let rowSelection = null
@@ -857,11 +1016,14 @@
      }
    }
    const components = {
    let components = {
      body: {
        row: BodyRow,
        cell: BodyCell
        row: BodyRow
      }
    }
    if (setting.tableMode !== 'fast') {
      components.body.cell = BodyCell
    }
    // 数据收起时,过滤已选数据
@@ -881,7 +1043,7 @@
        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']}`
        showTotal: (total, range) => `${range[0]}-${range[1]} 共 ${total} 条`
      }
    }
@@ -892,26 +1054,43 @@
    }
    let height = setting.height || false
    if (height && height <= 100) {
      height = height + 'vh'
    }
    let loading = this.props.loading
    if (setting.mask === 'hidden') {
      loading = false
    }
    let fixed = ''
    if (setting.colfixed && setting.colfixed.length) {
      if (setting.colfixed.includes('first')) {
        fixed = 'mk-fixed-first-col'
      }
      if (setting.colfixed.includes('last')) {
        fixed += ' mk-fixed-last-col'
      }
    }
    return (
      <div className={`normal-custom-table ${setting.tableHeader || ''} ${height ? 'fixed-height' : ''} ${setting.mode || ''}`} id={tableId}>
      <div className={`normal-custom-table ${setting.tableHeader || ''} ${height ? 'fixed-height' : ''} ${setting.mode || ''} table-vertical-${setting.vertical || 'middle'} table-col-${columns.length} ${fixed}`} id={tableId}>
        {(setting.tableType === 'radio' || setting.tableType === 'checkbox') && data && data.length > 0 ?
          <Switch title="收起" className="main-pickup" checkedChildren="开" unCheckedChildren="关" checked={pickup} onChange={this.pickupChange} /> : null
        }
        <Table
          components={components}
          // style={setting.style}
          size={setting.size || 'middle'}
          bordered={setting.bordered !== 'false'}
          rowSelection={rowSelection}
          columns={this.state.columns}
          columns={columns}
          dataSource={_data}
          loading={this.props.loading}
          loading={loading}
          scroll={{ x: '100%', y: height }}
          onRow={(record, index) => {
            return {
              lineMarks,
              lineMarks: setting.tableMode !== 'fast' ? lineMarks : null,
              data: record,
              title: setting.tipField ? record[setting.tipField] : '',
              className: index === activeIndex ? ' mk-row-active ' : '',
              onClick: () => {this.changeRow(record, index)},
              onDoubleClick: () => {this.doubleClickLine(record)}