Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass Static Value to Formatter Parameters in XML View

Tags:

sapui5

I want to call the function getCountdown with two parameters:

  • The first (AuctionEnd) is dynamic from a model.
  • The second should be hard coded in either "Time" or "Status".

This is my code:

<ObjectStatus
  title="Time"
  text="{
    parts: [
      {path: 'AuctionEnd'},
      {path: 'Time'}
    ],
    formatter: '.formatter.getCountdown'
  }"
/>

In formatter.js, there is only the first parameter as seen in my console log:

["2016-05-20T12:00:00", undefined]

In JS, I would do it this way:

var AuctionEnd = "2016-05-20T12:00:00";
getCountdown(AuctionEnd, "Time");
like image 686
alexP Avatar asked May 19 '16 07:05

alexP


4 Answers

As of UI5 1.61 (commit), you can add static values to the binding info object. The syntax is value instead of path.

parts: [
  { path: '/modelPath' },
  { value: 123 }
],

Same as with path, you can enhance the value binding info with more settings as shown in this demo ==> https://jsbin.com/yeguhow/edit?js,output



⚡ Bug in UI5 1.79 and below

As reported on GitHub issue #2916, there is a bug which was fixed first in UI5 1.80.
Consider adding { value: 123, model: <modelName> } as a temporary solution, or removing the model name from the path (binding from a default model) to circumvent the issue.

like image 112
Boghyon Hoffmann Avatar answered Nov 13 '22 09:11

Boghyon Hoffmann


I found another possibility, maybe not completely for the usecase of this question but it was what I was looking for when I found this question.

If the "constant" or "static" text you want to set is an actual translatable text you can define it in the i18n model and then access it like this:

<ObjectStatus
  title="Time"
  text="{
    parts: [
      {path: 'AuctionEnd'},
      {path: 'i18n>/Time'}
    ],
    formatter: '.formatter.getCountdown'
  }"
/>

Maybe a more fitting usecase would be setting the title of a table column and adding an unit from a model to the end like this: "Space [m^2]". Since "Space" has to be translated anyways, you can just pull it from the model directly.

like image 45
IceRevenge Avatar answered Nov 13 '22 10:11

IceRevenge


For passing static value to formatter you can use i18n model with no translation (since if you look for property that doesn't exist in i18n.prop it returns the key-word you are looking for):

I was trying to represent 'X, , ,X' as list of check boxes:

// will format the first of values boolvalues.split(',')[index]
selected="{parts: [ 'modelName>/boolvalues', 'i18n>0'], formatter: '.formatter.checkBoxFormatter'}" 

In this case I later followed different approach when I realiesd I don't even need formatter:

selected="{= ${modelName>/boolvalues}.split(',')[0] === 'X' }
like image 1
Branislav Popadič Avatar answered Nov 13 '22 11:11

Branislav Popadič


Solution for version < 1.61:

  1. Register a model to view in init method

    onInit: function() { // JSONModel required from "sap/ui/model/json/JSONModel"
      var oModelConstants = new JSONModel(Object.freeze({ myConstant: 123 }));
      oModelConstants.setDefaultBindingMode("OneTime");
      this.getView().setModel(oModelConstants, "constants");
    }
    
  2. In XML view, assign your formatter with the following parts:

    <ObjectStatus text="{
      parts: [
        'AuctionEnd',
        'constants>/myConstant'
      ],
      formatter: '.formatter.getCountdownTime'
    }" />
    

For version 1.61 and above, see answer 53609552.

like image 1
Heinrich Avatar answered Nov 13 '22 10:11

Heinrich