Dan
Dan

Reputation: 5986

Change class on tab links when selected using AngularJS

I have some tabs which display tables of content pulled from JSON data. The tabs work and I have sorting on the table columns. All good so far. Two things I want to achieve however:

  1. I want to display the first tab panel by default when the page loads.
  2. I'd like to add a class of 'active' to the active tab link.

The code I have can be seen in this fiddle: http://jsfiddle.net/9hZx5/5/, also as follows:

HTML:

<div ng-app="myApp">
    <div class="tabs">
        <a href="" title="" class="tab selected" rel="tab1" ng:click="selected=1">Purchases</a>
        <a href="" title="" class="tab" rel="tab2" ng:click="selected=2">Products on sale</a>
        <a href="" title="" class="tab" rel="tab3" ng:click="selected=3">Last 30 days sales</a>
    </div>
    <div id="tab1" class="tabContent selected" ng-controller="PurchasesCtrl" ng:show="selected == 1">
        <h2>Purchases:</h2>
        <table cellspacing="0">
            <tr class="first">
                <th class="first">Date</th>
                <th>Description</th>
            </tr>
            <tr ng-repeat="purchase in purchases.data" ng-class-odd="'odd'" ng-class-even="'even'" ng-class="{'last':$last}">
                <td class="first">{{purchase.date}}</td>
                <td>{{purchase.text}}</td>
            </tr>
        </table>
    </div>


    <div id="tab2" class="tabContent selected" ng-controller="SaleProductsCtrl" ng:show="selected == 2">
        <h2>Sale products:</h2>                         
        <table cellspacing="0">
            <tr class="first">
                <th class="first">Date</th>
                <th>Description</th>
            </tr>
            <tr ng-repeat="saleProduct in saleProducts.data" ng-class-odd="'odd'" ng-class-even="'even'" ng-class="{'last':$last}">
                <td class="first">{{saleProduct.date}}</td>
                <td>{{saleProduct.text}}</td>
            </tr>
        </table>
    </div>


    <div id="tab3" class="tabContent selected" ng-controller="Sale30DaysCtrl" ng:show="selected == 3">
        <h2>Sale 30 days:</h2>
        <table cellspacing="0">
            <tr class="first">
                <th class="first">Date</th>
                <th>Description</th>
            </tr>
            <tr ng-repeat="sale30Day in sale30Days.data" ng-class-odd="'odd'" ng-class-even="'even'" ng-class="{'last':$last}">
                <td class="first">{{sale30Day.date}}</td>
                <td>{{sale30Day.text}}</td>
            </tr>
        </table>
    </div>
</div>

JS:

var myApp = angular.module("myApp",[]);

myApp.factory("Purchases", function(){
    var Purchases = {};
    Purchases.data = [
        {
            date: "20/05/2012",
            text: "Lorem ipsum dolor sit amet ipsum dolor"
        },
        {
            date: "20/05/2012",
            text: "Lorem ipsum dolor sit amet ipsum dolor"
        },
        {
            date: "20/05/2012",
            text: "Lorem ipsum dolor sit amet ipsum dolor"
        }
    ];
    return Purchases;
});

function PurchasesCtrl($scope, Purchases){
    $scope.purchases = Purchases;   
}




myApp.factory("SaleProducts", function(){
    var SaleProducts = {};
    SaleProducts.data = [
        {
            date: "20/05/2012",
            text: "Lorem ipsum dolor sit amet ipsum dolor"
        },
        {
            date: "20/05/2012",
            text: "Lorem ipsum dolor sit amet ipsum dolor"
        }
    ];
    return SaleProducts;
});

function SaleProductsCtrl($scope, SaleProducts){
    $scope.saleProducts = SaleProducts;   
}




myApp.factory("Sale30Days", function(){
    var Sale30Days = {};
    Sale30Days.data = [
        {
            date: "20/05/2012",
            text: "Lorem ipsum dolor sit amet ipsum dolor"
        },
        {
            date: "20/05/2012",
            text: "Lorem ipsum dolor sit amet ipsum dolor"
        },
        {
            date: "20/05/2012",
            text: "Lorem ipsum dolor sit amet ipsum dolor"
        },
        {
            date: "20/05/2012",
            text: "Lorem ipsum dolor sit amet ipsum dolor"
        }
    ];
    return Sale30Days;
});

function Sale30DaysCtrl($scope, Sale30Days){
    $scope.sale30Days = Sale30Days;   
}

I'm not sure if I need to define a controller for the tab links (tried that and it seemed to break the tabbing altogether!), or whether I've gone about this slightly wrongly in having a controller for each tab panel and I should just have one master controller for the whole section.

Any help would be really appreciated, as I'm a total newcomer to Angular and trying to find my way around.

Thanks folks...

Upvotes: 1

Views: 9922

Answers (1)

Nitish Kumar
Nitish Kumar

Reputation: 4870

<div class="tabs">
    <a href="" title="" class="tab selected" rel="tab1" ng:click="selected=1">Purchases</a>
    <a href="" title="" class="tab" rel="tab2" ng:click="selected=2">Products on sale</a>
    <a href="" title="" class="tab" rel="tab3" ng:click="selected=3">Last 30 days sales</a>
</div>

Replace To :

   <div class="tabs" ng-init="selected=1">
            <a href="" title="" class="tab selected" rel="tab1" ng:click="selected=1" ng:class="{'active' : selected==1 }">Purchases</a>
            <a href="" title="" class="tab" rel="tab2" ng:click="selected=2" ng:class="{'active' : selected==2 }">Products on sale</a>
            <a href="" title="" class="tab" rel="tab3" ng:click="selected=3" ng:class="{'active' : selected==3 }">Last 30 days sales</a>
        </div>

See DEMO Here

Upvotes: 9

Related Questions