SapUi5 Table Multiple Filter - sapui5

I'm developing a sap ui5 application using sap.ui.table.Table.
I need to apply a filter based on multiple strings. For example, if the user input is an array like:
["Stack", "Overflow"]
I need:
Filter all table fields by "Stack";
Filter the result of point 1 by "Overflow";
the result will be all rows that have "Stack" and "Overflow", no matter the field.
Does anyone have a solution?

As per the sap.ui.model.Filter documentation, you can create a filter either based on a filter info object, or from an array of previously created filters. This allows us to do the following:
Create a filter for the first value (eg "Stack")
Create a filter for the second value (eg "Overflow")
Create a filter which contains both of these values, and use it to filter the table.
Let's have a look at some code.
// We will only display rows where ProductName contains
// "Stack" AND CustomerName equals "Overflow"
var oFilterForProductName,
oFilterForCustomerName,
aArrayWhichContainsBothPreviousFilters = [],
oFilterToSetOnTheTable;
var sValueToFilterTheProductNameOn = "Stack",
sValueToFilterTheCustomerNameOn = "Overflow";
var sKeyForProductNameInTheTableModel = "ProductName",
sKeyForCustomerNameInTheTableModel = "CustomerName";
var oTableToFilter = this.byId("myTableId");
// Step 1: create two filters
oFilterForProductName = new sap.ui.model.Filter(
sKeyForProductNameInTheTableModel,
sap.ui.model.FilterOperator.Contains,
sValueToFilterTheProductNameOn);
oFilterForCustomerName = new sap.ui.model.Filter(
sKeyForCustomerNameInTheTableModel,
sap.ui.model.FilterOperator.EQ,
sValueToFilterTheCustomerNameOn);
// Step 2: add these two filters to an array
aArrayWhichContainsBothPreviousFilters.push(oFilterForProductName);
aArrayWhichContainsBothPreviousFilters.push(oFilterForCustomerName);
// Step 3: create a filter based on the array of filters
oFilterToSetOnTheTable = new sap.ui.model.Filter({
filters: aArrayWhichContainsBothPreviousFilters,
and: true
});
oTableToFilter.getBinding("items").filter(oFilterToSetOnTheTable , sap.ui.model.FilterType.Application);
Hope this helps. Let me know if you have any questions.
Chris

Please pass that array in for loop and pass filters like,
var tableId = this.byId("oTable");
for(var i=0;i < array.length ; i++)
{
oTable.getBinding().filter(new sap.ui.model.Filter("", sap.ui.model.FilterOperator.Contains, array[0]));
}
it may be helpful for you.

Related

How to modify the available filter operators of a smart table

I have a smart table that shows data from odata service. all properties of the entity type are Edm.String.
now i can set a filter for each column of the resulting table with a lot of filter operators.
My goal is to filter the list of available filter operators depending on the selected column.
e.g.
selected colum 'A' then allow only 'equal to'.
Is that somehow possible? I would like to solve it in front end code.
I didn't find anything like that in ui5 docu...
you need to use equals FilterOperator
here is a link for FilterOperator and another example how to use filter in grid table https://sapui5.hana.ondemand.com/
Here is a quick example of setting more than one filter each with different Filter Operator
filterGlobally : function(oEvent) {
var sQuery = oEvent.getParameter("query");
this._oGlobalFilter = null;
if (sQuery) {
this._oGlobalFilter = new Filter([
new Filter("columA", FilterOperator.EQ, sQuery),
new Filter("columB", FilterOperator.Contains, sQuery)
], false);
}
var oFilter = null;
if (this._oGlobalFilter) {
oFilter = new Filter([this._oGlobalFilter], true);
}
this.byId("idTable").getBinding().filter(oFilter, "Application");

How to use database data on Event OnBeforeCreate

I want to get the last data created on my database, more specifically the field called: "Saldo Atual" and after this get the form data field "Valor" and make a sum between this fields (the Old one "Saldo Atual" and the new "Valor") before create a new record and add this "Valor" and the sum of "Saldo Atual"+"Valor".
I don't know how to get the database last record
var saldoAtual =
record.saldoAtual = saldoAtual + record.valor;
And I want to know if the second line it's correct to save the sum of "Saldo Atual"+"Valor"
Based on my comment, I think that your records will need to have a Date_Created field. Then in your onBeforeCreate event the following script should work (this is untested, so you need to do your own testing):
var query = app.models.YourModelTheSameAsYourEventModel.newQuery();
query.sorting.Date_Created._descending();
query.limit = 1;
var lastrecord = query.run();
record.saldoAtual = lastrecord.saldoAtual + record.valor; //You may need to use lastrecord[0].saldoAtual
record.Created_Date = new Date();
Try that and do some testing to see if this yields your desired output.

Reset filter parameters no working using SApUI5

After applying that filter, it adds to the request
&$filter=contains(Status, eq 'active')&$skip=0&$top=100
What I need is to be able to remove that filter
I tried to remove it with the Filter model
var oFilterModel = this.getView ().getModel("filters");
oFilterModel.setProperty ("/", {});
which if you reset the other filters of the type
aFilters.push (new Filter ("Name", FilterOperator.Contains, sName));
The problem is, that you try to manipulate the model. The filtering happens in the binding of the model. Therefore, you need to change the binding of the aggregation.
As shown here:
var oList = this.getView().byId("invoiceList");
var oBinding = oList.getBinding("items");
oBinding.filter([]);

How to Convert list into other list using entityframework

I have two model classes first one is "Ticket" and other is "ExpTicket" there is only one additional column/properties in ExpTicket and others properties are same in both lists. I extract the data from database in the form of ExpTicket list and then re-assign all column/properties to Ticket List except one additional column which does not exist in Ticket list.
But iam unable to assign data from "ExpTicket" list to "Ticket" list. If anyone can timely help I shall be very thankful. Following is the code which i need to convert From ExpTicket into "Ticket" List but failed. Please help.
var GetTicket = ticketRepository.ExpTicket(r => r.TicketId == TicketId).ToList();
List<Ticket> CTicket = GetTicket.ToList();
First you have:
var GetTicket = ticketRepository.ExpTicket(r => r.TicketId == TicketId).ToList();
Then make a query:
var CTickets = (from t in GetTicket
select new Ticket{field1=t.field1,field2=t.field2...}).ToList();
Or just readjust your model to use TPT (Table per type, Table per type code first) Good luck
var CTickets = new List<Ticket>();
var ExpTicket = ticketRepository.Where(r => r.TicketId == TicketId);
foreach (var ticket in ExpTicket)
{
CTickets.Add(new Ticket { TicketId = ticket.TicketId, ... and so on });
}
// CTickets is the new list of tickets

Composite views in couchbase

I'm new to Couchbase and am struggling to get a composite index to do what I want it to. The use-case is this:
I have a set of "Enumerations" being stored as documents
Each has a "last_updated" field which -- as you may have guessed -- stores the last time that the field was updated
I want to be able to show only those enumerations which have been updated since some given date but still sort the list by the name of the enumeration
I've created a Couchbase View like this:
function (doc, meta) {
var time_array;
if (doc.doc_type === "enum") {
if (doc.last_updated) {
time_array = doc.last_updated.split(/[- :]/);
} else {
time_array = [0,0,0,0,0,0];
}
for(var i=0; i<time_array.length; i++) { time_array[i] = parseInt(time_array[i], 10); }
time_array.unshift(meta.id);
emit(time_array, null);
}
}
I have one record that doesn't have the last_updated field set and therefore has it's time fields are all set to zero. I thought as a first test I could filter out that result and I put in the following:
startkey = ["a",2012,0,0,0,0,0]
endkey = ["Z",2014,0,0,0,0,0]
While the list is sorted by the 'id' it isn't filtering anything! Can anyone tell me what I'm doing wrong? Is there a better composite view to achieve these results?
In couchbase when you query view by startkey - endkey you're unable to filter results by 2 or more properties. Couchbase has only one index, so it will filter your results only by first param. So your query will be identical to query with:
startkey = ["a"]
endkey = ["Z"]
Here is a link to complete answer by Filipe Manana why it can't be filtered by those dates.
Here is a quote from it:
For composite keys (arrays), elements are compared from left to right and comparison finishes as soon as a element is different from the corresponding element in the other key (same as what happens when comparing strings à la memcmp() or strcmp()).
So if you want to have a view that filters by date, date array should go first in composite key.