AngularJS enables tab highlighting by responding to button selections

In my application, there are 3 tabs set up as follows:

<li ng-class="{ active: isActive('#one')}"><a data-toggle="tab" class="tab-txt-color" href="#one" ng-click="selectTab('fruits')">Fruits</a></li>
<li ng-class="{ active: isActive('#two')}"><a data-toggle="tab" class="tab-txt-color" href="#two" ng-click="selectTab('veggies')">Veggies</a></li>
<li ng-class="{ active: isActive('#three')}"><a data-toggle="tab" class="tab-txt-color" href="#three" ng-click="selectTab('decors')">Decors</a></li>

Code in .js file:

 $scope.isActive = function (viewLocation) { 
    return viewLocation === $location.path();
  };
    

When a tab is clicked, it becomes highlighted.

I have added previous and next buttons to each tab's content. How can I make the respective tab active when clicking on the previous button?

  <div>
 .......
 ......
 ....(content of Veggies tab)
 [At the end of this tab, there are 2 buttons]

 <div class="col-md-12 col-lg-12" style="margin-top:20px;" align="right">
 <a href="#"><img src="core/static/images/previous-btn.gif"  border="0" ng-click="selectTab('fruits')"></a>
 <a href="#"><img src="core/static/images/next-btn.gif"  border="0" ng-click="selectTab('decors')"></a>
</div>

Answer №1

If you require the next and previous actions to work sequentially, I have a solution for you. Simply add an array of tabs with their names and a variable to hold the current tab name:

$scope.tabs = ["fruits", "veggies", "decors"];
$scope.currentTab = "";

Additionally, include two more methods for handling the Previous and Next button clicks:

$scope.nextTab = function(){
    if($scope.currentTab == ""){
        $scope.selectTab(tabs[0]);
    }
    else {
        var index = $scope.tabs.indexOf($scope.currentTab);
        var nextIndex = ((index + 1) < $scope.tabs.length) ? (index+1) : 0;
        $scope.selectTab($scope.tabs[nextIndex]);
    }
}
$scope.prevTab = function() {
    if($scope.currentTab == ""){
        $scope.selectTab(tabs[0]);
    }
    else {
        var index = $scope.tabs.indexOf($scope.currentTab);
        var prevIndex = ((index == 0) ? ($scope.tabs.length - 1) : (index-1));
        $scope.selectTab($scope.tabs[prevIndex]);
    }
}

Include a line in the selectTab method to update the current tab name:

$scope.selectTab = function(tab){
    //other functionalities

    //add this line
    $scope.currentTab = tab;
}

Don't forget to update the click methods in the Previous and Next buttons:

<a href="#"><img src="core/static/images/previous-btn.gif" border="0" ng-click="prevTab()"></a>
<a href="#"><img src="core/static/images/next-btn.gif" border="0" ng-click="nextTab()"></a>

I hope this information proves useful to you.

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

div not recognizing the minimum height specified in percentage

I'm encountering an issue where one of my divs, content_wrap, is not adhering to the CSS property min-height:100% HTML <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ...

Internet Explorer experiencing difficulties displaying elements

On occasion, my website (find-minecraft-servers.com) may appear distorted in Internet Explorer. Sometimes, the number under Servers Listed in the green-ish banner fails to display correctly, despite being present in the source code. This issue seems to be ...

The submission directed me to the PHP webpage

Trying to integrate AJAX and jQuery into my code has been a challenge. Whenever the form is submitted, it redirects me to the PHP page in the action attribute. I'm not sure why this keeps happening, and I've been struggling to figure it out. The ...

Performing a usemin with Grunt to retrieve the ultimate directory of compressed files for preloading purposes

Within my HTML block, I have code for minifying CSS and JS files: <!-- build:css static/assets/css/combined.css --> <link rel="stylesheet" href="static/bower_components/font-awesome/css/font-awesome.css"> <link rel="stylesheet" href="static ...

What is the best way to ensure that each box remains separate and does not overlap with the box next to it?

Just an FYI - styling in React using CSS is quite similar to regular CSS. Each individual box has its own code snippet: export const CardWrapper = styled.div` display: flex; flex-direction: column; align-items: center; border-radius: 4px; backg ...

Tips for adjusting the width of ngx bootstrap modal?

Is there a way to adjust the width of my ngx bootstrap modal so that it is not fixed? I have attempted to modify it in the HTML code below but without success: Here's the HTML snippet: <div bsModal #childModal="bs-modal" class=" ...

Creating a progress bar with blank spaces using HTML, CSS, and JavaScript

Upon running the code below, we encounter an issue when the animation starts. The desired effect is to color the first ten percent of the element, then continue coloring incrementally until reaching 80%. However, as it stands now, the animation appears mes ...

Using Jquery to allow users to dynamically add <a href> links to localstorage

In the context of a work project, there is a bookmark page with internal links already set up. However, I am looking to allow users to add custom bookmarks with their own URLs dynamically and save them via local storage. This could potentially include an i ...

Copying an HTML <div> and inserting it into a database involves capturing the input text value as part of the process

How can I duplicate a content div in jQuery using the clone() method, while also incorporating the value of an input text? Is this the correct approach, or is there a better alternative? ...

Enhancing List Page Functionality with AngularJS/MVC5 Search Feature

As I work on enhancing the List page, my main focus is on implementing a search feature. While the code below effectively displays data in a list format, I am uncertain about how to start incorporating a search functionality into this page. HTML: <bo ...

How do you invoke a function from within another function in AngularJS?

I'm facing an issue with a function that needs to be called every time a page is loaded. I attempted to achieve this by using the following code: callFunction(); function callFunction(){ $scope.onClickModel(); } ...

Having trouble locating the search bar element on Walmart's website?

I'm currently working on a bot that needs Selenium to interact with the search bar on Walmart's website, where it will input the name of a specific product and perform a search. However, I've encountered an issue where no matter what method ...

Ways to incorporate the setTimeOut function

<body> <script> $(window).scroll(function() { $('#csgo').each(function(){ var imagePos = $(this).offset().top; var topOfWindow = $(window).scroll ...

Increasing box width in Django

I'm currently working on a website using Django all-auth for authentication. Everything is functioning perfectly, but I'm wondering if there's a way to increase the size of the username and password input boxes using HTML. Below is the code ...

Tips for adjusting border colors based on the current time

Trying to change the border color based on the opening hours. For example, green border from 10am to 9:29pm, yellow from 9:30pm to 9:44pm, and orange from 9:45pm until closing at 10pm. The issue is that the colors change at incorrect times beyond midnight. ...

How come jQuery is retaining the original DOM element classes even after I have modified them using jQuery?

Here is the code snippet I am working on: $(".drop-down-arrow-open i").click(function(){ console.log("The click function for .drop-down-arrow-open is triggered even when it is closed"); let thisParent = $(this).closest(".projects-container").find(".need ...

I'm having trouble getting Pycharm to locate my static files

My Pycharm is having trouble locating my CSS files. I've attached a screenshot showing the settings.py file, the directory where the .css file is located, and the error message from the terminal indicating a 404 error. Could someone please help me ide ...

Explore the world of HTML by uncovering both the tags and

I am currently focused on enhancing a table I created to allow sorting rows in both ascending and descending order, as well as the ability to rearrange columns through click-and-drag functionality. While the basic structure is working, I am seeking to refi ...

What are some methods to prevent cookies from being overridden?

Just beginning my journey in web development. Currently utilizing asp.net Web API and Angular with token authentication. Every time a user logs in, I set the token in a cookie and send it with each request. Everything has been running smoothly so far, bu ...

Updating a CSS file through jQuery is proving to be ineffective

After searching extensively on Stack Overflow and other websites, I have not been able to find a clear answer to my question regarding changing a CSS class (#navbar a) after a specific function is triggered. My understanding of jQuery and JavaScript is lim ...