From 06404e701a89955958cbf56213e2eec618d8644d Mon Sep 17 00:00:00 2001 From: king <18310653075@163.com> Date: 星期三, 16 十二月 2020 18:36:16 +0800 Subject: [PATCH] 2020-12-16 --- src/tabviews/custom/components/card/data-card/index.jsx | 290 +++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 files changed, 248 insertions(+), 42 deletions(-) diff --git a/src/tabviews/custom/components/card/data-card/index.jsx b/src/tabviews/custom/components/card/data-card/index.jsx index 02f4756..373bcd7 100644 --- a/src/tabviews/custom/components/card/data-card/index.jsx +++ b/src/tabviews/custom/components/card/data-card/index.jsx @@ -1,75 +1,191 @@ import React, {Component} from 'react' import PropTypes from 'prop-types' import { is, fromJS } from 'immutable' -import { Spin, Empty, notification } from 'antd' +import { Spin, Empty, notification, Col, Pagination } from 'antd' -import asyncComponent from '@/utils/asyncComponent' import Api from '@/api' -// import Utils from '@/utils/utils.js' import UtilsDM from '@/utils/utils-datamanage.js' -import zhCN from '@/locales/zh-CN/main.js' -import enUS from '@/locales/en-US/main.js' import preImg from '@/assets/img/prev.png' import nextImg from '@/assets/img/next.png' +import MKEmitter from '@/utils/events.js' +import asyncComponent from '@/utils/asyncComponent' import './index.scss' const CardItem = asyncComponent(() => import('../cardItem')) +const MainAction = asyncComponent(() => import('@/tabviews/zshare/actionList')) class DataCard extends Component { static propTpyes = { BID: PropTypes.any, // 鐖剁骇Id data: PropTypes.array, // 缁熶竴鏌ヨ鏁版嵁 config: PropTypes.object, // 缁勪欢閰嶇疆淇℃伅 - mainSearch: PropTypes.any, // 鍏ㄥ眬鎼滅储鏉′欢 + mainSearch: PropTypes.any, // 澶栧眰鎼滅储鏉′欢 menuType: PropTypes.any, // 鑿滃崟绫诲瀷 - dataManager: PropTypes.any, // 鏁版嵁鏉冮檺 } state = { - dict: localStorage.getItem('lang') !== 'en-US' ? zhCN : enUS, // 瀛楀吀 + BID: '', // 涓婄骇ID + BData: '', // 涓婄骇琛屾暟鎹� config: null, // 鍥捐〃閰嶇疆淇℃伅 - pageIndex: 1, + pageIndex: 1, // 椤电爜 + activeKey: '', // 閫変腑鍗� + selectKeys: [], // 澶氶�夋椂閫変腑鍗$墖 + selectedData: [], // 閫変腑鏁版嵁锛岀敤浜庡伐鍏锋爮鎸夐挳 loading: false, // 鏁版嵁鍔犺浇鐘舵�� + sync: false, // 鏄惁缁熶竴璇锋眰鏁版嵁 + card: null, // 鍗$墖璁剧疆 data: null, // 鏁版嵁 total: null } UNSAFE_componentWillMount () { + const { data, initdata, BID } = this.props let _config = fromJS(this.props.config).toJS() + let _card = _config.subcards[0] + let _cols = new Map() + + let _data = null + let _sync = _config.setting.sync === 'true' + + if (_config.setting.sync === 'true' && data) { + _data = data[_config.dataName] || [] + _sync = false + } else if (_config.setting.sync === 'true' && initdata) { + _data = initdata || [] + _sync = false + } + + if (_data) { + _data = _data.map((item, index) => { + item.key = index + item.$$uuid = item[_config.setting.primaryKey] || '' + item.$$BID = BID || '' + return item + }) + } + + _config.columns.forEach(item => { + _cols.set(item.field, item) + }) + + _card.elements = _card.elements.map(item => { + if (item.field && _cols.has(item.field)) { + item.col = _cols.get(item.field) + } + return item + }) + _card.backElements = _card.backElements.map(item => { + if (item.field && _cols.has(item.field)) { + item.col = _cols.get(item.field) + } + return item + }) this.setState({ + sync: _sync, + data: _data, + BID: BID || '', config: _config, + card: _card, arr_field: _config.columns.map(col => col.field).join(','), }, () => { - this.loadData() + if (_config.setting.sync !== 'true' && _config.setting.onload === 'true') { + this.loadData() + } }) } - /** - * @description 鏍¢獙鍥捐〃鐨勬寜閽粍锛屽鏋滀负缁熻鍥捐〃锛岃绠楀浘琛ㄥ瓧娈� - */ componentDidMount () { - - } - - /** - * @description 鍥捐〃鏁版嵁鏇存柊锛屽埛鏂板唴瀹� - */ - UNSAFE_componentWillReceiveProps (nextProps) { - + MKEmitter.addListener('syncRefreshComponentId', this.reload) + MKEmitter.addListener('resetSelectLine', this.resetParentParam) } shouldComponentUpdate (nextProps, nextState) { return !is(fromJS(this.state), fromJS(nextState)) } + UNSAFE_componentWillReceiveProps (nextProps) { + const { sync, config, BID } = this.state + + if (sync && !is(fromJS(this.props.data), fromJS(nextProps.data))) { + let _data = [] + if (nextProps.data && nextProps.data[config.dataName]) { + _data = nextProps.data[config.dataName] || [] + } + + _data = _data.map((item, index) => { + item.key = index + item.$$uuid = item[config.setting.primaryKey] || '' + item.$$BID = BID || '' + return item + }) + + this.setState({sync: false, data: _data}) + } else if (!is(fromJS(this.props.mainSearch), fromJS(nextProps.mainSearch))) { + if (config.setting.syncRefresh === 'true') { + this.setState({}, () => { + this.loadData() + }) + } + } + } + + componentWillUnmount () { + this.setState = () => { + return + } + MKEmitter.removeListener('syncRefreshComponentId', this.reload) + MKEmitter.removeListener('resetSelectLine', this.resetParentParam) + } + + reload = (syncId) => { + const { config } = this.state + + if (syncId && syncId !== config.uuid) return + + this.setState({ + pageIndex: 1 + }, () => { + this.loadData() + }) + } + + resetParentParam = (MenuID, id, data) => { + const { config } = this.state + + if (!config.setting.supModule || config.setting.supModule !== MenuID) return + if (id !== this.state.BID) { + this.setState({ BID: id, BData: data }, () => { + this.loadData() + }) + } + } + async loadData () { - const { mainSearch, BID, menuType, dataManager } = this.props - const { config, arr_field, pageIndex } = this.state - + const { mainSearch, menuType } = this.props + const { config, arr_field, pageIndex, BID } = this.state + + if (config.setting.supModule && !BID) { // BID 涓嶅瓨鍦ㄦ椂锛屼笉鍋氭煡璇� + this.setState({ + activeKey: '', + selectKeys: [], + selectedData: [], + pageIndex: 1, + data: [], + total: 0, + loading: false + }) + return + } + let searches = [] if (mainSearch && mainSearch.length > 0) { // 涓昏〃鎼滅储鏉′欢 - searches = [...mainSearch, ...searches] + let keys = searches.map(item => item.key) + mainSearch.forEach(item => { + if (!keys.includes(item.key)) { + searches.push(item) + } + }) } this.setState({ @@ -77,12 +193,20 @@ }) let _orderBy = config.setting.order || '' - let param = UtilsDM.getQueryDataParams(config.setting, arr_field, searches, _orderBy, pageIndex, config.setting.pageSize, BID, menuType, dataManager) + let param = UtilsDM.getQueryDataParams(config.setting, arr_field, searches, _orderBy, pageIndex, config.setting.pageSize, BID, menuType) let result = await Api.genericInterface(param) if (result.status) { this.setState({ - data: result.data, + activeKey: '', + selectKeys: [], + selectedData: [], + data: result.data.map((item, index) => { + item.key = index + item.$$uuid = item[config.setting.primaryKey] || '' + item.$$BID = BID || '' + return item + }), total: result.total, loading: false }) @@ -98,6 +222,20 @@ } } + updateStatus = (type, position, btn) => { + const { config } = this.state + + if (type === 'refresh' && position === 'grid') { + this.loadData() + if (btn && btn.syncComponent && btn.syncComponent[0]) { + let syncId = btn.syncComponent.slice(-1)[0] + if (config.uuid !== syncId) { + MKEmitter.emit('syncRefreshComponentId', syncId) + } + } + } + } + prevPage = () => { const { pageIndex } = this.state @@ -109,6 +247,7 @@ this.loadData() }) } + nextPage = () => { const { config, pageIndex, total } = this.state let _total = config.setting.pageSize * pageIndex @@ -122,31 +261,98 @@ }) } - render() { - const { config, loading, data, pageIndex, total } = this.state + changePageIndex = (page) => { + this.setState({ + pageIndex: page + }, () => { + this.loadData() + }) + } + + changeCard = (index, item) => { + const { config, selectKeys, selectedData, activeKey } = this.state - let _total = config.setting.pageSize * pageIndex - let pageable = config.pageable && config.setting.laypage - if ((pageIndex === 1 && total <= _total) || !data) { - pageable = false + if (!config.wrap.cardType) return + + let _selectKeys = [] + let _selectedData = [] + let _activeKey = '' + let _item = item + if (config.wrap.cardType === 'checkbox') { + if (activeKey === index) { + _selectKeys = selectKeys.filter(key => key !== index) + _selectedData = selectedData.filter(cell => cell.key !== index) + _activeKey = _selectKeys.slice(-1)[0] + _item = selectedData.slice(-1)[0] || '' + } else if (selectKeys.indexOf(index) > -1) { + _selectKeys = selectKeys.filter(key => key !== index) + _selectedData = selectedData.filter(cell => cell.key !== index) + _activeKey = activeKey + _item = selectedData.filter(cell => cell.key === activeKey)[0] || '' + } else { + _selectKeys = [...selectKeys, index] + _selectedData = [...selectedData, item] + _activeKey = index + } + } else { + if (activeKey === index) return + + _selectedData = [item] + _activeKey = index + } + + this.setState({ + activeKey: _activeKey, + selectKeys: _selectKeys, + selectedData: _selectedData + }) + + MKEmitter.emit('resetSelectLine', config.uuid, (_item ? _item.$$uuid : ''), _item) + } + + render() { + const { config, loading, data, pageIndex, total, card, activeKey, BID, BData, selectedData, selectKeys } = this.state + + let _total = 0 + let switchable = false + if (config.wrap.pagestyle === 'switch' && config.pageable && config.setting.laypage && total > config.setting.pageSize && data) { + _total = config.setting.pageSize * pageIndex + switchable = true } return ( - <div className="custom-card-box" style={config.style}> + <div className="custom-data-card-box" style={{...config.style, minHeight: config.wrap.minHeight}}> {loading ? <div className="loading-mask"> <div className="ant-spin-blur"></div> <Spin /> </div> : null } - {pageable ? <div className={'prev-page ' + (pageIndex === 1 ? 'disabled' : '')} onClick={this.prevPage}><div><div><img src={preImg} alt=""/></div></div></div> : null} - {data && data.length > 0 ? <div className="card-row-list"> - {data.map((item, index) => ( - <CardItem key={index} card={config.subcards[0]} cards={config} data={item} /> - ))} - </div> : null} - {pageable ? <div className={'prev-page ' + (total <= _total ? 'disabled' : '')} onClick={this.nextPage}><div><div><img src={nextImg} alt=""/></div></div></div> : null} - {data && data.length === 0 ? <Empty description={false}/> : null} + {config.action && config.action.length > 0 ? + <MainAction + BID={BID} + BData={BData} + setting={config.setting} + actions={config.action} + columns={config.columns} + selectedData={selectedData} + refreshdata={this.refreshbyaction} + getexceloutparam={this.getexceloutparam} + /> : null + } + <div className="data-zoom"> + {switchable ? <div className={'prev-page ' + (pageIndex === 1 ? 'disabled' : '')} onClick={this.prevPage}><div><div><img src={preImg} alt=""/></div></div></div> : null} + {data && data.length > 0 ? <div className="card-row-list"> + {data.map((item, index) => ( + <Col className={activeKey === index ? 'active' : (selectKeys.indexOf(index) > -1 ? 'selected' : '')} key={index} span={card.setting.width} onClick={() => {this.changeCard(index, item)}}> + <CardItem card={card} cards={config} data={item} updateStatus={this.updateStatus}/> + </Col> + ))} + </div> : null} + {switchable ? <div className={'prev-page ' + (total <= _total ? 'disabled' : '')} onClick={this.nextPage}><div><div><img src={nextImg} alt=""/></div></div></div> : null} + {data && data.length === 0 ? <Empty description={false}/> : null} + </div> + {config.wrap.pagestyle !== 'switch' && config.setting.laypage && data ? <Pagination size="small" total={total} showTotal={t => `鍏� ${t} 鏉} pageSize={config.setting.pageSize} onChange={this.changePageIndex} current={pageIndex}/> : null} </div> ) } -- Gitblit v1.8.0