cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

SAP XSJS CRUD

Former Member
0 Kudos
579

I am following https://github.com/heukirne/xs-sapui-crud, The read var oModel = new sap.ui.model.json.JSONModel works but add/edit/delete doesn't work. Found out that sap.ui.commons has been deprecated and replaced by sap.m. Is this the reason menu.html is not able to call add/edit/delete .xsjs and pass on value.

Is there any way around or how to update new libarary and button so aforesaid start working.

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

There is no error but when i try to create... there is a pop up i punch in value and submit.. the html page refreshes but data doesn't get inserted, same goes for edit and delete. I have debug addData/editData/deleteData individually and it works. I added alert(oEntry.Id); so i can see if data is getting captured in html, it pops up notification showing id which i am adding, according to me menu.html is not able to call and pass on values to addData/editData/deleteData .xsjs pages respectively. Please guide/suggest/help...

menu.html

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta http-equiv='Content-Type' content='text/html;charset=UTF-8'/>


<!-- Load UI5, select gold reflection theme and the "commons" and "table" control libraries -->
  <script id='sap-ui-bootstrap' type='text/javascript'
    src='https://openui5.hana.ondemand.com/resources/sap-ui-core.js'
    data-sap-ui-theme='sap_bluecrystal'
    data-sap-ui-libs='sap.ui.commons,sap.ui.table'></script>
        <!-- only load the mobile lib "sap.m" and the "sap_bluecrystal" theme -->


        <script>
            //create the ApplicationHeader control 
		//create the ApplicationHeader control
		var oAppHeader = new sap.ui.commons.ApplicationHeader("appHeader"); 
		//configure the branding area
		oAppHeader.setLogoSrc("http://www.sap.com/global/images/SAPLogo.gif");
		oAppHeader.setLogoText("STATICGLACCOUNT: Insert, Update, Delete");
		//configure the welcome area
		oAppHeader.setDisplayWelcome(false);
		oAppHeader.setUserName("AC User");
		//configure the log off area
		oAppHeader.setDisplayLogoff(false);
            oAppHeader.placeAt("content");
            
            // setting up model
            var oModel = new sap.ui.model.json.JSONModel("./jsondata.xsjs", true);
            sap.ui.getCore().setModel(oModel);
            
            
            /*****   CREATE Operation  *****/
            function openCreateDialog(){ 
                var oCreateDialog = new sap.ui.commons.Dialog();
                oCreateDialog.setTitle("Create COPA Entry");
                var oSimpleForm = new sap.ui.layout.form.SimpleForm({
                    maxContainerCols: 2,
                    content:[
                        new sap.ui.core.Title({text:"STATICGLACCOUNT"}),
                        new sap.ui.commons.Label({text:"ID"}),
                        new sap.ui.commons.TextField({value:""}),
                        new sap.ui.commons.Label({text:"SubjectArea"}),
                        new sap.ui.commons.TextField({value:""}),
                        new sap.ui.commons.Label({text:"ColumnName"}),
                        new sap.ui.commons.TextField({value:""}),
                        new sap.ui.commons.Label({text:"ColumnValue"}),
                        new sap.ui.commons.TextField({value:""}),
                        new sap.ui.commons.Label({text:"Value1"}),
                        new sap.ui.commons.TextField({value:""}),
                    ]
                });                
                oCreateDialog.addContent(oSimpleForm);
                oCreateDialog.addButton(
                    new sap.ui.commons.Button({
                        text: "Submit", 
                         press: function() {
                            var content = oSimpleForm.getContent();
                            var oEntry = {};
                            oEntry.ID = content[2].getValue();
                            oEntry.SubjectArea = content[4].getValue();
                            oEntry.ColumnName = content[6].getValue();
                            oEntry.ColumnValue = content[8].getValue();
                            oEntry.Value1 = content[10].getValue();
                            
                            // Post data to the server
                            oModel.loadData("./addData.xsjs", oEntry, true, 'POST');
							location.reload();	
                        }
                    })
                );
                oCreateDialog.open();
            };        
            
            /*****   PUT Operation  *****/
            function openUpdateDialog(user){ 
                var oUpdateDialog = new sap.ui.commons.Dialog();
                oUpdateDialog.setTitle("Update COPA Data");
                var oSimpleForm = new sap.ui.layout.form.SimpleForm({
                    maxContainerCols: 2,
                    content:[
                        new sap.ui.core.Title({text:"STATICGLACCOUNT"}),
                        new sap.ui.commons.Label({text:"ID"}),
                        new sap.ui.commons.TextField({value: user[0].getValue(), editable: false}),
                        new sap.ui.commons.Label({text:"SubjectArea"}),
                        new sap.ui.commons.TextField({value: user[1].getValue()}),
                        new sap.ui.commons.Label({text:"ColumnName"}),
                        new sap.ui.commons.TextField({value: user[2].getValue()}),
                        new sap.ui.commons.Label({text:"ColumnValue"}),
                        new sap.ui.commons.TextField({value: user[3].getValue()}),
                        new sap.ui.commons.Label({text:"Value1"}),
                        new sap.ui.commons.TextField({value: user[4].getValue()}),
                    ]
                });                
                oUpdateDialog.addContent(oSimpleForm);
                oUpdateDialog.addButton(
                    new sap.ui.commons.Button({
                        text: "Submit", 
                        press: function() {
                            var content = oSimpleForm.getContent();
                            var oEntry = {};
                            oEntry.ID = content[2].getValue();
                            oEntry.SubjectArea = content[4].getValue();
                            oEntry.ColumnName = content[6].getValue();
                            oEntry.ColumnValue = content[8].getValue();
                            oEntry.Value1 = content[10].getValue();
                            // Post data to the server
                            oModel.loadData("./editData.xsjs", oEntry, true, 'POST');		
                            oUpdateDialog.close();
								location.reload();								   
                        }
                    })
                );
                oUpdateDialog.open();
            };
            
            /*****   DELETE Operation  *****/
            function openDeleteDialog(user) { 
                var oDeleteDialog = new sap.ui.commons.Dialog();
                oDeleteDialog.setTitle("Delete user");
                var oSimpleForm = new sap.ui.layout.form.SimpleForm({
                    maxContainerCols: 2,
                    content:[
                        new sap.ui.core.Title({text:"STATICGLACCOUNT"}),
                        new sap.ui.commons.Label({text:"ID"}),
                        new sap.ui.commons.TextField({value: user[0].getValue(), editable: false}),
                        new sap.ui.commons.Label({text:"SubjectArea"}),
                        new sap.ui.commons.TextField({value: user[1].getValue(), editable: false}),
                        new sap.ui.commons.Label({text:"ColumnName"}),
                        new sap.ui.commons.TextField({value: user[2].getValue(), editable: false}),
                        new sap.ui.commons.Label({text:"ColumnValue"}),
                        new sap.ui.commons.TextField({value: user[3].getValue(), editable: false}),
                        new sap.ui.commons.Label({text:"Value1"}),
                        new sap.ui.commons.TextField({value: user[4].getValue(), editable: false}),
                    ]
                });                
                oDeleteDialog.addContent(oSimpleForm);
                oDeleteDialog.addButton(
                    new sap.ui.commons.Button({
                        text: "Submit", 
                        press: function() {
                            var content = oSimpleForm.getContent();
                            var oEntry = {};
                            oEntry.ID = content[2].getValue();
                            oEntry.SubjectArea = content[4].getValue();
                            oEntry.ColumnName = content[6].getValue();
                            oEntry.ColumnValue = content[8].getValue();
                            oEntry.Value1 = content[10].getValue();
                            // Post data to the server
                            oModel.loadData("./deleteData.xsjs", oEntry, true, 'POST');	 							   
								oDeleteDialog.close();
								location.reload();	
                        }
                    })
                );
                oDeleteDialog.open();
            };
            // setting up table
            var oTable = new sap.ui.table.Table({
                editable: false,
                toolbar: new sap.ui.commons.Toolbar({ 
                    items: [ 
                        new sap.ui.commons.Button({
                            text: "Insert COPA", 
                            press: function() {
                                openCreateDialog();
                            }, 
                        }),
                        new sap.ui.commons.Button({
                            text: "Update COPA data", 
                            press: function() {
                                var idx = oTable.getSelectedIndex();
                                if (idx == -1) return;
                                var rows = oTable.getRows();
                                var user = rows[idx].getCells();
                                openUpdateDialog(user);
                            }, 
                        }),
                        new sap.ui.commons.Button({
                            text: "Delete COPA", 
                            press: function() {
                                var idx = oTable.getSelectedIndex();
                                if (idx == -1) return;
                                var rows = oTable.getRows();
                                var user = rows[idx].getCells();
                                openDeleteDialog(user);
                            }, 
                        })				
                    ]
                }),
            });
        
            oTable.addColumn(new sap.ui.table.Column({
                label: new sap.ui.commons.Label({text: "ID"}),
                template: new sap.ui.commons.TextField().bindProperty("value", "ID"),
                editable: false,
                sortProperty: "id"
            }));
        
            oTable.addColumn(new sap.ui.table.Column({
                label: new sap.ui.commons.Label({text: "SUBJECTAREA"}),
                template: new sap.ui.commons.TextField().bindProperty("value", "SubjectArea"),
                sortProperty: "subjectarea",
                editable: false,
            }));
        
            oTable.addColumn(new sap.ui.table.Column({
                label: new sap.ui.commons.Label({text: "ColumnName"}),
                template: new sap.ui.commons.TextField().bindProperty("value", "ColumnName"),
                sortProperty: "columnname",
                editable: false,
            }));
        
            oTable.addColumn(new sap.ui.table.Column({
                label: new sap.ui.commons.Label({text: "ColumnValue"}),
                template: new sap.ui.commons.TextField().bindProperty("value", "ColumnValue"),
                sortProperty: "columnvalue",
                editable: false,
            }));
        
            oTable.addColumn(new sap.ui.table.Column({
                label: new sap.ui.commons.Label({text: "Value1"}),
                template: new sap.ui.commons.TextField().bindProperty("value", "Value1"),
                sortProperty: "value1",
                editable: false,
            }));
            
            
            oTable.setModel(oModel);
            oTable.bindRows("/UserSet");
            oTable.placeAt("content");
        
        </script>


    </head>
    <body class="sapUiBody" role="application">
        <div id="content"></div>
    </body>
</html>

jsondata.xsjs

var select_all_staticglaccounts_query =
	"select * from \"ABC\".\"EQP::EQP1\"";




function close(closables) {
	var closable;
	var i;
	for (i = 0; i < closables.length; i++) {
		closable = closables[i];
		if (closable) {
			closable.close();
		}
	}
}


function getDataStaticGLAccounts() {


	var dataGLAccountsList = [];
    var connection = $.db.getConnection();
	var statement = null;
	var resultSet = null;
	try {
		statement = connection.prepareStatement(select_all_staticglaccounts_query);
		resultSet = statement.executeQuery();
		var dataGLAccount;


	while (resultSet.next()) {
			dataGLAccount = {};
			dataGLAccount.ID = resultSet.getString(1);
			dataGLAccount.SubjectArea = resultSet.getString(2);
			dataGLAccount.ColumnName = resultSet.getString(3);
			dataGLAccount.ColumnValue = resultSet.getString(4);
			dataGLAccount.Value1 = resultSet.getString(5);
			dataGLAccountsList.push(dataGLAccount);
		}
	} finally {
		close([resultSet, statement, connection]);
	}


	var str = JSON.stringify({
		UserSet: dataGLAccountsList
	});
	//var x1 = {
	//	UserSet: str
	//	};


	return str;
}


function doGet() {
	try {
		$.response.contentType = "application/json";
		$.response.setBody(getDataStaticGLAccounts());
	} catch (err) {
		$.response.contentType = "text/plain";
		$.response.setBody("Error while executing query: [" + err.message + "]");
		$.response.returnCode = 200;
	}
}
doGet();

addData.xsjs

var id = $.request.parameters.get('ID');
var subjectarea = $.request.parameters.get('SubjectArea');
var columnname = $.request.parameters.get('ColumnName');
var columnvalue = $.request.parameters.get('ColumnValue');
var value1 = $.request.parameters.get('Value1');
if (id === null) {
    $.response.setContentType("text/plain");
    $.response.addBody("id is null!");
}
var output = {};
output.data = [];
var conn = $.db.getConnection();
var pstmt = conn.prepareStatement('INSERT INTO  "ABC"."EDP.EQP::EQP1" values(?,?,?,?,?)');


pstmt.setString(1, id);
pstmt.setString(2, subjectarea);
pstmt.setString(3, columnname);
pstmt.setString(4, columnvalue);
pstmt.setString(5, value1);
pstmt.execute();
conn.commit();


var record = [];
record.push(id);
record.push(subjectarea);
record.push(columnname);
record.push(columnvalue);
record.push(value1);
output.data.push(record);
conn.close();


$.response.contentType = 'text/plain';
$.response.setBody('Data Inserted');
$.response.status = 200;

editData.xsjs

var subjectarea = $.request.parameters.get('SubjectArea');
var columnname = $.request.parameters.get('ColumnName');
var columnvalue = $.request.parameters.get('ColumnValue');
var value1 = $.request.parameters.get('Value1');
var id = $.request.parameters.get('ID');


var output = {};
output.data = [];
var conn = $.db.getConnection();
var pstmt = conn.prepareStatement(
'UPDATE   "ABC"."EDP.EQP::EQP1" set  "SubjectArea" =?, "ColumnName" =?, "ColumnValue" =?, "Value1" =? where "ID" =?');


pstmt.setString(1, subjectarea);
pstmt.setString(2, columnname);
pstmt.setString(3, columnvalue);
pstmt.setString(4, value1);
pstmt.setString(5, id);
pstmt.execute();
conn.commit();


var record = [];
record.push(subjectarea);
record.push(columnname);
record.push(columnvalue);
record.push(value1);
record.push(id);
output.data.push(record);
conn.close();


$.response.contentType = 'text/plain';
$.response.setBody('Data Updated');
$.response.status = 200;

deleteData.xsjs

var id = $.request.parameters.get('ID');
var conn = $.db.getConnection();
var pstmt = conn.prepareStatement('delete from "ABC"."EDP.EQP::EQP1" where "ID"=?');
pstmt.setString(1, id);
pstmt.execute();
conn.commit();
$.response.contentType = 'text/plain';
$.response.setBody('Data Deleted');
$.response.status = 200;
var rs = pstmt.executeQuery();

EQP.hdbdd

namespace EDP.EQP;
@Schema: 'ABC'
entity EQP1 {
ID :  Integer; 
SubjectArea  :  String (20) ; 
ColumnName  :  String(10);
ColumnValue  :  String(10);
Value1  :  String (20) ;
};