Skip to Content
0

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

Sep 05, 2017 at 08:52 AM

220

avatar image
Former Member

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

10 |10000 characters needed characters left characters exceeded

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.

0
Former Member
saurabh vakil

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

0
* Please Login or Register to Answer, Follow or Comment.

3 Answers

Sharath M G Sep 06, 2017 at 07:10 AM
0

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

Show 2 Share
10 |10000 characters needed characters left characters exceeded
Former Member

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.

0

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.

0
Joseph BERTHE Sep 07, 2017 at 11:17 AM
0

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

Show 2 Share
10 |10000 characters needed characters left characters exceeded
Former Member

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.

0

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);


0
Sachin Dhavanam Sep 11, 2017 at 01:29 PM
0

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.

Show 1 Share
10 |10000 characters needed characters left characters exceeded
Former Member

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

0