index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. import React, { PureComponent, Fragment, Component } from 'react';
  2. import { Table, Alert, Input, Select } from 'antd';
  3. import { sldEmptyHandle1,sldComLanguage } from '@/utils/utils';
  4. import styles from './index.less';
  5. import global from '@/global.less';
  6. import { Scrollbars } from 'react-custom-scrollbars';
  7. import { Resizable } from 'react-resizable';
  8. import SldRowMoreColumnLR from '@/components/SldRowMoreColumnLR';
  9. const InputGroup = Input.Group;
  10. const Option = Select.Option;
  11. function initTotalList(columns) {
  12. const totalList = [];
  13. columns.forEach(column => {
  14. if (column.needTotal) {
  15. totalList.push({ ...column, total: 0 });
  16. }
  17. });
  18. return totalList;
  19. }
  20. const ResizeableTitle = (props) => {
  21. const { onResize, width, ...restProps } = props;
  22. if (!width) {
  23. return <th {...restProps} />;
  24. }
  25. return (
  26. <Resizable width={width ? width : 100} height={0} onResize={onResize}>
  27. <th {...restProps} />
  28. </Resizable>
  29. );
  30. };
  31. class StandardTable extends Component {
  32. constructor(props) {
  33. super(props);
  34. const { columns } = props;
  35. const needTotalList = initTotalList(columns);
  36. this.state = {
  37. selectedRowKeys: [],
  38. needTotalList,
  39. rowId: '',// 选中行的id
  40. preScrollBarToTop: 0,// Scrollbars滚动条是否返回顶部的标识,默认为0,不返回,逐渐加1——上一次
  41. scrollBarToTop: 0,// Scrollbars滚动条是否返回顶部的标识,默认为0,不返回,逐渐加1——当前
  42. };
  43. }
  44. static getDerivedStateFromProps(nextProps,prevState) {
  45. if(nextProps.scrollBarToTop != undefined && nextProps.scrollBarToTop > 0 && (nextProps.scrollBarToTop - prevState.preScrollBarToTop > 0)){
  46. return {scrollBarToTop: nextProps.scrollBarToTop, preScrollBarToTop: nextProps.scrollBarToTop - 1 };
  47. }
  48. // clean state
  49. if (nextProps.selectedRows != undefined && nextProps.selectedRows.length === 0) {
  50. const needTotalList = initTotalList(nextProps.columns);
  51. return {
  52. selectedRowKeys: [],
  53. needTotalList,
  54. };
  55. } else {
  56. if (nextProps.flag_show_sele_data != undefined && nextProps.flag_show_sele_data) {
  57. return {
  58. selectedRowKeys: nextProps.selectedRowKeys,
  59. };
  60. }
  61. }
  62. return null;
  63. }
  64. componentDidUpdate(prevProps, prevState, snapshot) {
  65. const {scrollBarToTop,preScrollBarToTop} = this.state;
  66. if(scrollBarToTop > 0 && scrollBarToTop - preScrollBarToTop > 0){
  67. if(this.refs!=undefined&&this.refs.scrollRef!=undefined&&this.refs.scrollRef.getScrollTop()>0){
  68. this.refs.scrollRef.scrollToTop();
  69. this.setState({preScrollBarToTop:scrollBarToTop})
  70. }
  71. }
  72. }
  73. handleRowSelectChange = (selectedRowKeys, selectedRows) => {
  74. let { needTotalList } = this.state;
  75. needTotalList = needTotalList.map(item => ({
  76. ...item,
  77. total: selectedRows.reduce((sum, val) => sum + parseFloat(val[item.dataIndex], 10), 0),
  78. }));
  79. const { onSelectRow } = this.props;
  80. if (onSelectRow) {
  81. onSelectRow(selectedRows, selectedRowKeys);
  82. }
  83. this.setState({ selectedRowKeys, needTotalList });
  84. };
  85. handleTableChange = (pagination, filters, sorter) => {
  86. const { onChange } = this.props;
  87. if (onChange) {
  88. onChange(pagination, filters, sorter);
  89. }
  90. };
  91. cleanSelectedKeys = () => {
  92. this.handleRowSelectChange([], []);
  93. };
  94. //表格单行点击事件
  95. sldHandleRow = (record, index, rowKey) => {
  96. this.props.onSldHandleSeleRow(record);
  97. let cur_id = '';
  98. if (rowKey == 'id') {
  99. cur_id = record.id;
  100. } else if (rowKey == 'upTime') {
  101. cur_id = record.upTime;
  102. } else if (rowKey == 'specId') {
  103. cur_id = record.specId;
  104. } else if (rowKey == 'brandId') {
  105. cur_id = record.brandId;
  106. } else if (rowKey == 'goodsId') {
  107. cur_id = record.goodsId;
  108. } else if (rowKey == 'categoryId') {
  109. cur_id = record.categoryId;
  110. } else if (rowKey == 'decoId') {
  111. cur_id = record.decoId;
  112. } else if (rowKey == 'seckillId') {
  113. cur_id = record.seckillId;
  114. } else if (rowKey == 'labelId') {
  115. cur_id = record.labelId;
  116. } else if (rowKey == 'integralGoodsId') {
  117. cur_id = record.integralGoodsId;
  118. } else if (rowKey == 'couponId') {
  119. cur_id = record.couponId;
  120. } else if (rowKey == 'attributeId') {
  121. cur_id = record.attributeId;
  122. } else if (rowKey == 'drawId') {
  123. cur_id = record.drawId;
  124. }
  125. this.setState({
  126. rowId: cur_id,
  127. });
  128. };
  129. //设置选中行的背景色
  130. setRowClassName = (rowKey, record) => {
  131. if (this.props.onSldHandleSeleRow) {
  132. let cur_id = '';
  133. if (rowKey == 'id') {
  134. cur_id = record.id;
  135. }else if (rowKey == 'specId') {
  136. cur_id = record.specId;
  137. } else if (rowKey == 'brandId') {
  138. cur_id = record.brandId;
  139. } else if (rowKey == 'goodsId') {
  140. cur_id = record.goodsId;
  141. }else if (rowKey == 'categoryId') {
  142. cur_id = record.categoryId;
  143. }else if (rowKey == 'decoId') {
  144. cur_id = record.decoId;
  145. }else if (rowKey == 'seckillId') {
  146. cur_id = record.seckillId;
  147. } else if (rowKey == 'labelId') {
  148. cur_id = record.labelId;
  149. } else if (rowKey == 'integralGoodsId') {
  150. cur_id = record.integralGoodsId;
  151. } else if (rowKey == 'couponId') {
  152. cur_id = record.couponId;
  153. } else if (rowKey == 'attributeId') {
  154. cur_id = record.attributeId;
  155. } else if (rowKey == 'drawId') {
  156. cur_id = record.drawId;
  157. }
  158. let showClassName = '';
  159. if (this.props.showMarkColor && record.mark_color) {
  160. //去掉#
  161. let color = record.mark_color.replace('#', '');
  162. //小写转为大写,方便获取类名
  163. showClassName = 'row_sld_color_' + color.toUpperCase();
  164. }
  165. if (cur_id === this.state.rowId) {
  166. showClassName = 'seleSingle';
  167. }
  168. return showClassName;
  169. } else {
  170. return '';
  171. }
  172. };
  173. components = {
  174. header: {
  175. cell: ResizeableTitle,
  176. },
  177. };
  178. handleResize = index => (e, { size }) => {
  179. if (this.props.resizeTable) this.props.resizeTable(index, size);
  180. };
  181. onExpand = (expanded, record) => {
  182. if (this.props.onExpand) {
  183. this.props.onExpand(expanded, record);
  184. }
  185. };
  186. render() {
  187. const { selectedRowKeys, needTotalList } = this.state;
  188. const {
  189. data: { list, pagination },
  190. rowKey, isCheck, sldSeleSingleRow, sldpagination, scroll, isColumnResize,
  191. ...rest
  192. } = this.props;
  193. const paginationProps = {
  194. showSizeChanger: true,
  195. showQuickJumper: true,
  196. ...pagination,
  197. };
  198. const rowSelection = {
  199. type: this.props.sel_type != undefined ? this.props.sel_type : 'checkbox',
  200. selectedRowKeys:this.props.selectedRowKeys!=undefined?this.props.selectedRowKeys:selectedRowKeys,
  201. onChange: this.handleRowSelectChange,
  202. getCheckboxProps: record => ({
  203. disabled: (record.disabled == 'true' || record.disabled == true) ? true : false,
  204. }),
  205. };
  206. const columns = isColumnResize ? this.props.columns.map((col, index) => ({
  207. ...col,
  208. onHeaderCell: column => ({
  209. width: column.width,
  210. onResize: this.handleResize(index),
  211. }),
  212. })) : this.props.columns;
  213. let showData = <div className={styles.standardTableSlo_don}
  214. style={{ width: this.props.width != undefined ? this.props.width : '100%' }}>
  215. {this.props.expandedRowKeys != undefined && this.props.expandData == undefined && <Table
  216. expandedRowKeys={this.props.expandedRowKeys}
  217. showHeader={this.props.showHeader != undefined ? this.props.showHeader : true}
  218. bordered={this.props.border!=undefined?this.props.border:true}
  219. components={this.components}
  220. size={'small'}
  221. scroll={typeof (scroll) != 'undefined' ? scroll : {}}
  222. rowClassName={(record) => this.setRowClassName(rowKey, record)}
  223. onRow={(record, index) => {
  224. return {
  225. onClick: () => {
  226. if (this.props.onSldHandleSeleRow) {
  227. this.sldHandleRow(record, index, rowKey);
  228. }
  229. },
  230. };
  231. }}
  232. indentSize={this.props.indentSize}
  233. rowKey={rowKey || 'key'}
  234. rowSelection={isCheck ? rowSelection : null}
  235. dataSource={list}
  236. pagination={sldpagination != undefined ? (sldpagination ? paginationProps : false) : paginationProps}
  237. onChange={this.handleTableChange}
  238. onExpand={this.onExpand}
  239. {...rest}
  240. columns={columns}
  241. />}
  242. {this.props.expandedRowKeys == undefined && this.props.expandData != undefined && <Table
  243. expandedRowRender={(record) => {
  244. let expand_data = [];
  245. for (let i in this.props.expandData) {
  246. expand_data.push({
  247. lval: this.props.expandData[i].key,
  248. rval: sldEmptyHandle1(record[this.props.expandData[i].val]),
  249. });
  250. }
  251. return <SldRowMoreColumnLR item_width={400} data={expand_data}/>;
  252. }}
  253. showHeader={this.props.showHeader != undefined ? this.props.showHeader : true}
  254. bordered={this.props.border!=undefined?this.props.border:true}
  255. components={this.components}
  256. size={'small'}
  257. scroll={typeof (scroll) != 'undefined' ? scroll : {}}
  258. rowClassName={(record) => this.setRowClassName(rowKey, record)}
  259. onRow={(record, index) => {
  260. return {
  261. onClick: () => {
  262. if (this.props.onSldHandleSeleRow) {
  263. this.sldHandleRow(record, index, rowKey);
  264. }
  265. },
  266. };
  267. }}
  268. indentSize={this.props.indentSize}
  269. rowKey={rowKey || 'key'}
  270. rowSelection={isCheck ? rowSelection : null}
  271. dataSource={list}
  272. pagination={sldpagination != undefined ? (sldpagination ? paginationProps : false) : paginationProps}
  273. onChange={this.handleTableChange}
  274. {...rest}
  275. columns={columns}
  276. />}
  277. {this.props.expandedRowKeys == undefined && this.props.expandData == undefined && <Table
  278. showHeader={this.props.showHeader != undefined ? this.props.showHeader : true}
  279. bordered={this.props.border!=undefined?this.props.border:true}
  280. components={this.components}
  281. size={'small'}
  282. scroll={typeof (scroll) != 'undefined' ? scroll : {}}
  283. rowClassName={(record) => this.setRowClassName(rowKey, record)}
  284. onRow={(record, index) => {
  285. return {
  286. onClick: () => {
  287. if (this.props.onSldHandleSeleRow) {
  288. this.sldHandleRow(record, index, rowKey);
  289. }
  290. },
  291. };
  292. }}
  293. indentSize={this.props.indentSize}
  294. rowKey={rowKey || 'key'}
  295. rowSelection={isCheck ? rowSelection : null}
  296. dataSource={list}
  297. pagination={sldpagination != undefined ? (sldpagination ? paginationProps : false) : paginationProps}
  298. onChange={this.handleTableChange}
  299. {...rest}
  300. columns={columns}
  301. />}
  302. </div>;
  303. return (
  304. <Fragment>
  305. {this.props.showScrollbar!=undefined&&!this.props.showScrollbar
  306. ?showData
  307. :<Scrollbars autoHeight
  308. autoHeightMax={this.props.totalHeight != undefined ? this.props.totalHeight : document.body.clientHeight - 200} ref={'scrollRef'}>
  309. {showData}
  310. </Scrollbars>
  311. }
  312. </Fragment>
  313. );
  314. }
  315. }
  316. export default StandardTable;