Skip to Content
0

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

Apr 12 at 05:39 PM

120

avatar image

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.

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

2 Answers

Best Answer
Sharath M G Apr 13 at 07:45 AM
0

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 :""}));
Show 6 Share
10 |10000 characters needed characters left characters exceeded

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

Thanks! It helps me a lot.

0
Juan Arturo Diaz Aguilar

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

0

How can I do it cleaner?

0
Juan Arturo Diaz Aguilar

see my updated answer.

0
Juan Arturo Diaz Aguilar

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.

0
Jun Wu Apr 12 at 07:24 PM
0

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

oView.getModel().setData({})

or

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

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

Thanks for your answer.

0