【发布时间】:2019-07-14 14:15:36
【问题描述】:
更新:
我是 Angular 的新手。我使用的是 1.7.7 版本。
我正在寻找的是:
每当我从select 列表中选择一个特定值,比如说n(请参考代码sn-p),它应该显示一个n x n 的表格。每当用户再次更改select 列表的值时,新表应替换前一个表。请让我知道是否有任何不清楚的地方。提前致谢!
const app = angular.module('grad-table',[]);
app.controller('mainCtrl',function($scope){
$scope.dimen;
$scope.n = 10;
$scope.max = 100;
$scope.getNum = function(){
arr = [];
for(i = 2 ; i <= $scope.max; i++) {
arr.push(i);
}
//console.log(arr);
return arr;
}();
$scope.getCol = function(){
arr = [];
count = 0;
for(i = 1; i <= $scope.dimen; i++) {
j = Math.floor(i/26); // used to calculate first Alphabet in two letter column name
if(i <= 26) {
arr.push(String.fromCharCode(64+i)); // For A-Z one character
} else if(i % 26 == 0) {
arr.push(String.fromCharCode(64+j-1,64+(i-26*(j-1)))); // For last Z in two character
}
else {
arr.push(String.fromCharCode(64+j,64+(i-26*j)));
}
}
console.log(arr);
return arr;
};
$scope.getRow = function(){
arr = [];
for (i = 1; i <= $scope.dimen; i++) {
arr.push(i);
}
console.log(arr);
return arr;
};
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<!DOCTYPE html>
<html ng-app="grad-table">
<head>
<title>Page Title</title>
</head>
<body ng-controller="mainCtrl">
<h1 class="title">Angular</h1>
<form>
Select Dimensions (2-100):<br>
<select ng-model="dimen" ng-change="getCol();getRow();">
<option value="">--Select--</option>
<option ng-repeat="n in getNum" value={{n}}>
{{n}}
</option>
</select>
</form>
<h3>{{dimen}}</h3>
<table>
<tr>
<th ng-repeat="c in getCol">{{c}}</th>
</tr>
<tr>
<td ng-repeat="r in getRow">{{r}}</td>
</tr>
</table>
</body>
</html>
【问题讨论】:
标签: angularjs html-table