How to prevent the new data duplicate?

I am using reactJS and have checked the link: 
link 1: https://www.syncfusion.com/kb/5434/how-to-prevent-the-addition-of-duplicate-records-to-the-grid
link 2: https://ej2.syncfusion.com/react/documentation/grid/edit/#custom-validation

about the link1: the is args.model undefined, so I want to know how can I get the column data? 

and the other question is I want to custom-validation duplicate column by column when add/edit the grid row. It that any idea to make the validationRules dynamic ( using one function to validate all the column duplicate and show the alert message) or I must only one by one to add the validation function?

// Can use one duplicationRules function for all column field?
<ColumnDirective field='OrderID' headerText='Order ID' validationRules={this.duplicationRules} width='100' textAlign="Right" isPrimaryKey={true}/>
<ColumnDirective field='CustomerID' defaultValue='HANAR' validationRules={this.duplicationRules} headerText='Customer ID' width='120'/>

7 Replies

BS Balaji Sekar Syncfusion Team April 2, 2020 09:54 AM UTC

Hi Customer, 
 
Query #1: The link1 - the is args.model undefined, so I want to know how can I get the column data?  

As per your query, we have achieved the link 1 requirement in the EJ2 Grid using actionBegin event. You can access the Grid added row details using args.data from the actionBegin with requestType in save state. Please refer the below code example for more information. 
[index.js] 
 actionBegin(args) {       
        if (args.requestType === 'save' && args.action == "add") { 
           var isExist = this.gridInstance.dataSource.some(row=> row.OrderID == args.data.OrderID) 
           if(isExist){ 
             args.cancel =true; 
             var editform = this.gridInstance.getContentTable().querySelector(".e-row.e-addedrow"); 
             editform.remove(); 
             this.gridInstance.refresh(); 
           } 
        } 
    } 
 
 
Query #2: I want to custom-validation duplicate column by column when add/edit the grid row 

We are able to bind the duplicate validation function in the multiple columns(column.validationRules) using custom validation function. In below code example, we have defined the “commonCustomFn” function in OrderID, CustomerName  using validationRules property since it will trigger when focus out on the edit form. We can identify the specific columns validation using args.element.name in that function. Please refer the below code example for more information 
[index.js] 
export class NormalEdit extends SampleBase { 
    constructor() { 
        super(...arguments); 
        .     .     .      . 
        this.validationRule = { required: [this.commonCustomFN.bind(this),"Please enter CustomerName"] }; 
        this.orderidRules = { required: [this.commonCustomFN.bind(this),"Please enter OrderID"]}; 
} 
commonCustomFN(args){       
      switch (args.element.name) { 
        case "OrderID": 
          if (args.value == "") { 
            return false 
          } else { return true } 
          break; 
        case "CustomerName": 
          if (args.value == "") { 
            return false 
          } else { return true } 
      } 
    } 
 
   
<GridComponent dataSource={orderDataSource} ref={grid => this.gridInstance = grid} toolbar={this.toolbarOptions} allowPaging={true} editSettings={this.editSettings} pageSettings={this.pageSettings} actionBegin={this.actionBegin.bind(this)}> 
              <ColumnsDirective> 
                <ColumnDirective field='OrderID' headerText='Order ID' width='140' textAlign='Right' validationRules={this.orderidRules} isPrimaryKey={true}></ColumnDirective> 
                <ColumnDirective field='CustomerName' headerText='Customer Name' width='150' validationRules={this.validationRule}></ColumnDirective> 
                 .     .      .      . 
              </ColumnsDirective> 
              <Inject services={[Page, Toolbar, Edit]}/> 
 </GridComponent> 
  
 
 
 
For your reference, we have created a sample with above two queries in the below sample link 
 
 
Please get back to us, if you need further assistance. 
 
Regards, 
Balaji Sekar. 



DE developer April 3, 2020 08:18 AM UTC

Thank a lot, and I want to ask another question about how to prevent the row duplicate?

for the example, the first row [Pual Henriot, Remis, France]
if add a new data is same  [Pual Henriot, Remis, France] it will show the validation tooltip message (duplicate row) <----- (add new data fail)
if the row have one column that is not duplicate e.g. [Pual Henriot, Remis, France1], it will pass the validation. <---- (add new data success)




BS Balaji Sekar Syncfusion Team April 6, 2020 01:31 PM UTC

Hi Developer, 
 
Thanks for your feedback, we have achieved the identification of row duplicate when added a new record in the Grid dataSource using actionBegin event of the Grid component. Please refer the code example and sample for more information. 
 
actionBegin(args) {       
        if (args.requestType === 'save' && args.action == "add") {           
           var isExist = this.gridInstance.dataSource.some(row=> row.OrderID == args.data.OrderID) 
           var isDuplicate = this.gridInstance.dataSource.some((data)=>{ 
             return Object.entries(data).toString() === Object.entries(args.data).toString(); 
           }) 
           if(isDuplicate){ 
             args.cancel =true; 
             var editform = this.gridInstance.getContentTable().querySelector(".e-row.e-addedrow"); 
             editform.remove(); 
             this.gridInstance.refresh(); 
           } 
        } 
    } 
 
 
 
Note:  We cannot directly compared two objects because of comparing object values is more complex. Since we achieved your query using toString concept. For your reference we have share those concepts links given below.  
 
                           https://www.samanthaming.com/tidbits/33-how-to-compare-2-objects/  
 
Regards, 
Balaji Sekar 



DE developer April 14, 2020 01:59 AM UTC

Thank you for your reply, 


About the coding :
var isDuplicate = this.gridInstance.dataSource.some((data)=>{ 
             return Object.entries(data).toString() === Object.entries(args.data).toString(); 
           }) 

will return true when I edit the row record, Is that anything solution for preventing the cell data duplicate when add/edit record?


BS Balaji Sekar Syncfusion Team April 15, 2020 02:35 AM UTC

Hi Developer, 
 
Query: Is that anything solution for preventing the cell data duplicate when add/edit record? 
 
Before proceeding further, we need to confirm that whether you want to prevent duplicate value of the edit cell alone or check other cell value of the same column. 

Regards, 
Balaji Sekar. 



DE developer April 15, 2020 02:44 AM UTC

Hi

I want to prevent the duplicate value of the edit/add row for check other cell values of the same column. 

Thank you


BS Balaji Sekar Syncfusion Team April 16, 2020 07:17 AM UTC

Hi Developer, 
 
Query: Prevent the duplicate value of the edit/add row for check other cell values of the same column.  
 
Based on your query, we can prevent save action when duplicate value is add/edit form using actionBegin event with requestType in save. Please refer the below code example and sample for more information. 
[index.js] 
actionBegin(args) {     
    if (args.requestType === 'save' && args.action == "add") { 
      var isExist = this.gridInstance.dataSource.some(row => row.OrderID == args.data.OrderID) 
      var isDuplicate = this.gridInstance.dataSource.some((data) => { 
        return Object.entries(data).toString() === Object.entries(args.data).toString(); 
      }) 
      if (isDuplicate) { 
        args.cancel = true; 
        var editform = this.gridInstance.getContentTable().querySelector(".e-row.e-addedrow"); 
        editform.remove(); 
        this.gridInstance.refresh(); 
      } 
    } 
    if (args.requestType === 'save') {       
      var columns = DataUtil.distinct(this.gridInstance.columns, "field", false); 
      var obj = {} 
      columns.forEach((col) => { 
        if (args.data[col] != args.rowData[col]) { 
          obj[col] = args.data[col] 
        } 
      }) 
      var key = Object.keys(obj); 
      var hasDuplicate = false; 
      if (key.length > 0) { 
        hasDuplicate = this.gridInstance.dataSource.some((item) => item[key] === obj[key]) 
      } 
      if (hasDuplicate) { 
        args.cancel = true; 
           var editform; 
           if(args.action == "edit") { 
            editform = this.gridInstance.getContentTable().querySelector(".e-row.e-editedrow"); 
           } else if(args.action="add"){ 
             editform =this.gridInstance.getContentTable().querySelector(".e-row.e-addedrow"); 
           } 
        editform.remove(); 
        this.gridInstance.refresh(); 
      } 
    } 
  } 
  
 

Please get back to us, if you need further assistance. 

Regards, 
Balaji Sekar 


Loader.
Up arrow icon