Posted 27 June 2019, 6:11 am EST
Hi,
You may use the CollectionView’s getError property to check whether the grid or the collectionView has any error. The getProperty takes a Function with 2 parameters, the item and the property to be validated.
new wjcCore.CollectionView(data, {
getError: (item, propName: string) => {
var data = item[propName];
if(typeof data !== 'undefined' && !wjcCore.isNullOrWhiteSpace(data.toString())) {
return '';
}
return 'Value cannot be empty';
}
});
Now, you need to check the grid for errors in the loadedRows event of the FlexGrid. In this event, you need to save all of the rows that currently have errors in an array. We can use this array to enable or disable the button.
HTML:
<wj-flex-grid #grid (loadedRows)="loadRows(grid, $event)" (rowEditEnded)="rowEditEnded(grid, $event)" [itemsSource]="source"
[validateEdits]="false">
</wj-flex-grid><br><br>
<button (click)="onClick()" [disabled]="errorRows.length > 0">Submit</button>
TS:
errorRows: any[];
loadRows(s, e) {
var self = this;
s.rows.forEach(r => {
var data = r.dataItem;
let errorFlag = this.checkForError(s.columns, data);
if (errorFlag) {
self.errorRows.push(r.index);
}
});
}
Then, you also need to handle the rowEditEnded event of FlexGrid. In this event, we will check for any error in the row that’s just been edited.
rowEditEnded(s, e) {
var rowIdx = e.row;
var data = s.rows[rowIdx].dataItem
var errors = this.checkForError(s.columns, data);
if(errors && this.errorRows.indexOf(rowIdx) == -1) {
this.errorRows.push(rowIdx);
}
else if(!errors) {
var idx = this.errorRows.indexOf(rowIdx);
this.errorRows.splice(idx, 1);
}
}
Please refer to the following sample demonstrating the same:
https://stackblitz.com/edit/angular-uc7x3o
Regards