Skip to Content
0
Dec 27, 2018 at 08:53 PM

Taking input and using a button to excute a POST

165 Views

Hello I am trying to take user input for a name and once typed into the box they hit save it should POST to a table but I can't seem to get the input from the view to the controller. Please see code below. Any help will be appreciated. Thanks.

  sap.ui.jsview("ECSB1.CommClassTable.view.add", {


	/** Specifies the Controller belonging to this View. 
	 * In the case that it is not implemented, or that "null" is returned, this View does not have a Controller.
	 * @memberOf ECSB1.CommClassTable.view.add
	 */
	getControllerName: function () {
		return "ECSB1.CommClassTable.controller.add";
	},


	/** Is initially called once after the Controller has been instantiated. It is the place where the UI is constructed. 
	 * Since the Controller is given to this method, its event handlers can be attached right away. 
	 * @memberOf ECSB1.CommClassTable.view.add
	 */
	createContent: function (oController) {
	
		var name = new sap.m.InputBase({id: "commClassName", width: "150px"});
		var code = new sap.m.Input({id: "code", width: "150px"});
		var freightFactor  =  new sap.m.Input({id: "freightFactor", width: "150px"});
		
		
		//buttons 
		var saveBtn = new sap.m.Button({
					text: "Save",
					width: "150px",
					
					press: [oController.onPressSave, oController]
		});
		var backBtn = new sap.m.Button({
					text: "back",
					width: "150px",
					press: [oController.onPressBack, oController]
		});
		
		//Page
		var add = new sap.m.Page({
			title: "Add",
			content: [
			
				code,
				name,
				freightFactor, 
				saveBtn,
				backBtn
			]
		});
		
		return add;
	}


});

sap.ui.define([
	"sap/ui/core/mvc/Controller"
], function (Controller) {
	"use strict";


	return Controller.extend("ECSB1.CommClassTable.controller.add", {


		/**
		 * Called when a controller is instantiated and its View controls (if available) are already created.
		 * Can be used to modify the View before it is displayed, to bind event handlers and do other one-time initialization.
		 * @memberOf ECSB1.CommClassTable.view.add
		 */
		onInit: function() {
			var user = "manager";
			var pass = "secofr";
			var comp = "ZZZTESTING05";
			
			var jData = JSON.stringify({
				UserName: user,
				Password: pass,
				CompanyDB: comp
			});
			var loginURL = "/destinations/Goetze/Login";
			$.ajax({
				url: loginURL,
				data: jData,
				type: "POST",
				dataType: "json"
			});
			
		},
		
		onPressSave: function(){
			var saveURL = "/destinations/Goetze/ECSB1_COMMCLASS";
			var text = sap.ui.getCore().byId("commClassName");
			//var text = name.setValue();
			
			var jData = JSON.parse({
				Code: 102,
				Name: text
				//frieghtFactor: freightFactor
			});
			$.ajax({
					url: saveURL,
					//xhrFields: {withCredentials:true},
					data: jData,
					type:"POST",
					dataType:"json"
			});
		}
		
	
			
				
		


		/**
		 * Similar to onAfterRendering, but this hook is invoked before the controller's View is re-rendered
		 * (NOT before the first rendering! onInit() is used for that one!).
		 * @memberOf ECSB1.CommClassTable.view.add
		 */
		//	onBeforeRendering: function() {
		//
		//	},


		/**
		 * Called when the View has been rendered (so its HTML is part of the document). Post-rendering manipulations of the HTML could be done here.
		 * This hook is the same one that SAPUI5 controls get after being rendered.
		 * @memberOf ECSB1.CommClassTable.view.add
		 */
		//	onAfterRendering: function() {
		//
		//	},


		/**
		 * Called when the Controller is destroyed. Use this one to free resources and finalize activities.
		 * @memberOf ECSB1.CommClassTable.view.add
		 */
		//	onExit: function() {
		//
		//	}


	});


});