cancel
Showing results for 
Search instead for 
Did you mean: 

best way to check if a row already exists in ui5 before insertion

former_member224398
Participant
0 Kudos

Hi Guys,

what will be the best way to check if the row is already exists in UI5 table before insetion a new record in ui5 table.

I thought i will check the record which i'm going to insert by doing oModel.Read and if the record exists then give message to user if not insert a new record.

Can you share your views or the methods?

Thanks,
Anurag

saurabh_vakil
Active Contributor
0 Kudos

You can indeed perform a read operation to the backend by passing as a parameter the key field of the record to the read function and if you get the record in the response conclude the record already exists. Else call the create function to create the record in the backend.

former_member224398
Participant
0 Kudos

right.. the same way i was thinking. i would welcome if i get any other methods to do the same.

Accepted Solutions (1)

Accepted Solutions (1)

sachindhavanam
Discoverer
0 Kudos

Hi Anurag,

My suggestion is to use model to check the duplicate which is associate with the table.

We have array.map function in javascript.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

oTableModel = this.getModel("TableModel");
oTableModel.getProperty("/Items");
var iIndex = oTableModel.map(function(img) {
	return img.id;
}).indexOf(pass_inserted_Id);

It will return true, if that model already contains same id.

Using this you can avoid duplicated in your table.

former_member224398
Participant
0 Kudos

thanks.. i will apply this solution and let you know.

Answers (2)

Answers (2)

Joseph_BERTHE
Active Contributor
0 Kudos

Hello,

You can check with the key of your entry. If your table is bound to OData model, then you can check if the entry exist with that code :

var entry = myOdataModel.getProperty("/myService(key1='value1',key2='value2')");

The entry variable will tell you if there is something or not.

Regards

former_member224398
Participant
0 Kudos

Can the value1 and value2 be taken runtime? i mean i want to pass the same values which i will get from the textfield entered by user.

Joseph_BERTHE
Active Contributor
0 Kudos

Yes of course, you can use it dynamically.

You can do something like :

var myUniqueKEy = myOdataModel.createKey("/myService", {
  key1: myVar1,
  key2: myVar2
});
var entry = myOdataModel.getProperyty(myUniqueKEy);


Sharathmg
Active Contributor
0 Kudos

For the data in a view control ex: table, we can either check for the data through the control id or through the model bound to the control.

I would suggest, checking with the model data, as based on the entries of the model the rows are created in the control.

If the model is empty, then there are no rows in the table.

Regards,

Sharath

former_member224398
Participant
0 Kudos

We have the data in the model , we just want to verify before inserting a new record the same shouldn't exists already otherwise it will create junk data. If the newly created record is already present we can give information to the user that this row already exists.

Sharathmg
Active Contributor
0 Kudos

Is there any data variable(in the row) which is unique? If yes, then get the existing data/row in the table.

Loop through it by checking for any duplicate value of the Key variable in that model. Is duplicate exists, then do not create a row.