Web前端之路WEB前端程序开发

重温AngularJS(七)-- Select(选择框)、表格

2017-02-20  本文已影响364人  Zyao89

关联目录索引:
重温AngularJS(一)-- 表达式、指令
重温AngularJS(二)-- 模型ng-model指令
重温AngularJS(三)-- Scope(作用域)
重温AngularJS(四)-- 控制器ng-controller
重温AngularJS(五)-- 过滤器
重温AngularJS(六)-- 服务Service
重温AngularJS(七)-- Select(选择框)、表格
重温AngularJS(八)-- 事件
重温AngularJS(九)-- 模块、全局API
重温AngularJS(十)-- 表单、输入验证
重温AngularJS(十一)-- 包含、动画
重温AngularJS(十二)-- 依赖注入(5个核心组件)
重温AngularJS(十三)-- 路由

作者:Zyao89;转载请保留此行,谢谢;


AngularJS 可以使用数组或对象创建一个下拉列表选项。

使用 ng-options 创建选择框

在 AngularJS 中我们可以使用 ng-option 指令来创建一个下拉列表,列表项通过对象和数组循环输出,如下实例:

<div ng-app="myApp" ng-controller="myCtrl">
 
<select ng-init="selectedName = names[0]" ng-model="selectedName" ng-options="x for x in names">
</select>
 
</div>
 
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.names = ["Taobao", "Runoob", "Google"];
});
</script>

ng-init 设置默认选中值。

运行结果

ng-options 与 ng-repeat

我们也可以使用ng-repeat指令来创建下拉列表:

<select>
<option ng-repeat="x in names">{{x}}</option>
</select>

ng-repeat 指令是通过数组来循环 HTML 代码来创建下拉列表,但 ng-options 指令更适合创建下拉列表,它有以下优势:
使用 ng-options 的选项是一个对象, ng-repeat 是一个字符串。

应该用哪个更好?

假设我们使用以下对象:

$scope.sites = [
    {site : "Google", url : "http://www.google.com"},
    {site : "Runoob", url : "http://www.runoob.com"},
    {site : "Taobao", url : "http://www.taobao.com"}
];

ng-repeat 有局限性,选择的值是一个字符串:

<select ng-model="selectedSite">
<option ng-repeat="x in sites" value="{{x.url}}">{{x.site}}</option>
</select>

<h1>你选择的是: {{selectedSite}}</h1>  <!-- 选择的值是一个字符串 -->

使用 ng-options 指令,选择的值是一个对象:

<select ng-model="selectedSite" ng-options="x.site for x in sites">
</select>

<h1>你选择的是: {{selectedSite.site}}</h1>
<p>网址为: {{selectedSite.url}}</p>

当选择值是一个对象时,我们就可以获取更多信息,应用也更灵活。

数据源为对象

以下我们将数据对象作为数据源。

$scope.sites = {
    site01 : "Google",
    site02 : "Runoob",
    site03 : "Taobao"
};

ng-options 使用对象有很大的不同,如下所示:

<select ng-model="selectedSite" ng-options="x for (x, y) in sites"> 
</select> <!-- 使用对象作为数据源, x 为键(key), y 为值(value) -->

<h1>你选择的值是: {{selectedSite}}</h1>

你选择的值为在 key-value 对中的 value。

★ value 在 key-value 对中也可以是个对象:

$scope.cars = {
car01 : {brand : "Ford", model : "Mustang", color : "red"},
car02 : {brand : "Fiat", model : "500", color : "white"},
car03 : {brand : "Volvo", model : "XC90", color : "black"}
};

在下拉菜单也可以不使用 key-value 对中的 key , 直接使用对象的属性:

<select ng-model="selectedCar" ng-options="y.brand for (x, y) in cars">
</select>

AngularJS 表格

ng-repeat 指令可以完美的显示表格。

在表格中显示数据

<div ng-app="myApp" ng-controller="customersCtrl"> 

<table>
  <tr ng-repeat="x in names">
    <td>{{ x.Name }}</td>
    <td>{{ x.Country }}</td>
  </tr>
</table>

</div>

<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
    $http.get("json数据链接地址")
    .success(function (response) {$scope.names = response.records;});
});
</script>

使用 CSS 样式

为了让页面更加美观,我们可以在页面中使用CSS:

<style>
table, th , td {
  border: 1px solid grey;
  border-collapse: collapse;
  padding: 5px;
}
table tr:nth-child(odd) {
  background-color: #f1f1f1; //奇数行
}
table tr:nth-child(even) {
  background-color: #ffffff; //偶数行
}
</style>

使用 orderBy 过滤器

排序显示,可以使用 orderBy 过滤器:

<table>
  <tr ng-repeat="x in names | orderBy : 'Country'">
    <td>{{ x.Name }}</td>
    <td>{{ x.Country }}</td>
  </tr>
</table>

使用 uppercase 过滤器

使用 uppercase 过滤器转换为大写:

<table>
  <tr ng-repeat="x in names">
    <td>{{ x.Name }}</td>
    <td>{{ x.Country | uppercase }}</td>
  </tr>
</table>

显示序号 ($index)

表格显示序号可以在 <td> 中添加 $index:

<table>
  <tr ng-repeat="x in names">
    <td>{{ $index + 1 }}</td>
    <td>{{ x.Name }}</td>
    <td>{{ x.Country }}</td>
  </tr>
</table>

使用 $even 和 $odd

<table>
<tr ng-repeat="x in names">  <!-- $odd奇数, $even 偶数-->
<td ng-if="$odd" style="background-color:#f1f1f1">{{ x.Name }}</td>
<td ng-if="$even">{{ x.Name }}</td>
<td ng-if="$odd" style="background-color:#f1f1f1">{{ x.Country }}</td>
<td ng-if="$even">{{ x.Country }}</td>
</tr>
</table>
上一篇 下一篇

猜你喜欢

热点阅读