cancel
Showing results for 
Search instead for 
Did you mean: 

How to stop when a user inserting duplicate/same values in the table ?

jakir03
Explorer
0 Kudos

Hi Team,

I'm inserting the data into a Table using Simple form. If I give the data two times, it needs to neglect it or else I need to get a alert.
Could anyone please help me.

Regards,
Jakir

Bernard
Participant

Hi Jakir. You would check for the existence of the current instance prior to inserting (assuming the data has an identifier that allows for duplicate checking). Without seeing more detail its hard to help further. (Is the table bound to a model?, ...)

jakir03
Explorer
0 Kudos

//View.xml

<mvc:View controllerName="Navigation.Navigation.controller.View1" xmlns:l="sap.ui.layout" xmlns:f="sap.ui.layout.form" xmlns:mvc="sap.ui.core.mvc" displayBlock="true" xmlns="sap.m">

<Shell id="shell">

<App id="app">

<pages>

<Page id="page" title="{i18n>title}">

<content>

<VBox class="sapUiSmallMargin">

<f:SimpleForm id="SimpleFormChange354" editable="true" layout="ResponsiveGridLayout" title="Prodcut Details" labelSpanXL="3" labelSpanL="3"

labelSpanM="3" labelSpanS="12" adjustLabelSpan="false" emptySpanXL="4" emptySpanL="4" emptySpanM="4" emptySpanS="0" columnsXL="1"

columnsL="1" columnsM="1" singleContainerFullSize="false">

<f:content>

<Label text="Product Name"/>

<Input id="oProduct" />

<Label text="Company"/>

<Input id="oCompany" />

<Label text="Price"/>

<Input id="oPrice"/>

<Button text="Add" type="Emphasized" press="onadd" />

</f:content>

</f:SimpleForm>

</VBox>

<Table items="{JSON>/Product}" inset="true">

<columns>

<Column>

<Text text="Product Name"></Text>

</Column>

<Column>

<Text text="Company"></Text>

</Column>

<Column>

<Text text="Price"></Text>

</Column>

</columns>

<items>

<ColumnListItem>

<cells>

<Text text="{JSON>Product}"/>

<Text text="{JSON>Company}"/>

<Text text="{JSON>Price}"/>

</cells>

</ColumnListItem>

</items>

</Table>

<Button text="Submit" press="onpress"/>

</content>

</Page>

</pages>

</App>

</Shell>

</mvc:View>

// controller.js------------------------------------------------------------------------------------

sap.ui.define([

"sap/ui/core/mvc/Controller"

], function (Controller) {

"use strict";

return Controller.extend("Navigation.Navigation.controller.View1", {

onInit: function () {

},

onpress: function () {

var oRouter = sap.ui.core.UIComponent.getRouterFor(this);

oRouter.navTo("View2");

},

onadd: function (oEvent) {

var oProduct = this.getView().byId("oProduct").getValue();

var oCompany = this.getView().byId("oCompany").getValue();

var oPrice = this.getView().byId("oPrice").getValue();

var JSON = this.getView().getModel("JSON");

var nObject = {

Product: oProduct,

Company: oCompany,

Price: oPrice

};

var oModelData = JSON.getProperty("/Product");

oModelData.push(nObject);

JSON.setProperty("/Product", oModelData);

}

});

});

//model/data.json --------------------------------------------------------------------------

{

"Product": [{

"Product": "Pen Drive",

"Company" : "Samsung",

"Price": 400

}, {

"Product": "Keyboard",

"Company" : "Acer",

"Price": 600

}, {

"Product": "Power Bank",

"Company" : "Asus",

"Price": 800

}]

}

//manifest.js-----------------------------------------------------------------


"JSON": {

"type": "sap.ui.model.json.JSONModel",

"uri": "model/data.json"

}

//output------------------------------

duplicate.png

@barnslet
Hi Bernard, this is my program.

Accepted Solutions (1)

Accepted Solutions (1)

sachinkalpetta
Explorer
0 Kudos

Hi jakir03 ,

You can do this in many ways based on the scenario. It's question of validation. On any business case you should have a validation at the client-side and server-side to ensure data quality.

Here in your example, the table is backed by a JSONModel, hence you can add validation in the onAdd function to see if already exists in the model.

if(isValid(nObject)){
oModelData.push(nObject);
}

Thanks,

Sachin Srambickal

jakir03
Explorer
0 Kudos

Hi @Sachin Srambickal , thanks for the information.

Answers (0)