Skip to Content
0

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

Jan 10 at 09:49 AM

72

avatar image

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

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

2 Answers

Best Answer
Iftah Peretz
Jan 10 at 09:35 PM
0

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...).

Show 2 Share
10 |10000 characters needed characters left characters exceeded

Uncaught TypeError: Cannot read property '0' of null error im getting for maxVal = myTableArray[0].Sales

0

How is your JSON structured? Send here the first lines of it, before it becomes repetative...

0
Sharath M G Jan 10 at 11:21 AM
1

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?

Show 2 Share
10 |10000 characters needed characters left characters exceeded

I want to show it somewhere

0

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

1