Search help is not giving exact results in UI5 - sapui5

I have written code for search fields in ui5 controllers files, But the search field is not giving expected output when I pasted particular column value in input search field. Instead if I type the value in search field it is giving exact output.
I have used livechange event. I also tried with search event but unable to get the output.
I am using odata services.
Below is the controller file code:
onSearch: function(oEvt) {
// add filter for search
var aFilters = [];
var sQuery = oEvt.getSource().getValue();
if (sQuery && sQuery.length > 0) {
var filter = new Filter("UNIQUE_ID", sap.ui.model.FilterOperator.EQ, sQuery);
aFilters.push(filter);
}
// update list binding
var list = this.byId("orderTable");
var binding = list.getBinding("items");
binding.filter(aFilters);
}
View code:
Label class="sapUiMediumMarginTop" text="Unique Id"
SearchField width="150px" class="sapUiSmallMargin" liveChange="onSearch"
Please suggest me if I am missing anything.

Maybe this example can help you out? I tested your code with some random data and it worked for me.
onSearch: function(oEvent) {
var sQuery = oEvent.getParameter("query");
var oFilter1 = new Filter("Name", FilterOperator.Contains, sQuery);
var oFilter2 = new Filter("Age", FilterOperator.Contains, sQuery);
var arrFilter = new Filter([oFilter1, oFilter2], false);
// filter binding
var oList = this.byId("idList");
var oBinding = oList.getBinding("items");
oBinding.filter(arrFilter);
},

Related

AND Filter SAP UI5

AND Filter is not working.
Filter is applied on both selected date and selected value from Combo box.
var oVal=this.getView().byId("idUser").getValue();
var dDateStart = oEvent.getSource().getProperty('dateValue');
var dDateEnd = new Date(dDateStart + 1);
var isValidDate = oEvent.getParameter("valid");
var aFilters = [];
dDateStart.setMilliseconds(0);
dDateStart.setSeconds(0);
dDateStart.setMinutes(0);
dDateStart.setHours(0);
dDateEnd.setMilliseconds(0);
dDateEnd.setSeconds(59);
dDateEnd.setMinutes(59);
dDateEnd.setHours(23);
aFilters=[new sap.ui.model.Filter("CreatedBy",sap.ui.model.FilterOperator.EQ, oVal),
new sap.ui.model.Filter("CrDate", sap.ui.model.FilterOperator.BT, dDateStart, dDateEnd)];
var oFilter = new sap.ui.model.Filter({
filters: aFilters,
and: true });
this.getView().byId("idCoTable").getBinding("items").filter(oFilter,true);
It is resolved.
Issue is with filtering with value instead of Key.
Thanks

sapui5 how can i get selected item from value help into table cell?

I have a table.a column consists of input fields. How can I get value from searchHelpDialog to table item?
Below is a sample table:
And the value help dialog:
You can add the id of the field as custom data to your value help dialog.
You can get the id from the oEvent.
openValueHelpDialog: function(oEvent) {
var oDialog = sap.ui.xmlfragment();
var oField = new sap.ui.core.CustomData();
oField.setKey("field");
oField.setValue(oEvent.getParameter("id"));
oDialog.addCustomData(oField);
oDialog.open();
}`
In the handleConfirm function you can then set value into the field with the id from the custom data:
handleConfirm: function(oEvent) {
var sFieldId = oEvent.getSource().data("field");
var oField = this.getView().byId(sFieldId);
var sSelectedValue = oEvent.getParameter("selectedItem");
oField.setValue(sSelectedValue);
}
you could use a datamodel, in which the value is written when selected.
this would need to happen in the controller of the fragment you use to build the searchHelpDialog.
also the model needs to be generated, since the rows of your list probably aren't static.
Thanks friends.
Problem solved.Code;
_handleValueHelp: function(oEvent) {
this.selectedValueHelp = oEvent.getSource();
}
_handleValueHelpClose: function(oEvent) {
var oSelectedItem = oEvent.getParameter("selectedItem");
if (oSelectedItem) {
this.selectedValueHelp.setValue(oSelectedItem.getTitle());
var productInput = this.getView().byId("helpvalue");
productInput.setValue(oSelectedItem.getTitle());
}
}

How can we pass multiple filters to OData read

I have come across with situation where more than 10 input values has to be passed to back end as filters. Is there any other option to create and pass filters instead of creating it in controller for each input filed?
If you want to have more sophisticated way of controlling the filter stuff declaratively via the XML view, for example, you can go with the following approach:
Assign to all the affected inputs a "customData" property, defining the "filterProperty" and "filterOperation"
Assign to all affected inputs the same "fieldGroupId"
On the filter button trigger, grab all the input via the "getControlsByFieldGroupId" method and construct the filters using the custom data form each input via the "data" function call
for using custom data include the namespace to the view:
xmlns:data="http://schemas.sap.com/sapui5/extension/sap.ui.core.CustomData/1"
customData doc
in XML:
<Input fieldGroupIds="InputGroup" data:filterName="Id" data:filterOperation="EQ" />
<Input fieldGroupIds="InputGroup" data:filterName="Name" data:filterOperation="Constains" />
<Input fieldGroupIds="InputGroup" data:filterName="Color" data:filterOperation="LT" />
in JS:
var aInputs = oView.getControlsByFieldGroupId("InputGroup");
var aFilters = aInputs.map(function(oInput) {
var sFilterName = oInput.data("filterName");
var sFilterOperation = oInput.data("filterOperation");
return new Filter(sFilterName, sFilterOperation, oInput.getValue());
});
See example
You can create filter for every input and push it to the array of filters which you will use as you need.
E.g.:
var oFilter1 = new sap.ui.model.Filter("Input1", sap.ui.model.FilterOperator.Contains, value1);
var oFilter2 = new sap.ui.model.Filter("Input2", sap.ui.model.FilterOperator.Contains, value2);
var oFilter3 = new sap.ui.model.Filter("Input3", sap.ui.model.FilterOperator.Contains, value3);
var oFilter4 = new sap.ui.model.Filter("Input4", sap.ui.model.FilterOperator.Contains, value4);
var allFilters = new sap.ui.model.Filter([oFilter1, oFilter2, oFilter3, oFilter4], false);
For more info look here.
//Create an array
var aFilters = [];
if (yourVal1) {
aFilters.push(new sap.ui.model.Filter("Dimension", sap.ui.model.FilterOperator.EQ, yourVal1));
}
if (yourVal2) {
aFilters.push(new sap.ui.model.Filter("Language", sap.ui.model.FilterOperator.EQ, yourVal1));
}
this.getModel().read("/YourEntitySet", {
filters: aFilters,
success: function(oData) {
// use the response as required.
},
error: function() {
}
});

Filtering with more than one parameter in sapui5

I am working on an odata fiori app & I want to filter my list depending on two parameters and not just one : "Mpobj" & "Atinn", so i tried this code :
_onObjectMatched: function(oEvent) {
var sObjectPath = oEvent.getParameter("arguments").infosId;
var sObjectPath1 = oEvent.getParameter("arguments").objectI;
var oView = this.getView();
oView.bindElement(sObjectPath, sObjectPath1);
var frag = this.getView().byId("tableid1");
var oFilter = new sap.ui.model.Filter("Mpobj", sap.ui.model.FilterOperator.Contains, sObjectPath);
var oFilter1 = new sap.ui.model.Filter("Atinn", sap.ui.model.FilterOperator.Contains, sObjectPath1);
frag.bindElement("Infos" + "/mesureSet").getBinding("items").filter([oFilter], [oFilter1]);
},
But when i debugg, it appears that he's reading the first parameter "Mpobj" but doesn't recognize the second one "Atinn".
Any suggestions ?
You should use just one array. Try the following
var aFilters=[];
var oFilter = new sap.ui.model.Filter("Mpobj", sap.ui.model.FilterOperator.Contains, sObjectPath);
aFilters.push(oFilter);
var oFilter1 = new sap.ui.model.Filter("Atinn", sap.ui.model.FilterOperator.Contains, sObjectPath1);
aFilters.push(oFilter1);
frag.bindElement("Infos" + "/mesureSet").getBinding("items").filter(aFilters);

How can I delete a row from a Table in SAPUI5 Application when I used Model as XMLModel?

I have created SAPUI5 application, in that I have loaded data from external .xml file into a table, it was fine. Now, I am trying to delete a specific row from that table.
For this purpose, I use this code:
var oModel = new sap.ui.model.xml.XMLModel();
oModel.loadData("Deployments.xml", "", false);
sap.ui.getCore().setModel(oModel);
oTable.bindRows("/service"); // here "service" is the root element of xml file
var oTable = new sap.ui.commons.Button({
text: "Delete Service",
press: function() {
var idx = oTable.getSelectedIndex();
if (idx !== -1) {
var m = oTable.getModel();
var data = m.getData();
var removed = data.splice(idx, 1); // error showing at this line
m.setData(data);
sap.m.MessageToast.show(JSON.stringify(removed[0]) + 'is removed');
} else {
sap.m.MessageToast.show('Please select a row');
}
}
});
But, I am getting error at the line: var removed = data.splice(idx, 1);. However, the same code is good for when model is JSON. How can I delete a specific row from a table when model XMLModel?
It is a lot easier an more reliable to use a Bindings BindingPath to manipulate data belonging to a particular binding. Here is your adapted sample for a XMLModel:
press: function() {
var iIdx = oTable.getSelectedIndex();
var sPath = oTable.getContextByIndex(iIdx).getPath();
var oObj = oTable.getModel().getObject(sPath);
oObj.remove();
oTable.getModel().refresh();
}
This way you save the hazzle of dealing with the XML structure and furthermore this will scale with any change in the binding path you might introduce in the future.
BR
Chris
var data = m.getData();
data is not an Array. It is a XML document.
To remove an entry from the document:
var root = data.childNodes[0];
var aEntry = root.getElementsByTagName("entry");
root.removeChild(aEntry[idx]);