Grid custom validations

Hello Syncfusion,

I have a few questions to ask with grid custom validations than i can't implement.

Query 1: Compare cell value on validation with other cells in grid.

So what is happening right now is that when custom validation is called it gets only one cell value where custom validation is set. What i need to do is in custom validation property i need to compare that cell value with other cell values. 



Query 2: After entering value in first cell, set validation focus to second cell

What i mean is that when you enter first cell value and it passes validations but then the second cell does not pass the requirements, focus validations to the second cell.

For example. As in picture below we first set focus in the first cell and for example we enter value of 10. The first cell now passes the validations cause it is greater than 0. But i need another validation to kick in, where 9 (the second cell value) cannot be less than 10 (first cell value) and in this case i want to get focus to the second cell and apply validation error there.



1 Reply

MF Mohammed Farook J Syncfusion Team June 1, 2018 12:14 PM UTC

Hi Domants, 

Thanks for contacting Syncfusion support. 

Query 1:  Query 1: Compare cell value on validation with other cells in grid. 
 
Yes we can compare current value with other cell value in custom validation . please find the code example. 
 

  public customFn: (args: { [key: number]: number }) => boolean = (args: { [key: number]: number }) => { 
        // get the current cell element 
        this.currentCell = parentsUntil((<any>args).element.parentElement, 'e-rowcell'); 
       // get the value of other cell 
        this.nextMaxCellValue = parseInt(this.currentCell.nextElementSibling.nextElementSibling.innerHTML, 10); 
    return args['value'] > 0 && this.nextMaxCellValue > args['value']; 
  } 



Query 2: After entering value in first cell, set validation focus to second cell 

We have created a sample based on requirement by using ‘editCell’ and ‘saveCell’ method. Please find the code example. 


[app.component.html] 

<ejs-grid #grid [dataSource]='data' (dataBound)='dataBound($event)' (cellSave)='cellSave($event)' [editSettings]='editSettings' [toolbar]='toolbar' height='273px'> 
    <e-columns> 
        <e-column field='OrderID' headerText='Order ID' textAlign='Right' isPrimaryKey='true' [validationRules]='orderIDRules' width=100></e-column> 
        <e-column field='minValue' headerText='Min Quentity' [validationRules]='minRules' width=120></e-column> 
        <e-column field='maxValue' headerText='Max Quentity' [validationRules]='maxRules' textAlign='Right' editType='numericedit' width=120></e-column> 
        <e-column field='stock' headerText='stock' editType='dropdownedit' width=150></e-column> 
    </e-columns> 
</ejs-grid> 



We can get current row index from ‘cellSave’ method of Grid. Please find the code example 


    public cellSave(args: any): void { 
 
        if(args.columnName === 'minValue') { 
// get current row index 
 
            this.editRowIndex = parseInt(args.cell.parentElement.getAttribute('aria-rowindex'), 10); 
            this.isApplyEdit = true; 
        } 
    } 


To edit the next cell and apply the validation programmatically by using ‘editCell’ and ‘saveCell’ method of Grid in the toolbar-refresh internal event. Please find the code example. 

 
public dataBound(): void { 
        this.grid.on('toolbar-refresh', this.toolbarRefreshFun, this); 
} 
 
public toolbarRefreshFun(): void { 
        if(this.isApplyEdit) { 
            // edit next cell 
            this.isApplyEdit = false; 
            this.grid.editModule.editCell(this.editRowIndex, 'maxValue'); // edit cell programmatically 
            this.grid.editModule.saveCell();  // save cell programmatically 
        } 
    } 



Please find the sample and documentation for your reference. 


Documentation : 



screenshot: 

compare with other cell  value 

 

Automatically focus to next cell 

 
Note :  Grid batch editing working like Excel editing behavior. We can edit a single cell at a time. So above provide the sample ‘min Quentity’ column enter the values without any error we can edit the next cell and apply its validation programmatically. 
 

Regards, 
J Mohammed Farook 


Loader.
Up arrow icon