cancel
Showing results for 
Search instead for 
Did you mean: 

How Can I delete an input field after I leave the View?

0 Kudos

Hi experts...

I have 2 input fields in my sapui5 app, and I want to validate them.

I succesfully did it, but now the text that I write inside the input fields remains even when Im get out of my view...

How can I do it to delete the data from the input fields before I leave the xml view?

I have my codereference from here:

https://sapui5.hana.ondemand.com/#/sample/sap.m.sample.InputChecked/code

Here's my code:

XMLVIEW.

<layout:PositionContainer left="136px" top="244.01171875px">
<layout:control>
<Input	
	id ="HeadInput"
	width = "743px"
	placeholder="Ingresa Encabezado..."
	valueStateText="El encabezado no debe estar vacío"
        value= "{path: '/head',
	type: 'sap.ui.model.type.String',
	constraints: 
	{
	minLength: 1,
	maxLength: 40
	}
	}"
valueLiveUpdate="true" 
/>
</layout:control>
</layout:PositionContainer>
<layout:PositionContainer left="136px" top="301.01171875px">
<layout:control>
<Input 
	id ="SubHeadInput"
	width = "743px"
	placeholder="Ingresa Sub Encabezado..."
        valueStateText="El subencabezado no debe estar vacío"
	value= "{path: '/subhead',
           	type: 'sap.ui.model.type.String',
		constraints : {									minLength: 1,
			maxLength: 40
				}
				}"
	valueLiveUpdate="true"/>
</layout:control>
</layout:PositionContainer>

JSVIEW:

 onInit : function ()  {
 	var oView = this.getView();
	oView.setModel(new JSONModel({
	head : "",
	subhead : ""
	}));

// attach handlers for validation errors
sap.ui.getCore().getMessageManager().registerObject(oView.byId("HeadInput"), true);
sap.ui.getCore().getMessageManager().registerObject(oView.byId("SubHeadInput"), true);
    },

	onContinue : function () {
			// collect input controls
			var oView = this.getView();
			var aInputs = [
				oView.byId("HeadInput"),
				oView.byId("SubHeadInput")
			];
			var bValidationError = false;


			// check that inputs are not empty
			// this does not happen during data binding as this is only triggered by changes
			jQuery.each(aInputs, function (i, oInput) {
				var oBinding = oInput.getBinding("value");
				try {
					oBinding.getType().validateValue(oInput.getValue());
				} catch (oException) {
					oInput.setValueState("Error");
					bValidationError = true;
				}
			});


			// output result
			if (!bValidationError) {
				MessageToast.show("Todos los campos validados, el aviso se creo correctamente");
				var oHistory, sPreviousHash;
				oHistory =new sap.ui.core.routing.History.getInstance();
				sPreviousHash = oHistory.getPreviousHash();
				if (sPreviousHash !== undefined) {window.history.go(-1);} 
				else { this.getRouter().navTo("tileadmin", {}, true /*no history*/);}
			} else {
				MessageBox.alert("Complete todos los campos para poder continuar");
			}
		}

Best Regards.

Accepted Solutions (1)

Accepted Solutions (1)

Sharathmg
Active Contributor
0 Kudos

Call this code, on success. This will reset the data to empty values once again. The idea is to clear the values set to the model(bound to the view).

oView.setModel(new JSONModel({head:"",
	subhead :""}));
agentry_src
Active Contributor
0 Kudos

Removing flame war content in its entirety. When there is a disagreement on potential solutions, please keep the comments from being personal attacks.

Regards, Mike (Moderator)

0 Kudos

Thanks! It helps me a lot.

junwu
Active Contributor
0 Kudos

trust me, it is quick but dirty way to do the job

0 Kudos

How can I do it cleaner?

junwu
Active Contributor
0 Kudos

see my updated answer.

junwu
Active Contributor
0 Kudos

like I said before, you just want to clean your plate(model), no need to go to market buy a new plate(new model) to have the clean plate and throw the old one.

Answers (1)

Answers (1)

junwu
Active Contributor
0 Kudos

get your model and set the property(which bound to the input) value to ""

oView.getModel().setData({})

or

oView.getModel().setProperty("/head","");

0 Kudos

Thanks for your answer.