1

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...

0

1 Answer 1

9
<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

Sign up to request clarification or add additional context in comments.

2 Comments

That's great, thanks Nitish. That solves the second part of the question. Do you know how to get the first item to appear (and the first tab to be active) by default when the page loads? If I hard-code the 'active' class into the first tab it works but then it's not removed when I click the second tab, so I don't think that's the way to do it.
Fantastic! Thanks. The ng-init is interesting. You've taught me a lot, thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.