How to delete a CSS class from SAPUI5 aggregation items

I have a fragment containing an XML view with a side navigation menu

<SideNavigation id="sideNavigation" expanded="false">
    <item>
        <NavigationList expanded="false">
            <NavigationListItem text="Start" icon="sap-icon://employee" select="initChangeView" expanded="false">
            </NavigationListItem>
            <NavigationListItem text="On Track" icon="sap-icon://building" select="initChangeView" expanded="false">
            </NavigationListItem>
            <NavigationListItem text="Details" icon="sap-icon://employee" select="initChangeView" expanded="false">
            </NavigationListItem>
            <NavigationListItem text="Comparison" icon="sap-icon://employee" select="initChangeView" expanded="false">
            </NavigationListItem>
        </NavigationList>
    </item>

</SideNavigation>

I need to dynamically add and remove a custom CSS class when an item in the navigation is selected. My goal is to remove the "selectedNavItem" class from all NavigationListItems except the selected one, but I keep encountering a TypeError.

Cannot read property 'removeStyleClass' of undefined

The handler function in controller.js:

jQuery.sap.require("xxx.controller.NavigationBar");
[...]
initChangeView: function(oEvent){  
        setExpandedToFalse(this);
        changeView(this, oEvent.getSource());
    },
[...]

Code snippet from NavigationBar.js:

function changeView(controller, source) {
var items = source.getParent().getItems();
console.log(items);
for(i = 0; i < items.length; i++)
{
    items[i].getBindingContext().removeStyleClass("selectedNavItem");
}
source.addStyleClass("selectedNavItem");
[...]

Thank you!

Answer №1

Neither

items[i].getBindingContext().removeStyleClass("selectedNavItem");

nor

items[i].removeStyleClass("selectedNavItem");

will be effective.

The ContextBinding pertains specifically to the binding context of an object and is not related to the visual appearance of a control.

Additionally, removeStyleClass is not supported for NavigationListItem. It is not available in any of its ancestors either; if you examine its hierarchy, it follows

Element> Item> NavigationListItem
.

removeStyleClass can only be used with controls that extend the Control class.

Answer №2

When creating the NavigationListItems without using databinding, it's important to note that the items won't have a BindingContext. And even if they did have one, the BindingContext doesn't dictate the styles of the Control. To address this issue, consider implementing something along these lines:

items[i].removeStyleClass("selectedNavItem");

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Exploring the Realm of Angular Controllers and Services: Embracing Triumphs and

Currently in the process of creating a service layer for an existing web app using Angular. I am transitioning $http requests and data manipulation to custom Angular services. While I have a good understanding of Dependency Injection in services, I am enco ...

Having trouble with the isAuthenticated() function in node.js when using passport?

In the process of developing an online course application, I am encountering an issue where only authenticated users should have access to view course details and lectures. To handle user authentication, I have implemented the local strategy using passport ...

Can anyone explain why the Splice function is removing the element at index 1 instead of index 0 as I specified?

selectedItems= [5,47] if(this.selectedItems.length > 1) { this.selectedItems= this.selectedItems.splice(0,1); } I am attempting to remove the element at index 0 which is 5 but unexpectedly it deletes the element at index ...

Attempting to retrieve data from an object by utilizing an external URL, however

Upon starting the bot console, the console displays: Online with undefined/5 After 10 seconds, an error is thrown: undefined:1 [object Promise] ^ SyntaxError: Unexpected token o in JSON at position 1 This is the code snippet being used: let players clie ...

React component fails to reflect changes even after updates to the Redux state

Utilizing react, redux, and redux-saga in my current project has been an interesting experience. While attempting to fetch a list of users using Redux-saga, I noticed that the redux store was updating properly (verified through the redux dev tool), but the ...

Replacing CSS conditional statements with jQuery

My CSS code is set to hide the #global-widget-base tag and display a tab #aside-expander when the browser width is less than 1200px. @media screen and (max-width: 1200px) { aside#global-widget-base { display: none !important; } #aside- ...

A function that creates a new object with identical keys as the original input object

I am working on creating a function fn() that has the following specifications: It takes a single argument x which is an object with optional keys "a" and "b" (each field may be numeric for simplicity) The function should return a new object with the same ...

Position image in the center with two lines of text that adjust gracefully based on screen size

I am struggling to position an image alongside two lines of centered text. Currently, the image appears to the left of the text in my example, but what I really want is for the image to be centered with respect to the text and have a perfectly aligned imag ...

Trouble with webpage design persists following recent modifications

Today, I decided to make some alterations on the header.php file of my WordPress website in order to tweak the flag icons displayed at the top of the page. However, when I refreshed the page, everything looked completely chaotic. Even after undoing all th ...

The location of the new HTML window page is not functioning properly when trying to redirect to "mypage.html". Can you help troub

I am looking to open a new HTML page within the same page when a button is clicked. I have attempted to implement this code, however, it does not seem to be working. Can anyone help identify the error here? Thank you in advance. Button: <input type="s ...

Sorting a Javascript table performs effectively, however, the results may vary when iterating through all the indexes

I'm currently using a function to sort a table I have: function ReorderSupplyGP(table){ table.find('tr:not(.kn-table_summary)').sort(function (a, b) { var tda = $(a).find('td:eq(1)').text().trim(); var tdb = $(b).find(&a ...

Personalizing the service endpoint in Feathers.js: A guide

Is there a way to attach a URL to my user requests that reside in a different service? How can I customize a GET request? const { Service } = require('feathers-sequelize') exports.Users = class Users extends Service { get(id, params) { // ...

Challenges with creating a responsive layout for a Bootstrap timeline

I've been utilizing a Bootstrap-based sample timeline, and while it's functional overall, some layout elements become misaligned when resizing the window. I've made adjustments to the code so that all timeline items are positioned to the rig ...

Utilizing Shared Models Across Nested ng-Change Controllers

Within my AngularJS application, I am faced with the challenge of utilizing a value from an input/dropdown field in the second controller when a change event occurs. The structure of my code is as follows: Code snippet (HTML) :: <body ng-app="myApp" ...

Bootstrap rows are in need of additional padding

Could someone kindly explain what's incorrect with this? .container { margin-top: 10px; } <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /> <div class="panel panel-primary"> <d ...

"Enhancing data visualizations by integrating google.visualization.datatable with checkboxes and utilizing KnockOut data-bind

I am currently in the process of transitioning my regular HTML tables to Google's DataTable for fixed headers and sorting capabilities. Thanks to a helpful resource on creating advanced KnockOut binding handler for google.visualization.datatable, I wa ...

I possess 9 captivating visuals that gracefully unveil their larger counterparts upon being clicked. Curiously, this enchanting feature seems to encounter a perplexing issue within the realm of web browsing

<style type="text/javascript" src="jquery-1.11.0.min.js"></style> <style type="text/javascript" src="myCode.js"></style> </body> //jquery is within my site directory on my desktop $(document).ready(function(){ //note: $("#ar ...

Implementing the MVC pattern in the app.js file for a Node.js and Express web application

After completing several tutorials on nodejs, mongodb, and express, I have gained a solid understanding of the basics such as: The main controller file being app.js. Third party modules stored in their designated node_modules directory. Template files pl ...

Tips for adjusting the <object> video to perfectly match the width and height of its parent container

<div id="player" width='2000px' height='600px'> <object id="pl" classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6"> <param name='url' value='Video/3.mp4'/> <param name='ShowContro ...

A PHP tag containing a div element

As someone who is new to web development, I have a question that may seem silly to some. I am familiar with adding div tags inside php tags, but I am unsure if it's possible to add onclick functions or any other type of function to these div tags. He ...