主视图:
<mvc:View controllerName="com.abc.controller.Master" xmlns:mvc="sap.ui.core.mvc" xmlns:core="sap.ui.core" xmlns="sap.m"
xmlns:semantic="sap.m.semantic">
<semantic:MasterPage id="page" title="{masterView>/title}" navButtonPress="onNavBack" showNavButton="true">
<semantic:subHeader>
<Bar id="headerBar">
<contentMiddle>
<SearchField id="searchField" showRefreshButton="{= !${device>/support/touch} }" tooltip="{i18n>masterSearchTooltip}" width="100%"
search="onSearch" liveChange="onSearch"></SearchField>
</contentMiddle>
</Bar>
</semantic:subHeader>
<semantic:content>
<PullToRefresh id="pullToRefresh" visible="{device>/support/touch}" refresh="onRefresh"/>
<List id="list"
items="{ path: '/MaterialDocReservationSet', sorter: { path: 'MBLNR', descending: false }, groupHeaderFactory: '.createGroupHeader' }"
busyIndicatorDelay="{masterView>/delay}" noDataText="{masterView>/noDataText}"
mode="{= ${device>/system/phone} ? 'None' : 'SingleSelectMaster'}" growing="true" growingScrollToLoad="true"
updateFinished="onUpdateFinished" selectionChange="onSelectionChange">
<infoToolbar>
<Toolbar active="true" id="filterBar" visible="{masterView>/isFilterBarVisible}" press="onOpenViewSettings">
<Title id="filterBarLabel" text="{masterView>/filterBarLabel}"/>
</Toolbar>
</infoToolbar>
<items>
<ObjectListItem id="listItem" type="{= ${device>/system/phone} ? 'Active' : 'Inactive'}" press="onSelectionChange" title="{MBLNR}"
number="{ path: 'RSNUM' }">
<secondStatus>
<ObjectStatus text="{MJAHR}" visible="true"/>
</secondStatus>
</ObjectListItem>
</items>
</List>
</semantic:content>
</semantic:MasterPage>
</mvc:View>
主控制器:
/*global history */
sap.ui.define([
"com/abc/controller/BaseController",
"sap/ui/model/json/JSONModel",
"sap/ui/core/routing/History",
"sap/ui/model/Filter",
"sap/ui/model/FilterOperator",
"sap/m/GroupHeaderListItem",
"sap/ui/Device",
"com/abc/model/formatter"
], function(BaseController, JSONModel, History, Filter, FilterOperator, GroupHeaderListItem, Device, formatter) {
"use strict";
return BaseController.extend("com.abc.controller.Master", {
formatter: formatter,
/* =========================================================== */
/* lifecycle methods */
/* =========================================================== */
/**
* Called when the master list controller is instantiated. It sets up the event handling for the master/detail communication and other lifecycle tasks.
* @public
*/
onInit: function() {
// Control state model
var oList = this.byId("list"),
oViewModel = this._createViewModel(),
iOriginalBusyDelay = oList.getBusyIndicatorDelay();
this._oList = oList;
// keeps the filter and search state
this._oListFilterState = {
aFilter: [],
aSearch: []
};
this.setModel(oViewModel, "masterView");
oList.attachEventOnce("updateFinished", function() {
// Restore original busy indicator delay for the list
oViewModel.setProperty("/delay", iOriginalBusyDelay);
});
this.getView().addEventDelegate({
onBeforeFirstShow: function() {
this.getOwnerComponent().oListSelector.setBoundMasterList(oList);
}.bind(this)
});
this.getRouter().getRoute("master").attachPatternMatched(this._onMasterMatched, this);
this.getRouter().attachBypassed(this.onBypassed, this);
},
onUpdateFinished: function(oEvent) {
// update the master list object counter after new data is loaded
this._updateListItemCount(oEvent.getParameter("total"));
// hide pull to refresh if necessary
this.byId("pullToRefresh").hide();
},
onSearch: function(oEvt) {
// add filter for search
var aFilter = [];
var sQuery = oEvt.getSource().getValue();
if (sQuery && sQuery.length > 0) {
var filter = new Filter("MBLNR", FilterOperator.Contains, sQuery);
aFilter.push(filter);
}
// update list binding
var list = this.byId("list");
var binding = list.getBinding("items");
binding.filter(aFilter, "Application");
},
onRefresh: function() {
this._oList.getBinding("items").refresh();
},
onSelectionChange: function(oEvent) {
this._showDetail(oEvent.getParameter("listItem") || oEvent.getSource());
},
/**
* Event handler for the bypassed event, which is fired when no routing pattern matched.
* If there was an object selected in the master list, that selection is removed.
* @public
*/
onBypassed: function() {
this._oList.removeSelections(true);
},
createGroupHeader: function(oGroup) {
return new GroupHeaderListItem({
title: oGroup.text,
upperCase: false
});
},
onNavBack: function() {
var sPreviousHash = History.getInstance().getPreviousHash(),
oCrossAppNavigator = sap.ushell.Container.getService("CrossApplicationNavigation");
if (sPreviousHash !== undefined || !oCrossAppNavigator.isInitialNavigation()) {
history.go(-1);
} else {
oCrossAppNavigator.toExternal({
target: {
shellHash: "#Shell-home"
}
});
}
},
/* =========================================================== */
/* begin: internal methods */
/* =========================================================== */
_createViewModel: function() {
return new JSONModel({
isFilterBarVisible: false,
filterBarLabel: "",
delay: 0,
title: this.getResourceBundle().getText("masterTitleCount", [0]),
noDataText: this.getResourceBundle().getText("masterListNoDataText"),
sortBy: "RSNUM",
groupBy: "None"
});
},
_onMasterMatched: function() {
debugger;
this.getOwnerComponent().oListSelector.oWhenListLoadingIsDone.then(
function(mParams) {
if (mParams.list.getMode() === "None") {
return;
}
var sObjectId = mParams.firstListitem.getBindingContext().getProperty("MBLNR");
var sObjectId1 = mParams.firstListitem.getBindingContext().getProperty("RSNUM");
this.getRouter().navTo("object", {
objectId: sObjectId,
objectId1: sObjectId1
}, true);
}.bind(this),
function(mParams) {
if (mParams.error) {
return;
}
this.getRouter().getTargets().display("detailNoObjectsAvailable");
}.bind(this)
);
},
_showDetail: function(oItem) {
var bReplace = !Device.system.phone;
this.getRouter().navTo("object", {
objectId: oItem.getBindingContext().getProperty("MBLNR"),
objectId1: oItem.getBindingContext().getProperty("RSNUM")
}, bReplace);
},
_updateListItemCount: function(iTotalItems) {
var sTitle;
// only update the counter if the length is final
if (this._oList.getBinding("items").isLengthFinal()) {
sTitle = this.getResourceBundle().getText("masterTitleCount", [iTotalItems]);
this.getModel("masterView").setProperty("/title", sTitle);
}
},
_applyFilterSearch: function() {
var aFilters = this._oListFilterState.aSearch.concat(this._oListFilterState.aFilter),
oViewModel = this.getModel("masterView");
this._oList.getBinding("items").filter(aFilters, "Application");
// changes the noDataText of the list in case there are no filter results
if (aFilters.length !== 0) {
oViewModel.setProperty("/noDataText", this.getResourceBundle().getText("masterListNoDataWithFilterOrSearchText"));
} else if (this._oListFilterState.aSearch.length > 0) {
// only reset the no data text to default when no new search was triggered
oViewModel.setProperty("/noDataText", this.getResourceBundle().getText("masterListNoDataText"));
}
},
_applyGroupSort: function(aSorters) {
this._oList.getBinding("items").sort(aSorters);
},
_updateFilterBar: function(sFilterBarText) {
var oViewModel = this.getModel("masterView");
oViewModel.setProperty("/isFilterBarVisible", (this._oListFilterState.aFilter.length > 0));
oViewModel.setProperty("/filterBarLabel", this.getResourceBundle().getText("masterFilterBarText", [sFilterBarText]));
}
});
});
答案 0 :(得分:-1)
希望下面的代码对您有用。
onSearch: function (oEvent) {
var aFilters = [];// add filters for search
var sQuery = oEvent.getSource().getValue();
if (sQuery && sQuery.length > 0) {
var oFilter = new sap.ui.model.Filter("MBLNR", sap.ui.model.FilterOperator.Contains, sQuery);
aFilters = new sap.ui.model.Filter([oFilter], false);
}
var oList = this.getView().byId("list");// update list binding
var oBinding = oList.getBinding("items");
oBinding.filter(aFilters, "Application");
}