king
2019-10-24 a0d5241e8b7ec3d11e5e029187c48920c3834f8c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import React, {Component} from 'react'
import PropTypes from 'prop-types'
// import { is, fromJS } from 'immutable'
import { Table, Icon, message, Affix } from 'antd'
import './index.scss'
 
export default class MainTable extends Component {
  static propTpyes = {
    MenuNo: PropTypes.string, // 菜单参数
    fixed: PropTypes.object, // 表格头部是否固定于页面上方
    loading: PropTypes.bool,
    total: PropTypes.number,
    select: PropTypes.object,
    dict: PropTypes.object, // 字典项
    columns: PropTypes.array, // 表格列
    data: PropTypes.oneOfType([
      PropTypes.object,
      PropTypes.array
    ])
  }
 
  state = {
    selectedRowKeys: [],
    pageIndex: 1,
    pageSize: 10,
    columns: this.props.columns.map((item, index) => {
      let _width = parseInt(item.Width) || 50
      return {
        align: item.Align,
        dataIndex: item.FieldName,
        title: item.Label,
        // fixed: index < 3,
        sorter: item.IsSort === 'true',
        filterMultiple: item.CDefine1 === 'true',
        filters: item.CDefine2 && JSON.parse(item.CDefine2),
        width: _width * 30,
        render: (text, record) => (
          <div style={{ wordWrap: 'break-word', wordBreak: 'break-word', minWidth: _width + 'px' }}>
            {text}
            {item.FieldName === 'MenuNo' ? <Icon onClick={(e) => {this.copycontent(e, record[item.FieldName])}} type="copy"/> : ''}
          </div>
        )
        // onHeaderCell: () => ({style:{textAlign: 'center'}})
      }
    })
  }
 
  copycontent = (e, content) => {
    // 表格中内容复制
    e.stopPropagation()
    let oInput = document.createElement('input')
    oInput.value = content
    document.body.appendChild(oInput)
    oInput.select()
    document.execCommand('Copy')
    oInput.className = 'oInput'
    oInput.style.display='none'
    message.success(this.props.dict['main.copy.success'])
  }
 
  onSelectChange = selectedRowKeys => {
    this.setState({ selectedRowKeys })
  }
 
  changeRow = (record, index) => {
    // 点击整行,触发切换,判断是否可选,单选或多选,进行对应操作
    if (!this.props.select || !this.props.select.selectable) return
 
    let newkeys = JSON.parse(JSON.stringify(this.state.selectedRowKeys))
    let _re = newkeys.includes(index)
 
    if (this.props.select.selectType === 'radio') {
      this.setState({ selectedRowKeys: [index] })
    } else {
      if (_re) {
        newkeys = newkeys.filter(item => item !== index)
      } else {
        newkeys.push(index)
      }
      this.setState({ selectedRowKeys: newkeys })
    }
  }
 
  changeTable = (pagination, filters, sorter) => {
    this.setState({
      pageIndex: pagination.current,
      pageSize: pagination.pageSize,
      selectedRowKeys: []
    })
    this.props.refreshdata(pagination, filters, sorter)
  }
 
  resetTable = () => {
    this.setState({
      pageIndex: 1,
      selectedRowKeys: []
    })
  }
 
  render() {
    let { selectedRowKeys } = this.state
    let rowSelection = null
    if (this.props.select && this.props.select.selectable) {
      rowSelection = {
        selectedRowKeys,
        type: this.props.select.selectType === 'radio' ? 'radio' : 'checkbox',
        onChange: this.onSelectChange
      }
    }
    let offset = null
    if (this.props.fixed.fixtable) {
      // 表格头部固定于顶部时,判断距顶部高度
      if (!this.props.fixed.fixaction) {
        offset = 48
      } else {
        let box = document.getElementById(this.props.MenuNo + 'mainaction')
        if (box) {
          offset = 48 + box.offsetHeight
        } else {
          offset = 105
        }
      }
    }
    return (
      <div className="main-table">
        {this.props.fixed.fixtable && <Affix offsetTop={offset} className="fix-header">
          <Table
            bordered={true}
            rowSelection={rowSelection}
            size="middle"
            columns={this.state.columns.map(column => {
              return {
                align: column.align,
                dataIndex: column.dataIndex,
                title: column.title,
                width: column.width
              }
            })}
          />
        </Affix>}
        <Table
          bordered={true}
          rowSelection={rowSelection}
          size="middle"
          columns={this.state.columns}
          dataSource={this.props.data ? this.props.data : []}
          loading={this.props.loading}
          scroll={{ x: '100%', y: false }}
          onRow={(record, index) => {
            return {
              onClick: () => {this.changeRow(record, index)}
            }
          }}
          onChange={this.changeTable}
          pagination={{
            current: this.state.pageIndex,
            pageSize: this.state.pageSize,
            pageSizeOptions: ['10', '25', '50', '100', '500', '1000'],
            showSizeChanger: true,
            total: this.props.total,
            showTotal: (total, range) => `${range[0]}-${range[1]} ${this.props.dict['main.pagination.of']} ${total} ${this.props.dict['main.pagination.items']}`
          }}
        />
      </div>
    )
  }
}