Skip to Content
0

How to format a number in table? And count rows?

Nov 16, 2016 at 01:25 PM

187

avatar image

Hey, people!

I have one problem with a number format. How to format this number 00000000100 to 100?

I want that huge number of nulls don't show.

And question number two, how I can count rows? "Mandat" shows the same a number of rows..that's why I get the same Data...

unbenannt.png (34.1 kB)
10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

3 Answers

Josh Zavala Nov 16, 2016 at 08:49 PM
1

Hello Khristina,

SAP has provided us with a couple of great solutions to your questions. I'll share some code snippets from my own implementation on how I solved these issues.

First, leading 0's. As you've noticed, the SAP backend internal format for many data types contains a lot of 0's. When you use an ABAP data type in the OData service, you get those extra 0's from the backend. I've found the easiest way to handle this is with a custom "formatter function". Googling that will provide lots of helpful blogs, but here's what I've done in my own projects.

Here I'm using the MVC pattern, and my views are XML. Inside my view, I have the following code in my table definition (sap.m.Table) items aggregation:

<items>
	<ColumnListItem id="columnListItem" type="Navigation" press="onItemPress">
		<cells>
			<ObjectIdentifier title="{ parts: [ {path: 'Ebelp'} ], formatter: '.formatter.formatPOItem' }"/>

Here, <ObjectIdentifier> is the first element for my table column. I've created a binding to a custom formatter class using the PO item number as a function parameter. This formatter class resides in a separate file called "formatter.js". This new file is created in the 'models' folder of the app structure. The javascript string 'replace' function combined with the regex expression should help you to remove those pesky leading 0's.

"formatter.js"
sap.ui.define([], function() {
    "use strict";

    return {
	formatPOItem: function(sItemNumber) {
		var sItem = sItemNumber.replace(/^0+/, "");
		return sItem;
	}

        //more formatter functions as needed
});

The next thing you'll need to do for this to work is declare a reference to your formatter in the associated view's controller file. Here's an example.

sap.ui.define([
	"sap/ui/core/mvc/Controller",
	"your/namespace/model/formatter"
], function(Controller, formatter) {
	"use strict";

	return Controller.extend("you.namespace.controller.PO", {

		formatter: formatter,
//rest of your controller code

Since you've bound your model's data to elements in the XML view, any time your model changes the view is updated immediately. This is a nice feature of SAPUI5.

Next, you asked "...how can I count rows?". If you are really just looking for the number of entries in your table, this is gotten by capturing the array of bound items for that table and performing the javascript length function. For example:

var oTable = this.byId("table");
var oModel = this.getView().getModel();
var oContext = oTable.getBindingContext();
var aItems = oModel.getData(oContext.sPath + "/ItemSet"); //or whatever path leads to your 1-to-many items
var length = aItems.length;

Keep in mind there are other ways to get this value. I like this way since I usually need to do some sort of processing on those rows and now I also have a reference to their contexts, which allows me to loop through the model calling 'getObject' for whatever I need.

var oItem;
for (var i=0; i<aItems.length; i++) {
   oItem = oModel.getObject("/"+aItems[i]);
   //do something
   oItem.Menge = //access object's properties
}

However, it looks to me like your question on counting wasn't so much about needing the number of rows, but rather how to get the unique rows in your table to display properly. My guess is that the OData service from your backend is missing some 'key' references. In transaction SEGW, for each EntityType you want to bind to an aggregation you need to define the minimum number of keys necessary to produce unique rows. Without this, I believe the table control is unable to distinguish the entries even though you've gotten the correct 'number' of entries showing, and even the correct data from the backend, often just repeating the last record retrieved X count times.

Good luck,

Josh


segw.png (18.0 kB)
Show 1 Share
10 |10000 characters needed characters left characters exceeded

Thank you very much for your help:)

1
Jun Wu Nov 16, 2016 at 03:09 PM
0

why not do it at backend?

Share
10 |10000 characters needed characters left characters exceeded
Khristina Filonchik Nov 16, 2016 at 04:36 PM
0

Hm, gut question, but firstly I should try to make in frontend. Is it not possible? I suppose that I can change value format with JS function parseInt()...

Share
10 |10000 characters needed characters left characters exceeded