[SalesForce] Displaying multiple errors in lightning:datatable

As per below salesforce documentation we can display error for lightning datatable by setting the "error" attribute.

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

triggerError: function (cmp, event, helper) {
    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'
            ]
        }
    });
}

the above example is for showing one error. Since lightning:datatable has multi-edit functionality, a user can try to edit multiple rows at the same time.

is there any way to display the error for all the edited rows?

I tried to do something like below, but I am getting compilation error.

component.set('v.errors', {
                rows: [{
                    a: {
                        title: 'We found 2 errors.',
                        messages: [
                            'Enter a valid amount.',
                            'Verify the email address and try again.'
                        ],
                        fieldNames: ['estDebt', 'responsibility']
                    }
                },
                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'
                    ]
                },
                {
                    b: {
                        title: 'We found 2 errors.',
                        messages: [
                            'Enter a valid amount.',
                            'Verify the email address and try again.'
                        ],
                        fieldNames: ['estDebt', 'responsibility']
                    }
                },
                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'
                ]
            }]
        });

Best Answer

Your format is messed up. There should be two top-level objects, rows and table. The rows key should contain keys that match the keyField values for the table. Table messages themselves should be in the table key.

{ rows: 
  { a: { title: "...", messages: [...], fieldNames: [...] }, 
    b: { title: "...", messages: [...], fieldNames: [...] }
  },
  table: { title: "...", messages: [...] }
}

Assuming you had a list of errors from the server, you might do something like this:

var errors = { rows: {}, table: {} }
errorsFromServer.forEach(
  error => errors.rows[error.Id] = { title: "...", messages: [...], fieldNames: [...] }
);
errors.table.title = "We found "+errorsFromServer.length+" error(s). ...";
errors.table.messages = [...];
cmp.set("v.errors", errors);