AngularJS表格详解及示例代码,如何实现动态排序与分页功能?

AngularJS 作为一款经典的前端框架,其数据双向绑定和模块化特性为表格开发提供了强大支持,通过内置的 ng-repeat 指令和丰富的过滤器,开发者可以轻松实现动态数据渲染、排序、筛选等功能,本文将详细解析 AngularJS 表格的核心功能,结合示例代码展示实际应用场景。

AngularJS表格详解及示例代码,如何实现动态排序与分页功能?

基础表格渲染

AngularJS 表格的核心是 ng-repeat 指令,该指令用于遍历数组或对象数据并动态生成表格行,以下是一个基础示例:

<table>
  <thead>
    <tr>
      <th>姓名</th>
      <th>年龄</th>
      <th>职业</th>
    </tr>
  </thead>
  <tbody>
    <tr ng-repeat="user in users">
      <td>{{user.name}}</td>
      <td>{{user.age}}</td>
      <td>{{user.job}}</td>
    </tr>
  </tbody>
</table>

对应的 JavaScript 代码:

angular.module('myApp', [])
  .controller('TableController', function($scope) {
    $scope.users = [
      {name: '张三', age: 28, job: '工程师'},
      {name: '李四', age: 32, job: '设计师'},
      {name: '王五', age: 24, job: '产品经理'}
    ];
  });

此示例中,ng-repeat 遍历 users 数组,为每个用户对象生成一行表格数据。 语法用于数据绑定,确保视图与模型数据同步更新。

表格排序功能

AngularJS 提供了 orderBy 过滤器,可轻松实现表格列排序,通过在 ng-repeat 中指定排序字段和排序方向,即可实现动态排序:

<table>
  <thead>
    <tr>
      <th ng-click="sortBy('name')">姓名 
        <span ng-show sortKey === 'name' 
              ng-class="reverse ? 'desc' : 'asc'"></span>
      </th>
      <th ng-click="sortBy('age')">年龄 
        <span ng-show sortKey === 'age' 
              ng-class="reverse ? 'desc' : 'asc'"></span>
      </th>
    </tr>
  </thead>
  <tbody>
    <tr ng-repeat="user in users | orderBy:sortKey:reverse">
      <td>{{user.name}}</td>
      <td>{{user.age}}</td>
    </tr>
  </tbody>
</table>

控制器代码需添加排序逻辑:

$scope.sortKey = 'name';
$scope.reverse = false;
$scope.sortBy = function(key) {
  $scope.reverse = ($scope.sortKey === key) ? !$scope.reverse : false;
  $scope.sortKey = key;
};

点击表头时,sortBy 方法会切换排序字段和方向,orderBy 过滤器根据参数动态渲染排序结果。

AngularJS表格详解及示例代码,如何实现动态排序与分页功能?

表格筛选功能

结合 filter 过滤器,可实现基于用户输入的实时筛选,以下示例展示如何通过搜索框过滤表格数据:

<input type="text" ng-model="searchText" placeholder="搜索...">
<table>
  <tbody>
    <tr ng-repeat="user in users | filter:searchText">
      <td>{{user.name}}</td>
      <td>{{user.age}}</td>
    </tr>
  </tbody>
</table>

filter 过滤器会根据 searchText 的值实时匹配 users 数组中的对象,仅显示符合条件的数据,默认情况下,过滤器会对所有属性进行模糊匹配,可通过指定属性名实现精确筛选,如 filter:{name:searchText}

分页功能实现

对于大数据集,分页是必要的优化手段,以下实现包含分页控制器和分页按钮:

<div>
  <table>
    <tbody>
      <tr ng-repeat="user in users | limitTo:itemsPerPage:currentPage*itemsPerPage">
        <td>{{user.name}}</td>
        <td>{{user.age}}</td>
      </tr>
    </tbody>
  </table>
  <div class="pagination">
    <button ng-disabled="currentPage === 0" 
            ng-click="currentPage = currentPage - 1">上一页</button>
    <span>第 {{currentPage + 1}} 页</span>
    <button ng-disabled="currentPage >= users.length/itemsPerPage - 1" 
            ng-click="currentPage = currentPage + 1">下一页</button>
  </div>
</div>

控制器代码:

$scope.currentPage = 0;
$scope.itemsPerPage = 5;

limitTo 过滤器通过计算 currentPageitemsPerPage 实现数据分片,分页按钮通过修改 currentPage 控制显示的数据范围。

表格样式与交互

结合 CSS 框架(如 Bootstrap)可快速美化表格,AngularJS 的 ng-class 指令还能实现动态样式绑定:

AngularJS表格详解及示例代码,如何实现动态排序与分页功能?

<table class="table table-striped">
  <tr ng-repeat="user in users" 
      ng-class="{'highlight': user.age > 30}">
    <td>{{user.name}}</td>
    <td>{{user.age}}</td>
  </tr>
</table>

通过 ng-class 动态添加 highlight 类,可为年龄大于 30 的行设置特殊样式。ng-mouseenterng-click 等指令可实现行悬停效果和点击事件,增强用户交互体验。

复杂表格示例

以下综合示例展示包含排序、筛选、分页和样式的完整表格:

<div ng-app="myApp" ng-controller="TableController">
  <input type="text" ng-model="searchText" placeholder="搜索...">
  <table class="table table-bordered">
    <thead>
      <tr>
        <th ng-click="sortBy('name')">姓名 
          <span ng-show="sortKey === 'name'" 
                ng-class="reverse ? 'glyphicon glyphicon-chevron-down' : 'glyphicon glyphicon-chevron-up'"></span>
        </th>
        <th ng-click="sortBy('age')">年龄 
          <span ng-show="sortKey === 'age'" 
                ng-class="reverse ? 'glyphicon glyphicon-chevron-down' : 'glyphicon glyphicon-chevron-up'"></span>
        </th>
        <th ng-click="sortBy('job')">职业</th>
      </tr>
    </thead>
    <tbody>
      <tr ng-repeat="user in filteredUsers = (users | filter:searchText | orderBy:sortKey:reverse) 
                    | limitTo:itemsPerPage:currentPage*itemsPerPage">
        <td>{{user.name}}</td>
        <td>{{user.age}}</td>
        <td>{{user.job}}</td>
      </tr>
    </tbody>
  </table>
  <div class="pagination">
    <button ng-disabled="currentPage === 0" 
            ng-click="currentPage = currentPage - 1">上一页</button>
    <span>共 {{filteredUsers.length}} 条记录</span>
    <button ng-disabled="currentPage >= filteredUsers.length/itemsPerPage - 1" 
            ng-click="currentPage = currentPage + 1">下一页</button>
  </div>
</div>

对应的控制器需包含所有逻辑:

angular.module('myApp', [])
  .controller('TableController', function($scope) {
    $scope.users = [
      {name: '张三', age: 28, job: '工程师'},
      {name: '李四', age: 32, job: '设计师'},
      {name: '王五', age: 24, job: '产品经理'},
      {name: '赵六', age: 35, job: '经理'},
      {name: '钱七', age: 29, job: '开发'}
    ];
    $scope.sortKey = 'name';
    $scope.reverse = false;
    $scope.currentPage = 0;
    $scope.itemsPerPage = 3;
    $scope.sortBy = function(key) {
      $scope.reverse = ($scope.sortKey === key) ? !$scope.reverse : false;
      $scope.sortKey = key;
    };
  });

此示例中,filteredUsers 变量存储筛选和排序后的结果,分页逻辑基于过滤后的数据计算,确保分页准确性,通过合理组合 AngularJS 指令和过滤器,可构建功能完善、性能良好的动态表格。

图片来源于AI模型,如侵权请联系管理员。作者:酷小编,如若转载,请注明出处:https://www.kufanyun.com/ask/50852.html

(0)
上一篇 2025年11月2日 21:32
下一篇 2025年11月2日 21:36

相关推荐

  • 湖南地区服务器优势明显?为何选择湖南服务器成为热议话题?

    湖南省,作为中国中部的重要省份,不仅在历史文化和自然资源方面享有盛誉,同时在现代信息技术领域也展现出强大的实力,服务器产业作为信息技术的重要组成部分,在湖南得到了迅速发展,以下是对湖南服务器产业的详细介绍,湖南服务器产业概况产业规模湖南服务器产业近年来发展迅速,已成为湖南省高新技术产业的重要组成部分,根据最新数……

    2025年12月4日
    02500
  • 服务器设备要求有哪些关键参数需要注意?

    服务器设备要求在数字化时代,服务器作为企业信息系统的核心载体,其性能、稳定性和安全性直接关系到业务的连续性与数据的安全性,构建高效可靠的服务器环境,需从硬件配置、软件兼容性、扩展能力、功耗与散热、安全合规等多个维度综合考量,以满足不同场景下的应用需求,以下从关键要素出发,详细阐述服务器设备的核心要求,硬件配置……

    2025年12月6日
    02640
    • 服务器间歇性无响应是什么原因?如何排查解决?

      根源分析、排查逻辑与解决方案服务器间歇性无响应是IT运维中常见的复杂问题,指服务器在特定场景下(如高并发时段、特定操作触发时)出现短暂无响应、延迟或服务中断,而非持续性的宕机,这类问题对业务连续性、用户体验和系统稳定性构成直接威胁,需结合多维度因素深入排查与解决,常见原因分析:从硬件到软件的多维溯源服务器间歇性……

      2026年1月10日
      020
  • 服务器激活码找不到?新手小白激活步骤详解

    服务器激活码在哪里找在数字化办公和娱乐日益普及的今天,各类软件和服务器的使用已成为常态,无论是企业级服务器操作系统、专业软件,还是游戏服务器,激活码都是验证授权、解锁功能的关键,许多用户在初次使用或需要重新激活时,常常会困惑于“服务器激活码在哪里找”,本文将系统梳理不同场景下激活码的获取途径,帮助用户快速定位所……

    2025年12月16日
    03860
  • 服务器租价格受哪些因素影响?如何选择才最划算?

    在数字化浪潮席卷全球的今天,无论是初创企业、开发团队还是个人站长,服务器都已成为不可或缺的基础设施,而“服务器租价格”作为一项核心的运营成本,是所有决策者在规划IT架构时必须仔细权衡的关键指标,服务器租赁并非一个简单的标价,其背后牵涉到复杂的配置、服务、网络和地理位置等多重因素,理解这些因素如何影响最终价格,并……

    2025年10月26日
    01850

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注