import React, {Component} from 'react'; import {Col, Form, Input, Row, Table, Space, Button, Tag, message} from 'antd'; import { FilterOutlined, PlusCircleOutlined, } from '@ant-design/icons'; const { Search } = Input; class IceTable extends React.Component { state = { data: [], pagination: {}, loading: true, fetchConfig: false, //filter: null, showLoading: true, currentElement: null, fetchCompleted: false, }; constructor(props) { super(props); } componentDidMount() { const fetchConfig = { page: 1, }; message.config({ top: 40, }); this.setState({ fetchConfig, //filter: this.props.adapter.filter, pagination: { 'pageSize': this.props.reader.pageSize } }); //this.fetch(fetchConfig); } handleTableChange = (pagination, filters, sorter) => { const pager = { ...this.state.pagination }; const { search } = this.state; pager.current = pagination.current; this.setState({ pagination: pager, }); const fetchConfig = { limit: pagination.pageSize, page: pagination.current, sortField: sorter.field, sortOrder: sorter.order, filters: JSON.stringify(filters), search: search, }; this.setState({ fetchConfig }); this.fetch(fetchConfig); }; reload = () => { const fetchConfig = this.state.fetchConfig; if (fetchConfig) { this.fetch(fetchConfig) } }; search = (value) => { this.setState({ search: value }); const fetchConfig = this.state.fetchConfig; console.log(fetchConfig); if (fetchConfig) { fetchConfig.search = value; this.setState({ fetchConfig }); this.fetch(fetchConfig) } } addNew = () => { this.props.adapter.renderForm(); } showFilters = () => { this.props.adapter.showFilters(); } setFilterData = (filter) => { this.setState({ filter, }); } setCurrentElement = (currentElement) => { this.setState({currentElement}); } setLoading(value) { this.setState({ loading: value }); } fetch = (params = {}) => { //this.setState({ loading: this.state.showLoading }); this.setState({ loading: true }); //const hideMessage = message.loading({ content: 'Loading Latest Data ...', key: 'loadingTable', duration: 1}); const pagination = { ...this.state.pagination }; if (this.props.adapter.localStorageEnabled) { try { const cachedResponse = this.props.reader.getCachedResponse(params); if (cachedResponse.items) { this.setState({ loading: false, data: cachedResponse.items, pagination, showLoading: false, }); } else { this.props.reader.clearCachedResponse(params); } } catch (e) { this.props.reader.clearCachedResponse(params); } } this.props.reader.get(params) .then(data => { // Read total count from server // pagination.total = data.totalCount; pagination.total = data.total; //hideMessage(); // setTimeout( // () => message.success({ content: 'Loading Completed!', key: 'loadingSuccess', duration: 1 }), // 600 // ); this.setState({ loading: false, data: data.items, pagination, showLoading: false, fetchCompleted: true, }); }); }; getChildrenWithProps(element) { const childrenWithProps = React.Children.map(this.props.children, child => { // checking isValidElement is the safe way and avoids a typescript error too const props = { element, adapter: this.props.adapter, loading: this.state.loading, }; if (React.isValidElement(child)) { return React.cloneElement(child, props); } return child; }); return childrenWithProps; } render() { return ( {!this.state.currentElement && {this.props.adapter.hasAccess('save') && this.props.adapter.getShowAddNew() && } {this.props.adapter.getFilters() && } {this.state.fetchCompleted && this.props.adapter.getFilters() && this.props.adapter.filter != null && this.props.adapter.filter !== [] && this.props.adapter.filter !== '' && this.props.adapter.getFilterString(this.props.adapter.filter) !== '' && this.props.adapter.resetFilters()} visible={this.props.adapter.filter != null && this.props.adapter.filter !== [] && this.props.adapter.filter !== ''} > {this.props.adapter.getFilterString(this.props.adapter.filter)} }
this.form = formRef} name="advanced_search" className="ant-advanced-search-form" > this.search(value)} />
index % 2 === 0 ? 'table-row-light' : 'table-row-dark'} columns={this.props.columns} rowKey={record => record.id} dataSource={this.state.data} pagination={this.state.pagination} loading={this.state.loading} onChange={this.handleTableChange} reader={this.props.dataPipe} /> } {this.state.currentElement && this.getChildrenWithProps(this.state.currentElement) } ); } } export default IceTable;