-
Notifications
You must be signed in to change notification settings - Fork 0
/
step17.html
336 lines (296 loc) · 9.72 KB
/
step17.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
<!DOCTYPE html>
<html>
<head>
<meta http-equiv='X-UA-Compatible' content='IE=edge' />
<title>SAPUI5 Contacts Demo</title>
<script id="sap-ui-bootstrap"
type="text/javascript"
src="http://localhost/sapui5/resources/sap-ui-core.js"
data-sap-ui-theme="sap_goldreflection"
data-sap-ui-libs="sap.ui.commons,sap.ui.commons,sap.ui.table,sap.ui.ux3"
data-sap-ui-modules="sap.ui.commons.MessageBox">
</script>
<script type="text/javascript">
jQuery.sap.require("sap.ui.model.odata.datajs");
jQuery.sap.require("sap.ui.commons.MessageBox");
var c = sap.ui.commons, ui = sap.ui; //shorthand
// Gateway service parameters
var sContactsUrl = "http://nplhost.mysap.com:8042/sap/opu/odata/sap/ZTEST3_SRV/",
sServiceUrl = sContactsUrl + "Contacts",
sUser = "developer",
sPassword = "ch4ngeme";
// Contact Model
var oContactsModel = new sap.ui.model.odata.ODataModel(sContactsUrl, true, sUser, sPassword);
var actionPost = "1",
actionPut = "2",
actionSave = "";
var firstName = "",
lastname = "";
sap.ui.getCore().setModel(oContactsModel);
// Customers table
var oTable = new sap.ui.table.DataTable({
id : "contactsTable",
width : "50%",
visibleRowCount : 8,
selectionMode : sap.ui.table.SelectionMode.Single,
selectedIndex : 0,
editable : false
});
// Dialog Titles
var titleModify = "Modify Contact: ",
titleCreate = "New Contact: ";
function setDialogTitle(init){
if(init){
firstName = sap.ui.getCore().byId("firstNameTextField").getValue();
lastName = sap.ui.getCore().byId("lastNameTextField").getValue();
}
if (actionSave === actionPut){ title = titleModify;}
else { title = titleCreate;}
title += " " + firstName + " " + lastName;
oContactDialog.setTitle(title);
}
oTable.setRowHeight('36px');
oTable.setTitle("Contacts");
//Set table toolbar buttons
oTable.setToolbar(new sap.ui.commons.Toolbar({
items : [ new sap.ui.commons.Button({
text : "Modify Entry",
press : function(oEvent) {
actionSave = actionPut;
oContactDetails.bindContext(selectedRowContext);
oContactDialog.open();
setDialogTitle(true);
oButton3.setVisible(true);
}
}), new sap.ui.commons.Button({
text : "New Entry",
press : function(oEvent) {
actionSave = actionPost;
oContactDetails.unbindContext();
oContactDialog.open();
setDialogTitle(true);
oButton3.setVisible(false);
}
})
]
}));
var selectedRowContext = "";
var selectedContact = "";
oImage = new sap.ui.commons.Image().bindProperty("src", "Avatar");
oImage.setHeight("36px");
oImage.setWidth("36px");
oTable.addColumn(new sap.ui.table.Column({
label : new sap.ui.commons.Label({
text : "Avatar"
}),
template : oImage,
width : "55px",
hAlign : "Center"
}));
oTable.addColumn(new sap.ui.table.Column({
label : new sap.ui.commons.Label({ text : "First Name" }),
template : new sap.ui.commons.TextField({ value : "{FirstName}" })
}));
oTable.addColumn(new sap.ui.table.Column({
label : new sap.ui.commons.Label({ text : "Last Name" }),
template : new sap.ui.commons.TextField({ value : "{LastName}" })
}));
oTable.addColumn(new sap.ui.table.Column({
label : new sap.ui.commons.Label({ text : "Status" }),
template : new sap.ui.commons.TextField({ value : "{Status}" })
}));
oTable.bindRows("Contacts");
oTable.placeAt("table");
// Contact Details Dialog
var oContactDialog = new sap.ui.commons.Dialog({
id : "contactDialog",
height : "300px",
width : "400px",
modal : true,
showCloseButton : true
});
// Contact details dialog
var oContactDetails = new sap.ui.commons.layout.MatrixLayout();
oContactDetails.setLayoutFixed(false);
oContactDetails.createRow(new sap.ui.commons.Label({
text : "First Name",
width : "100px"
}), new sap.ui.commons.TextField("firstNameTextField", {
editable : true,
width : "200px",
value : "{FirstName}",
liveChange : function(oEvent){
var oTab = sap.ui.getCore().getControl("contactsTable");
firstName = oEvent.getParameter("liveValue");
setDialogTitle(false);
oContactsModel.setProperty("FirstName", firstName, selectedRowContext);
oTab.updateRows();
}
}));
oContactDetails.createRow(new sap.ui.commons.Label({
text : "Last Name",
width : "100px"
}), new sap.ui.commons.TextField("lastNameTextField", {
editable : true,
width : "200px",
value : "{LastName}",
liveChange : function(oEvent){
lastName = oEvent.getParameter("liveValue");
setDialogTitle(false);
}
}));
oContactDetails.createRow(new sap.ui.commons.Label({
text : "Email",
width : "100px"
}), new sap.ui.commons.TextField("emailTextField", {
editable : true,
width : "200px",
value : "{Email}"
}));
oContactDetails.createRow(new sap.ui.commons.Label({
text : "Status",
width : "100px"
}), new sap.ui.commons.TextField("statusTextField", {
editable : true,
width : "200px",
value : "{Status}"
}));
oContactDetails.createRow(new sap.ui.commons.Label({
text : "Home Phone",
width : "100px"
}), new sap.ui.commons.TextField("homePhoneTextField", {
editable : true,
width : "200px",
value : "{PhoneHome}"
}));
oContactDetails.createRow(new sap.ui.commons.Label({
text : "Office Phone",
width : "100px"
}), new sap.ui.commons.TextField("officePhoneTextField", {
editable : true,
width : "200px",
value : "{PhoneOffice}"
}));
oContactDetails.createRow(new sap.ui.commons.Label({
text : "Mobile Phone",
width : "100px"
}), new sap.ui.commons.TextField("mobilePhoneTextField", {
editable : true,
width : "200px",
value : "{PhoneMobile}"
}));
var oML = new sap.ui.commons.layout.MatrixLayout();
oML.setLayoutFixed(false);
var oRow1 = new sap.ui.commons.layout.MatrixLayoutRow();
var oRow2 = new sap.ui.commons.layout.MatrixLayoutRow();
var oCell1 = new sap.ui.commons.layout.MatrixLayoutCell();
var oCell2 = new sap.ui.commons.layout.MatrixLayoutCell();
oCell1.addContent(oContactDetails);
oRow1.addCell(oCell1);
// create a standalone toolbar
var oToolbar1 = new sap.ui.commons.Toolbar("tb1");
oToolbar1.setDesign(sap.ui.commons.ToolbarDesign.Flat);
var oButton1 = new sap.ui.commons.Button({
text : "Save",
style: sap.ui.commons.ButtonStyle.Accept,
press : actionSaveEvent
});
//create an accept button
var oButton2 = new sap.ui.commons.Button({
text : "Cancel",
//style: sap.ui.commons.ButtonStyle.Accept,
press: function() { oContactDialog.close(); }
});
//create a reject button
var oButton3 = new sap.ui.commons.Button({
id: "deleteBTN",
text : "Delete",
// style: sap.ui.commons.ButtonStyle.Reject,
press : actionDeleteEvent,
visible : false
});
oToolbar1.addItem(oButton1);
oToolbar1.addItem(oButton2);
oToolbar1.addItem(oButton3);
oRow2.addCell(oCell2);
oML.addRow(oRow1);
oML.createRow(oToolbar1);
oContactDialog.addContent(oML);
oTable.attachRowSelect(function(oEvent) {
// get the binding context of the first selected row
selectedRowContext = oEvent.getParameter("rowContext");
selectedContact = oContactsModel.getProperty("Handle", selectedRowContext);
});
function actionDeleteEvent(){ deleteEntry()}
function actionSaveEvent() {
switch (actionSave) {
case actionPost:
addEntry();
break;
case actionPut:
updateEntry();
break;
}
};
// Update contact with form fields and do a PUT
function updateEntry() {
var oContact = oContactsModel.getData(this.selectedRowContext);
oContact.FirstName = sap.ui.getCore().byId("firstNameTextField").getValue(),
oContact.LastName = sap.ui.getCore().byId("lastNameTextField").getValue(),
oContact.Email = sap.ui.getCore().byId("emailTextField").getValue(),
oContact.Status = sap.ui.getCore().byId("statusTextField").getValue();
oContact.PhoneMobile = sap.ui.getCore().byId("mobilePhoneTextField").getValue();
oContact.PhoneOffice = sap.ui.getCore().byId("officePhoneTextField").getValue();
oContact.PhoneHome = sap.ui.getCore().byId("homePhoneTextField").getValue();
oContactsModel.update(this.selectedRowContext, oContact, null, function(data) {
oContactDialog.close();
sap.ui.commons.MessageBox.show("Contact " + oContact.Handle + " updated",
sap.ui.commons.MessageBox.Icon.SUCCESS, "Contact Saved",
sap.ui.commons.MessageBox.Action.OK);
}, function(err) {
//Error Callback:
alert("Error occurred " + err.message);
})
};
// POST form fields and create new entry
function addEntry() {
var contactEntry = {
FirstName : sap.ui.getCore().byId("firstNameTextField").getValue(),
LastName : sap.ui.getCore().byId("lastNameTextField").getValue(),
Email : sap.ui.getCore().byId("emailTextField").getValue(),
Status : sap.ui.getCore().byId("statusTextField").getValue(),
PhoneMobile : sap.ui.getCore().byId("mobilePhoneTextField").getValue(),
PhoneOffice : sap.ui.getCore().byId("officePhoneTextField").getValue(),
PhoneHome : sap.ui.getCore().byId("homePhoneTextField").getValue()
};
oContactsModel.create("Contacts", contactEntry, null, function(data) {
oContactDialog.close();
sap.ui.commons.MessageBox.show("Contact " + data.Handle
+ " created", sap.ui.commons.MessageBox.Icon.SUCCESS,
"Contact Saved", sap.ui.commons.MessageBox.Action.OK);
}, function(err) {
//Error Callback:
alert("Error occurred " + err.message);
})
};
// delete selected entry
function deleteEntry(){
var oContact = oContactsModel.getData(this.selectedRowContext),
name = oContact.FirstName + " " + oContact.LastName;
oContactsModel.remove(this.selectedRowContext, null, function(data) {
oContactDialog.close();
sap.ui.commons.MessageBox.show("Contact " + name + " deleted",
sap.ui.commons.MessageBox.Icon.SUCCESS, "Contact deleted",
sap.ui.commons.MessageBox.Action.OK);
}, function(err) {
//Error Callback:
alert("Error occurred " + err.message);
})
};
</script>
</head>
<body class="sapUiBody">
<div id="table"></div>
</body>
</html>
</html>