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

相关推荐

  • AngularJS过滤器filter用法总结,如何实现复杂过滤与自定义过滤?

    AngularJS过滤器filter用法总结AngularJS作为一款经典的前端框架,其过滤器(Filter)功能在数据展示和处理中扮演着重要角色,过滤器主要用于格式化数据,如日期格式化、数字格式化、字符串处理等,同时支持自定义过滤逻辑,帮助开发者更高效地处理视图层的数据绑定,本文将详细介绍AngularJS过……

    2025年10月31日
    0660
  • 服务器路由器网关怎么设置?新手必看步骤指南

    服务器、路由器、网关的基础概念与作用在开始设置之前,首先需要明确三者的基本概念与功能定位,服务器是网络中为其他设备提供计算、存储或应用服务的专用设备,如Web服务器、数据库服务器等;路由器则是连接不同网络、实现数据包转发的设备,负责根据IP地址选择最佳路径;网关则是网络与外部网络(如互联网)之间的接口,通常指网……

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

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

      2026年1月10日
      020
  • 服务器服务在哪里打开

    服务器服务在哪里打开在数字化时代,服务器作为支撑各类应用运行的核心基础设施,其服务的开启与管理是技术运维和系统开发中的基础操作,无论是搭建网站、部署应用程序,还是进行数据存储与处理,正确找到并开启服务器服务都是首要步骤,本文将从不同操作系统、管理工具和服务类型出发,详细说明服务器服务的开启方法,帮助读者快速定位……

    2025年12月25日
    0720
  • 想知道服务器负责人是谁,该找哪个部门或人查询?

    在数字化时代,服务器作为企业业务运行的核心载体,其稳定性和安全性直接关系到日常运营的连续性,明确服务器的管理责任主体,建立清晰的权责体系,是保障服务器高效运转的前提,服务器负责人是谁”这一问题,答案并非单一,而是取决于组织架构、管理模式以及服务器的具体用途,通常涉及技术团队、管理层及第三方服务商等多重角色,技术……

    2025年11月24日
    0590

发表回复

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