Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
441 views
in Technique[技术] by (71.8m points)

SapUi5 Table Multiple Filter

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:

  1. Filter all table fields by "Stack";
  2. 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...