1
+ sap . ui . define ( [
2
+ "com/mrb/UI5-Navigation-and-Routing/controller/BaseController" ,
3
+ "sap/ui/model/Filter" ,
4
+ "sap/ui/model/FilterOperator" ,
5
+ "sap/ui/model/Sorter" ,
6
+ "sap/m/ViewSettingsDialog" ,
7
+ "sap/m/ViewSettingsItem"
8
+ ] , function (
9
+ BaseController ,
10
+ Filter ,
11
+ FilterOperator ,
12
+ Sorter ,
13
+ ViewSettingsDialog ,
14
+ ViewSettingsItem
15
+ ) {
16
+ "use strict" ;
17
+
18
+ return BaseController . extend ( "com.mrb.UI5-Navigation-and-Routing.controller.employee.overview.EmployeeOverviewContent" , {
19
+
20
+ onInit : function ( ) {
21
+ this . _oTable = this . byId ( "employeesTable" ) ;
22
+ this . _oVSD = null ;
23
+ this . _sSortField = null ;
24
+ this . _bSortDescending = false ;
25
+ this . _aValidSortFields = [ "EmployeeID" , "FirstName" , "LastName" ] ;
26
+ this . _sSearchQuery = null ;
27
+
28
+ this . _initViewSettingsDialog ( ) ;
29
+ } ,
30
+
31
+ onSortButtonPressed : function ( ) {
32
+ this . _oVSD . open ( ) ;
33
+ } ,
34
+
35
+ onSearchEmployeesTable : function ( oEvent ) {
36
+ this . _applySearchFilter ( oEvent . getSource ( ) . getValue ( ) ) ;
37
+ } ,
38
+
39
+ _initViewSettingsDialog : function ( ) {
40
+ this . _oVSD = new ViewSettingsDialog ( "vsd" , {
41
+ confirm : function ( oEvent ) {
42
+ var oSortItem = oEvent . getParameter ( "sortItem" ) ;
43
+ this . _applySorter ( oSortItem . getKey ( ) , oEvent . getParameter ( "sortDescending" ) ) ;
44
+ } . bind ( this )
45
+ } ) ;
46
+
47
+ // init sorting (with simple sorters as custom data for all fields)
48
+ this . _oVSD . addSortItem ( new ViewSettingsItem ( {
49
+ key : "EmployeeID" ,
50
+ text : "Employee ID" ,
51
+ selected : true // by default the MockData is sorted by EmployeeID
52
+ } ) ) ;
53
+
54
+ this . _oVSD . addSortItem ( new ViewSettingsItem ( {
55
+ key : "FirstName" ,
56
+ text : "First Name" ,
57
+ selected : false
58
+ } ) ) ;
59
+
60
+ this . _oVSD . addSortItem ( new ViewSettingsItem ( {
61
+ key : "LastName" ,
62
+ text : "Last Name" ,
63
+ selected : false
64
+ } ) ) ;
65
+ } ,
66
+
67
+ _applySearchFilter : function ( sSearchQuery ) {
68
+ var aFilters , oFilter , oBinding ;
69
+
70
+ // first check if we already have this search value
71
+ if ( this . _sSearchQuery === sSearchQuery ) {
72
+ return ;
73
+ }
74
+ this . _sSearchQuery = sSearchQuery ;
75
+ this . byId ( "searchField" ) . setValue ( sSearchQuery ) ;
76
+
77
+ // add filters for search
78
+ aFilters = [ ] ;
79
+ if ( sSearchQuery && sSearchQuery . length > 0 ) {
80
+ aFilters . push ( new Filter ( "FirstName" , FilterOperator . Contains , sSearchQuery ) ) ;
81
+ aFilters . push ( new Filter ( "LastName" , FilterOperator . Contains , sSearchQuery ) ) ;
82
+ oFilter = new Filter ( { filters : aFilters , and : false } ) ; // OR filter
83
+ } else {
84
+ oFilter = null ;
85
+ }
86
+
87
+ // update list binding
88
+ oBinding = this . _oTable . getBinding ( "items" ) ;
89
+ oBinding . filter ( oFilter , "Application" ) ;
90
+ } ,
91
+
92
+ /**
93
+ * Applies sorting on our table control.
94
+ * @param {string } sSortField the name of the field used for sorting
95
+ * @param {string } sortDescending true or false as a string or boolean value to specify a descending sorting
96
+ * @private
97
+ */
98
+ _applySorter : function ( sSortField , sortDescending ) {
99
+ var bSortDescending , oBinding , oSorter ;
100
+
101
+ // only continue if we have a valid sort field
102
+ if ( sSortField && this . _aValidSortFields . indexOf ( sSortField ) > - 1 ) {
103
+
104
+ // convert the sort order to a boolean value
105
+ if ( typeof sortDescending === "string" ) {
106
+ bSortDescending = sortDescending === "true" ;
107
+ } else if ( typeof sortDescending === "boolean" ) {
108
+ bSortDescending = sortDescending ;
109
+ } else {
110
+ bSortDescending = false ;
111
+ }
112
+
113
+ // sort only if the sorter has changed
114
+ if ( this . _sSortField && this . _sSortField === sSortField && this . _bSortDescending === bSortDescending ) {
115
+ return ;
116
+ }
117
+
118
+ this . _sSortField = sSortField ;
119
+ this . _bSortDescending = bSortDescending ;
120
+ oSorter = new Sorter ( sSortField , bSortDescending ) ;
121
+
122
+ // sync with View Settings Dialog
123
+ this . _syncViewSettingsDialogSorter ( sSortField , bSortDescending ) ;
124
+
125
+ oBinding = this . _oTable . getBinding ( "items" ) ;
126
+ oBinding . sort ( oSorter ) ;
127
+ }
128
+ } ,
129
+
130
+ _syncViewSettingsDialogSorter : function ( sSortField , bSortDescending ) {
131
+ // the possible keys are: "EmployeeID" | "FirstName" | "LastName"
132
+ // Note: no input validation is implemented here
133
+ this . _oVSD . setSelectedSortItem ( sSortField ) ;
134
+ this . _oVSD . setSortDescending ( bSortDescending ) ;
135
+ }
136
+
137
+ } ) ;
138
+
139
+ } ) ;
0 commit comments