0

I was writing a reactjs code and I use react-data-table-component, my table showed perfectly but i wanted to hide/show a column base on condition if true or false but I do not know where to write my javascript/typescript expression for the condition to determine if the column will be show or not. See below the basic code to display record in react-data-table-component:

//building the columns header
const columns = [
    {
        name: 'Title',
        selector: row => row.title,
    },
    {
        name: 'Year',
        selector: row => row.year,
    },
 {
      /*How do i write javascript conditional code here to determine if this column will be shown or not */
        name: 'Salary',
        selector: row => row.year,
        
    },
];

const data = [
    {
        id: 1,
        title: 'Beetlejuice',
        year: '1988',
        salary: '5000'
    },
    {
        id: 2,
        title: 'Ghostbusters',
        year: '1984',
        salary: '5000'
    },
]

function MyComponent() {
    return (
        <DataTable
            columns={columns}
            data={data}
        />
    );
};

My challenge is how/where do I write code to display or hide the column because all efforts to write the javascript/typescript in the column object didn't work. Note that I was able to manipulate or change record at row level using the conditional-row approach but it is the entire column i want to hide/show should a condition is met or not.

Thank you fams.

2 Answers 2

2

You can dynamically omit a column using "omit" prop as shown in the documentation

For example:

<DataTable
            columns={[
                {
                    name: "ID",
                    selector: row => row.id,
                },
                {
                    name: "Other Data",
                    selector: row => row.otherData,
                    omit: hideData === true
                }
            ]}
            data={data}
        />}
Sign up to request clarification or add additional context in comments.

Comments

0

Have you tried making a separate column, or writing a column object builder? You can append the column you want depending on your condition, and set that build to the columns object your table uses.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.