如何禁用反应物料表中的某些特定列进行编辑?

时间:2020-01-19 17:35:44

标签: reactjs material-ui

    import React from 'react';
    import MaterialTable from 'material-table';

    export default class DictionaryTable extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
        columns: [
            {title:'Need state',field: 'NeedState',lookup: { 1: 'Fragrance', 2: 'Price', 3:'Formulation',4:'Technolgy'} },
            { title: 'Keyword extracted', field: 'keyword' },
            { title: 'Actual Keyword', field: 'actual_keyword' },
            { title: 'Map score', field: 'map_score', type: 'numeric' },
            { title: '6 month mentions', field: 'six_month_mention'},
        ],
        data: [
            { NeedState: 1, keyword: 'mango', actual_keyword: 'apple', map_score: .3 ,six_month_mention:234},
            { NeedState: 2, keyword: 'expensive', actual_keyword: 'price', map_score: .6 ,six_month_mention:45},
            { NeedState: 2, keyword: 'costly', actual_keyword: 'price', map_score: .43 ,six_month_mention:433},
            { NeedState: 3, keyword: 'chemical', actual_keyword: 'oil', map_score: .43 ,six_month_mention:68},
            { NeedState: 4, keyword: 'humidifier', actual_keyword: 'diffuser', map_score: .63 ,six_month_mention:987},
        ]
        }
    }


    render() {
        return (
        <MaterialTable
            title={<div style={!this.state.editable ? { color:'red', fontSize:"18px" } : { color:'red', fontSize:"12px" }}>
            <p class={!this.state.editable ? "bg-light text-dark br6 pa-5-10" : "negative-bg-button text-white br6 pa-5-10" }>
                {
                !this.state.editable ? 
                (
                    <span>Run request for - {this.state.request_name}</span>
                ):(
                    <span>
                    <i class="fa">
                        &#xf071;
                    </i> 
                    &nbsp; Caution: The dictionary is on edit mode. Any changes made will trigger a pipeline rerun with updated dictionary
                    </span>
                )
                }
                </p>
            </div>}
            columns={this.state.columns}
            columns={[
            ...this.state.columns.map(data => {
                return {
                    field: data.field,
                    title: data.title,
                    isEditable: data["actual_keyword"] = false,
                    render: rowData => 
                        (
                            <div style={ data.field === "map_score" ? { color : "red",borderRadius:'3px',paddingLeft:'10px' } : {} }> 
                            {
                            data.field === "map_score" ?
                            (
                                <span>
                                {
                                    rowData[data.field] > 0.8 ?
                                    <i class="fas fa-thermometer-full positive-color-font"></i>
                                    : rowData[data.field] > 0.6 ?
                                    <i class="fas fa-thermometer-half neutral-color-font"></i> :
                                    <i class="fas fa-thermometer-quarter negative-color-font"></i>
                                } 
                                </span>
                            ):(
                                <span>
                                {
                                    data.field === "NeedState" ?
                                    (
                                    <span>
                                        {rowData["show_needstate"]}
                                    </span>
                                    ):data.field === "show_needstate" ? (
                                    <span style={{ display:'none' }}>

                                    </span>
                                    ) : (
                                    <span>
                                        {rowData[data.field]}
                                    </span>
                                    )
                                }

                                </span>
                            )
                            } 
                            </div>
                        )
                };
                })
        ]}
            data={this.state.data}
            editable={{
            onRowAdd: newData =>
                new Promise(resolve => {
                setTimeout(() => {
                    resolve();
                    this.setState(prevState => {
                    const data = [...prevState.data];
                    data.push(newData);
                    return { ...prevState, data };
                    });
                }, 1);
                }),
            onRowUpdate: (newData, oldData) =>
                new Promise(resolve => {
                setTimeout(() => {
                    resolve();
                    if (oldData) {
                    this.setState(prevState => {
                        const data = [...prevState.data];
                        data[data.indexOf(oldData)] = newData;
                        return { ...prevState, data };
                    });
                    }
                }, 1);
                }),
            onRowDelete: oldData =>
                new Promise(resolve => {
                setTimeout(() => {
                    resolve();
                    this.setState(prevState => {
                    const data = [...prevState.data];
                    data.splice(data.indexOf(oldData), 1);
                    return { ...prevState, data };
                    });
                }, 1);
                }),
            }}
        />
        );
    }
    }

我在这里共享了完整的组件。

在这里,我使用表 material

它工作正常,但我只想实现一项功能。

当我单击“编辑”时,所有行均处于可编辑状态。

但是我想防止两个列 six_month_mention map_score 不可编辑。

有什么办法可以实现?

请看看

1 个答案:

答案 0 :(得分:10)

U可以通过使用实质性UI中的列的“ editable”属性来使这些列禁用编辑。 为此,您必须设置editable:“从不”。在其中声明了列的所有标题和字段属性的地方。

我无法格式化答案,因为我正在使用移动电话来回答。enter image description here