2017-01-16 32 views
0

我正在創建一個Web應用程序,其中包含一個表格和一個複選框,如果選中該複選框我想要顯示錶格中有多少行數,計算我的表的總行數並在控制檯中打印angularjs

,如:

<table> 
<thead> 
    <tr> 
    <td> 
     <input type="checkbox" ng-model="checkall" ng-click="clickcheckall()"/> 
    </td> 
    <td>other td</td> 
    </tr> 
</thead> 
<tbody> 
    <tr ng-repeat="somedata in table"> 
    <td></td> 
    <td></td> 
    </tr> 
</tbody> 
</table> 

,在這裏我想打印

{{showcheckalldata}} 
在我的控制器

我有變量範圍

$scope.showcheckalldata=''; 

如果我想打印列數,我需要做什麼?

回答

3

可以只分配在陣列中元件的數量,

$scope.showcheckalldata= table.length; 

DEMO

var app = angular.module('plunker', []); 
 

 
app.controller('MainCtrl', function($scope, $http) { 
 

 
    
 
    $scope.results = [{ 
 
    "agence": "CTM", 
 
    "secteur": "Safi", 
 
    "statutImp": "operationnel" 
 
    }, 
 
    { 
 
    "agence": "SMS", 
 
    "secteur": "Safi", 
 
    "statutImp": "operationnel" 
 
    }]; 
 

 
    $scope.clickcheckall = function() { 
 
    $scope.showcheckalldata = $scope.results.length; 
 
    } 
 
});
<!DOCTYPE html> 
 
<html ng-app="plunker"> 
 

 
<head> 
 
    <meta charset="utf-8" /> 
 
    <title>AngularJS Plunker</title> 
 
    <script> 
 
    document.write('<base href="' + document.location + '" />'); 
 
    </script> 
 
    <link rel="stylesheet" href="style.css" /> 
 
    <script src="https://code.angularjs.org/1.4.7/angular.js"></script> 
 
    <script src="app.js"></script> 
 
</head> 
 

 
<body ng-controller="MainCtrl"> 
 

 

 
    <table> 
 
    <tr> 
 
    <td> 
 
     <input type="checkbox" ng-model="checkall" ng-click="clickcheckall()"/> 
 
    </td> 
 
    
 
    </tr> 
 
    <tr> 
 
     <th>Agence</th> 
 
     <th>Secteur</th> 
 
     <th>StatutImp</th> 
 
    </tr> 
 
    <tr ng-repeat="result in results"> 
 
     <td>{{result.agence}}</td> 
 
     <td>{{result.secteur}}</td> 
 
     <td>{{result.statutImp}}</td> 
 
    </tr> 
 
    </table> 
 
    <h1>Total rows are : {{showcheckalldata}}</h1> 
 
</body> 
 

 
</html>