Skip to content

Commit a06ed69

Browse files
committed
Added everything for Step 12: Make a Search Bookmarkable
1 parent ef5f4f4 commit a06ed69

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
lines changed

webapp/controller/employee/overview/EmployeeOverviewContent.controller.js

+35-12
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ sap.ui.define([
55
"sap/ui/model/Sorter",
66
"sap/m/ViewSettingsDialog",
77
"sap/m/ViewSettingsItem"
8-
], function(
8+
], function (
99
BaseController,
1010
Filter,
1111
FilterOperator,
@@ -18,25 +18,45 @@ sap.ui.define([
1818
return BaseController.extend("com.mrb.UI5-Navigation-and-Routing.controller.employee.overview.EmployeeOverviewContent", {
1919

2020
onInit: function () {
21+
22+
var oRouter = this.getRouter();
23+
2124
this._oTable = this.byId("employeesTable");
2225
this._oVSD = null;
2326
this._sSortField = null;
2427
this._bSortDescending = false;
2528
this._aValidSortFields = ["EmployeeID", "FirstName", "LastName"];
2629
this._sSearchQuery = null;
30+
this._oRouterArgs = null;
2731

2832
this._initViewSettingsDialog();
33+
34+
// make the search bookmarkable
35+
oRouter.getRoute("employeeOverview").attachMatched(this._onRouteMatched, this);
36+
},
37+
38+
_onRouteMatched: function (oEvent) {
39+
// save the current query state
40+
this._oRouterArgs = oEvent.getParameter("arguments");
41+
//make sure we either have a value or empty hand empty object
42+
this._oRouterArgs["?query"] = this._oRouterArgs["?query"] || {};
43+
// search/filter via URL hash
44+
this._applySearchFilter(this._oRouterArgs["?query"].search);
2945
},
3046

31-
onSortButtonPressed : function () {
47+
onSortButtonPressed: function () {
3248
this._oVSD.open();
3349
},
3450

35-
onSearchEmployeesTable : function (oEvent) {
36-
this._applySearchFilter( oEvent.getSource().getValue() );
51+
onSearchEmployeesTable: function (oEvent) {
52+
//this._applySearchFilter(oEvent.getSource().getValue());
53+
var oRouter = this.getRouter();
54+
// update the hash with the current search term
55+
this._oRouterArgs["?query"].search = oEvent.getSource().getValue();
56+
oRouter.navTo("employeeOverview", this._oRouterArgs, true /*no history*/);
3757
},
3858

39-
_initViewSettingsDialog : function () {
59+
_initViewSettingsDialog: function () {
4060
this._oVSD = new ViewSettingsDialog("vsd", {
4161
confirm: function (oEvent) {
4262
var oSortItem = oEvent.getParameter("sortItem");
@@ -48,7 +68,7 @@ sap.ui.define([
4868
this._oVSD.addSortItem(new ViewSettingsItem({
4969
key: "EmployeeID",
5070
text: "Employee ID",
51-
selected: true // by default the MockData is sorted by EmployeeID
71+
selected: true // by default the MockData is sorted by EmployeeID
5272
}));
5373

5474
this._oVSD.addSortItem(new ViewSettingsItem({
@@ -64,7 +84,7 @@ sap.ui.define([
6484
}));
6585
},
6686

67-
_applySearchFilter : function (sSearchQuery) {
87+
_applySearchFilter: function (sSearchQuery) {
6888
var aFilters, oFilter, oBinding;
6989

7090
// first check if we already have this search value
@@ -79,7 +99,10 @@ sap.ui.define([
7999
if (sSearchQuery && sSearchQuery.length > 0) {
80100
aFilters.push(new Filter("FirstName", FilterOperator.Contains, sSearchQuery));
81101
aFilters.push(new Filter("LastName", FilterOperator.Contains, sSearchQuery));
82-
oFilter = new Filter({ filters: aFilters, and: false }); // OR filter
102+
oFilter = new Filter({
103+
filters: aFilters,
104+
and: false
105+
}); // OR filter
83106
} else {
84107
oFilter = null;
85108
}
@@ -95,7 +118,7 @@ sap.ui.define([
95118
* @param {string} sortDescending true or false as a string or boolean value to specify a descending sorting
96119
* @private
97120
*/
98-
_applySorter : function (sSortField, sortDescending){
121+
_applySorter: function (sSortField, sortDescending) {
99122
var bSortDescending, oBinding, oSorter;
100123

101124
// only continue if we have a valid sort field
@@ -105,7 +128,7 @@ sap.ui.define([
105128
if (typeof sortDescending === "string") {
106129
bSortDescending = sortDescending === "true";
107130
} else if (typeof sortDescending === "boolean") {
108-
bSortDescending = sortDescending;
131+
bSortDescending = sortDescending;
109132
} else {
110133
bSortDescending = false;
111134
}
@@ -127,7 +150,7 @@ sap.ui.define([
127150
}
128151
},
129152

130-
_syncViewSettingsDialogSorter : function (sSortField, bSortDescending) {
153+
_syncViewSettingsDialogSorter: function (sSortField, bSortDescending) {
131154
// the possible keys are: "EmployeeID" | "FirstName" | "LastName"
132155
// Note: no input validation is implemented here
133156
this._oVSD.setSelectedSortItem(sSortField);
@@ -136,4 +159,4 @@ sap.ui.define([
136159

137160
});
138161

139-
});
162+
});

webapp/manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@
103103
"target": "employees"
104104
},
105105
{
106-
"pattern": "employees/overview",
106+
"pattern": "employees/overview:?query:",
107107
"name": "employeeOverview",
108108
"target": [
109109
"employeeOverviewTop",

0 commit comments

Comments
 (0)