2

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.

4

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

  • @BradC if this answer solved your question - please mark it as accepted – Itai Shmida Nov 18 '18 at 7:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.