4

Could someone provide a working example of a lightning:datatable that utilizes the 'Errors' attribute? I want to validate fields in a datatable and flag the cells that need correction. According to the documentation I have it is an object that specifies row and cell level errors. I have searched and searched but cannot seem to find anything. Thanks in advance. I am not on my work machine, but I can provide example code of what I am doing if needed.

1 Answer 1

5

When you define a datatable you asign an error attribute to it.

Now whenever you feel that you should display error for a particular cell after a validation session, you just input values to the errors attribute.

Define the errors and map them to fieldNames. When the error is triggered, the borders for the amount and contact cells turn red to represent the error state.

<lightning:datatable keyField="id" data="{! v.mydata }" columns="{! v.mycolumns }"
    errors="{!v.errors}"/>

JS Code:

validateValues : function(component , event ,helper){

    //Run your validate logic

    cmp.set('v.errors', {
                rows: {
                    b: {
                        title: 'We found 2 errors.',
                        messages: [
                            'Enter a valid amount.',
                            'Verify the email address and try again.'
                        ],
                        fieldNames: ['amount', 'contact']
                    }
                },
                table: {
                    title: 'Your entry cannot be saved. Fix the errors and try again.',
                    messages: [
                        'Row 2 amount must be number',
                        'Row 2 email is invalid'
                    ]
                }
            });

}

enter image description here

Src: https://developer.salesforce.com/docs/component-library/bundle/lightning:datatable/documentation

Edit: I added a small demo repo that i created to see the behaviour of datatable erros. You can refer it

https://github.com/pranayjswl007/lightningDatatableError

2
  • @BradC if this answer solved your question - please mark it as accepted Commented Nov 18, 2018 at 7:34
  • I am trying exact same thing for LWC. It is not working. I tried row# as a,b,c and 0,1,2. Nothing seems to be working. triggerError(event) { this.errors = { rows: { 1: { title: '', messages: [ '.' ], fieldNames: [''] } }, table: { title: '.', messages: [ '' ] } }; } Commented Aug 30, 2023 at 20:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .