Search code examples
javascriptsapui5

SAPUI5: DOM change commands interfere with each other


My List Binding:

<StandardListItem 
  title="{NewProposalTitle_DE}" 
  description="Stream: {Stream}, Time: {starttime}" 
  press="handlePress" 
>

I want to change the title's binding to title="{NewProposalTitle_EN}" from the oData model, when the user is english.

I looked into expression binding but this doesn't seem to work with dynamic data:

title="{= {language} === "de" ? {NewProposalTitle_DE} : {NewProposalTitle_EN} }

So I tried a different approach where I iterate through all items from a SAPUI5 List and want to change the (german) title attribute to the english one if the language of the user is english and add a number to the Item's counter attribute.

var Items = this.getView().byId("idList").getItems();
for (var i = 0; i < Items.length; ++i) {
  if(language === "en"){
    Items[i].setTitle(oData.results[i].Title_EN);
  }
  Items[i].setCounter(numbers[i]);
}

If the language is german, everything works like a charm and the counter is set correctly, but if the language is english, things get fishy. When the Title gets updated with the english title, the counter is set back to 0 (default). Even more strange, if the german and the english version of the title are equal (so no change needs to be done) the counter is still shown (i assume SAPUI5 is smart enough to recognize, that no change needs to be done).

I encountered the same problem, when I tried to add a Style Class for the Item:

Items[i].addStyleClass("blue");

again, as long as the title doesn't get changed it works.

I tried changing the order in every possible way without success. I also tried to setCounter and addStyleClass and omitted the setTitle command but only one affected the DOM.


Solution

  • Thank you all for your answers! I figured out a solution for my problem:

    In the controller, I check for the users language and update the property binding:

    Items[i].bindProperty("title", "NewProposalTitle_EN");
    

    It may not be the most elegant or correct way, but it works for me (at the moment).

    I hope you all have a great day!