簡體   English   中英

react-admin通過自定義形式刷新dataGrid

[英]react-admin refresh dataGrid by custom form

首先,我想說的是,我是新手(我討厭前沿發展,但是,您知道,有時候我們在工作生涯中沒有選擇)。

因此,我使用react-admin創建了一個自定義表單,而沒有使用react-admin的REST連接(這是一種特定的表單)。 表單驗證之后,一個名為processingStatut的值將更改多個數據,並且需要在react-admin映射的<List><Datagrid>顯示此新值。

所以我遵循文檔來創建一個reducer操作,以在我的dataGrid中更改一個名為processingStatut的布爾值,如下所示:

epassesReceived.js

export const EPASSES_RECEIVED = 'EPASSES_RECEIVED';
export const epassesReceived = (data) => ({
   type: EPASSES_RECEIVED,
   payload: { data },
});

我的customForm.js

import { epassesReceived as epassesReceivedAction } from './epassesReceived';
handleSubmit(event) {
    this.setState({
        post: this.post
    });
    const { fetchJson } = fetchUtils;
    const {
        showNotification,
        history,
        push,
        epassesReceived,
        fetchStart, fetchEnd
    } = this.props;
    const url = `${API_URL}/ePasses/update`;
    const datas = JSON.stringify(this.state);
    const options = {
        method: 'POST',
        body: datas
    };
    fetchStart();
    fetchJson(url, options)
        .then( response => epassesReceived(response.json) )
        .then(() => {
            showNotification('ra.notification.epasseRecorded');
            history.goBack();
        })
        .catch( error => {
            console.error(error);
            var message = error.message.replace(/ /g, '');
            showNotification(`ra.notification.${message}`, 'warning');
        })
        .finally(fetchEnd);
        event.preventDefault();
}

...

const mapStateToProps = state => ({
    customReducer: state.customReducer
});

export const EpassesUpdate = connect(mapStateToProps, {
    epassesReceived: epassesReceivedAction, 
    showNotification, 
    push,fetchStart, fetchEnd
})(translate(withStyles(formStyle)(EpassesUpdateView)));

並在我的app.js中

import { EPASSES_RECEIVED } from './epassesReceived';
const customReducer = (previousState = 0, { type, payload }) => {
    console.log(payload, type);
    if (type == EPASSES_RECEIVED) {
        // console.log('modif');
        // payload.data[0].processingStatut=1; this is the purpose of the script. To show de modification changed after form's validation
        return payload;
    }

    return previousState;
}

和viewDataGrid.js

<List 
        classes={props.classes} 
        {...props} 
        exporter={exporter} 
        title='ePass.pageTitle' 
        perPage={15} 
        pagination={<PostPagination />} 
        filters={<EPassFilter businessunit={businessUnit} />} 
        bulkActions={<EPassBulkActions businessunit={businessUnit} />} 
        actions={<PostActions businessUnit={businessUnit} />}
    >
        <Datagrid classes={props.classes}>
            { businessUnit === undefined || !businessUnit.companyName &&
                <TextField source="businessUnitName" label="ePass.businessUnitName" />
            }
            <StateField source="processingStatut" label="" translate={props.translate} />
.....

但是在我的控制台日志中,我的值沒有改變,我現在也不為什么......如果我按F5刷新網頁,這當然是可行的,因為該值在數據庫中已更改。 但是不在react的dataGrid中...我迷路了...

也許日志輸出會有所幫助:

log_output

我們可以看到“ EPASSES_RECEIVED”類型,並且數據已更改

我認為您的問題來自您的獲取。 嘗試這個 :

fetch(url, options)
        .then( response => response.json() )
        .then(data => {
            epassesReceived(data);
            showNotification('ra.notification.epasseRecorded');
            history.goBack();
        })
        .catch( error => {
            console.error(error);
            var message = error.message.replace(/ /g, '');
            showNotification(`ra.notification.${message}`, 'warning');
        })
        .finally(fetchEnd);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM