Search code examples
reactjsmaterial-table

auto-generated current date in a column using materialtable in reactjs


when i want to add a new row in the materialtable i need that the field date must be auto-generated and taks the value of current date and this date can't be editable in update or in insert enter image description here

this my solution but its not working

      columns: [
              {
...,
                    title: 'date',
                    field: 'fieldDate',
                    type: 'date',
                    default: new Date()
                },
...]

Solution

  • According to the documentation you can use the initialEditValue to set the value of a new row: https://material-table.com/#/docs/all-props

    Your columns definition would be:

    columns: [
        {
            ...,
            title: 'date',
            field: 'fieldDate',
            type: 'date',
            initialEditValue: new Date()
        },
        ...
    ]