material-table icon indicating copy to clipboard operation
material-table copied to clipboard

How to disable some specific columns from edit in react material table?

Open monetree opened this issue 6 years ago • 5 comments

    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);
                }),
            }}
        />
        );
    }
    }

Here i am using react material table.

It is working fine but i just wants to achive one feature.

When i am clicking edit all rows are getting editable state.

But i wants to prevent two columns six_month_mention and map_score from being editable.

Is there any way to achive it ?

Please have a look

monetree avatar Jan 19 '20 17:01 monetree

There is an example to do just that in their demo. See: https://material-table.com/#/docs/features/editable

6d70 avatar Jan 19 '20 22:01 6d70

There is an example to do just that in their demo. See: https://material-table.com/#/docs/features/editable

This example doesnt work :/ I'm using export default function instead of class Extending React.

I'm using React.useState({ columns : [ { title:'data', field: 'data', editable: 'never'} ... ] basic onRow,OnAdd, ... }

Yet it has not effect. I can still update that row while I had put in my code that it should be never editable 👎

tspanda avatar Feb 13 '20 21:02 tspanda

@tspanda What version of material-table are you using, I created a quick demo using the same code which might help.

https://codesandbox.io/s/material-ui-demo-x8ccq?fontsize=14&hidenavigation=1&theme=dark

6d70 avatar Feb 13 '20 22:02 6d70

i wish there'd be some way to at least disable items in the dropdown even if there is no feature to indent multi level dropdown items.

chitgoks avatar Apr 06 '20 07:04 chitgoks

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. You can reopen it if it required.

stale[bot] avatar Jul 05 '20 18:07 stale[bot]