cancel
Showing results for 
Search instead for 
Did you mean: 

How to sort and filter calculated fields in a table?

Former Member
0 Kudos

Hi all,

how can the "sortProperty" and "filterProperty" of a table column be filled, when there isnt a corresponding property in the model, but the value for the column was calculated?

Example:

new sap.ui.table.Column({

    label: "my label",

    template: new sap.ui.commons.TextView({

        text:{

            parts: [

                {path: "Model>Prop1"},

                {path: "Model>Prop2"}

            ],

            formatter : function(prop1, prop2){

                var calcValue = prop1 + prop2;

                return calcValue;

            },

        },

        textAlign: sap.ui.core.TextAlign.Right

    }),

    sortProperty: "calcValue",

    filterProperty: "calcValue"

}),

Regards

Stefan

Accepted Solutions (1)

Accepted Solutions (1)

mirco_graf4
Discoverer
0 Kudos

Hey Stefan,

I agree with you, it would be very helpful to have "calculated properties" in a model that can be accessed just like normal "physical" properties. WebDynpro for example has a similar concept with "calculated attributes" or by using supply functions for models. This feature has always been very useful to me.

A dirty solution would be to "enrich" your model just after loading it by adding new properties. But then you have to handle property-changes as well (e.g. by user-input), so this is a quite bad way.

The existing UI5-concept using "parts" and "formatter" works well when you have a UI-control, but I want to use calculated properties in plain JS-coding or in my controllers. Maybe there is a solution and I just didn't find it, if not this could be an idea for UI5 product development (so I hope SAP reads this as well ;).

Best regards

Mirco

Answers (2)

Answers (2)

fabian_krger
Participant
0 Kudos

If your model is an ODataModel, you can simply specify

sortProperty: "Prop1 add Prop2"

filterProperty: "Prop1 add Prop2" 


although if Prop1 and Prop2 are Measures from Calculation-Views and they are aggregated it is usually not possible to use them for filtering at all.

former_member182650
Contributor
0 Kudos

Hi Stefan,

You could create your custom Sorter or Filter on your table based on your calculated attribute:

In this example, comparator function must compare your model calculated values based on original column value.

Modify previous blog example with this:


function compareIntegers(value1, value2) { 

  var value1_calculated = applyTransform(value1);

  var value2_calculated = applyTransform(value2);

  if ((value1 == null || value1 == undefined || value1 == '') && 

  (value2 == null || value2 == undefined || value2 == '')) return 0; 

  if ((value1 == null || value1 == undefined || value1 == '')) return -1; 

  if ((value2 == null || value2 == undefined || value2 == '')) return 1; 

  if(parseInt(value1) < parseInt(value2)) return -1; 

  if(parseInt(value1) == parseInt(value2)) return 0; 

  if(parseInt(value1) > parseInt(value2)) return 1;             

}; 

Kind regards

Former Member
0 Kudos

Hi Angel,

thank you for your answer.

Actually I think this is working, but as Mirco said, I think there should be a smarter solution by the framework, instead of writing a complete new ColumnMenu for that.

But your solution works! 🙂

Regards

Stefan