Skip to Content

Get the max value from sap.m.table in particular column

Hi,

I have created a table with multiple columns populating the data from OData .In that Sales is one of the column I want to dispaly maximum sales from the table.

oTable = this.getView().byId("table");

variItems = oTable.getItems();

Im not getting how to read one column from the sap.m.Table.

Thanks in Advance

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • Best Answer
    Jan 10 at 09:35 PM

    Hi,

    Let's assume that your entire data is in this kind of JSON

    {
    	"ProductCollection": [
    		{
    			"Sales": 956,
    			...
    		},
                   ...
    }
    
    

    And you want to get the maximum value of the column "Sales", then use this

    var myTableArray = this.getView().byId("table").getModel().getProperty('/ProductCollection'),
        maxVal = myTableArray[0].Sales; //Assuming we have data, assertion checks needed
    for (var i = 1; i < myTableArray.length; i++) {
       if (myTableArray[i].Sales > maxVal ) { maxVal = myTableArray[i].Sales; }
    }
    
    

    I'm sure you can optimize this maximum finding. If so, revert back here (also if this is not what you meant...).

    Add comment
    10|10000 characters needed characters exceeded

  • Jan 10 at 11:21 AM

    The oData linked to the table, will have a parameter sales. You might have linked this to the table.

    Since oData holds the data, its better to read the sales values from the oData parameter - sales.

    Now, if you want to find the maximum sales of all the sales values, use the script var _array =[1,3,2];Math.max.apply(Math,_array);// 3

    What do you wnat to do with the maximum sales value? Highlight it in the table or show it soemwhere else?

    Add comment
    10|10000 characters needed characters exceeded

    • Somewhere.. ? Well, then, you can look up the oData returned as JSON to perform any of the required Math operations(max, min, total etc..) on that data using the JS/jQuery functions.

      But, first confirm that you have received the data in the correct form in the local JSON variable using the Chrome debugger.

      Regards,

      Sharath