skip to Main Content

I am writing a validation callback method for checking the duplicates in devexpress grid component
ts file

    validationCallback(e: any) {  
        let isValid = true;
        for (let i = 0; i < this.abbreviation_array.length; i++) {
          if (this.abbreviation_array[i] === e.value && e.value !== undefined  ) {
            isValid = false;
            break;
          }
    
        }
        if (isValid === true ) {
          this.abbreviation_array.push(e.value);
          this.abbrev_flag = false;
         
        }
        else {
          this.abbrev_flag = true;
        }
        
        return isValid;
    
      }

html file

<dxi-column dataField="Abbreviation">
                <dxi-validation-rule type="custom" [reevaluate]= "false" [validationCallback]="validationCallback">
                </dxi-validation-rule>
            </dxi-column>```
But while coming to first element or any other previous element it puts flag to true as it is entered in array and validation_callback is evaluated everytime the cell is entered with value.
Please suggest how to solve this

2

Answers


  1. populate the abbreviation array on initializing the grid like so

    let uniqueAbbrs = this.gridData.map((obj: any) =>  { return obj.Abbreviation; });
    uniqueAbbrs = ages.filter((v: any, i: number) => { return uniqueAbbrs.indexOf(v) == i });
    this.abbreviation_array = uniqueAbbrs;
    

    Then change the validation to just check the array!

    validationCallback(e: any) {  
        return e.value ? this.abbreviation_array.includes(e.value) : false;
    }
    
    Login or Signup to reply.
  2. To prevent the flag from being set to true when entering a value that already exists in the array, you can modify the validation callback method to exclude the current row’s value from the comparison. Here’s how you can do it:

        validationCallback(e: any) {  
        let isValid = true;
        for (let i = 0; i < this.abbreviation_array.length; i++) {
            if (this.abbreviation_array[i] === e.value && e.value !== undefined && i !== e.rowIndex) {
                isValid = false;
                break;
            }
        }
        if (isValid === true ) {
            this.abbreviation_array[e.rowIndex] = e.value; // Update value at current row index
            this.abbrev_flag = false;
        } else {
            this.abbrev_flag = true;
        }
        
        return isValid;
    }
    

    In this modified version, e.rowIndex is used to identify the current row being edited. When checking for duplicates, it skips the comparison for the current row’s value by excluding its index from the comparison. This way, the flag won’t be set to true when editing the same value in the same row.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search