angular表格带筛选分页,本地json

这篇博客展示了如何使用AngularJS实现一个表格,该表格包含筛选和分页功能。用户可以通过选择城市和国家来筛选数据,并通过按钮在本地JSON数据中进行前后翻页。表格的数据源是一个包含多个对象的数组,每个对象包含Name、City和Country属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<body ng-app="formTest" >
		<div class="container">
			<div class="add" ng-controller="formCtrl">			
				<h1>list</h1>
				<table border="1" cellspacing="0px" width="100%" >
					<thead>
						<tr>
							
							<th width="35%">name</th>
							<th width="35%"><select ng-model="data1.City" class="select">
								<option value="">city</option>
								<option value="Bergamo">Bergamo</option>
								<option value="London">London</option>
							</select></th>
							<th width="25%"><select ng-model="data1.Country" class="select">
								<option value="">country</option>
								<option value="Canada">Canada</option>
								<option value="Italy">Italy</option>
							</select></th>
						</tr>
					</thead>
					<tbody>
						<tr  ng-repeat="student in (stu=(students|filter:data1|orderBy:'Name')).slice((currentNum - 1) * pageSize, currentNum * pageSize)" >
							
							<td >{{student.Name}}</td>
							<td>{{student.City}}</td>
							<td>{{student.Country}}</td>
						</tr>
					</tbody>
					<tfoot>
						<tr>
							<td colspan="3">
								<div class="turnPageButtonArea" >
								    <button ng-click="previous()">Previous</button>
								    <button ng-click="next()">Next</button>
								    <input type="number" ng-model="toPage" class="pageNum" placeholder="{{currentNumber}}">
								    <button ng-click="goToPage()">Go</button>
								    <span class="totalPages" >共  {{(stu.length/pageSize)|ceil}}页</span>
								</div>
							</td>
						</tr>
					</tfoot>
				</table>
				
			</div>
			
		</div>
	
	</body>
script
var app = angular.module('formTest', []);
	app.factory('pageService',[
		function page(){
			return{
				setPage:function(data, currentPage, pageSize){
						 var pagedData = data.slice((currentPage - 1) * pageSize, currentPage * pageSize);
		       			 return pagedData;
				}				
			}
		}
		]);
	app.filter('ceil', function() {
 		return function(input) {
 			return Math.ceil(input);
 		};
	});
	app.controller('formCtrl', ['$rootScope','$scope','$http','$filter','pageService',
		function($rootScope,$scope,$http,$filter,pageService) {
			var url="stu.json";	
			$scope.pageSize=4;	
			
			$http.get(url).success( function(response) {
				$rootScope.students = response;
				$scope.currentNum=1;
				$scope.currentNumber=$scope.currentNum;
	   		});
	   		
   			$scope.previous=function(){
   				//console.log($scope.stu.length);
   				if($scope.currentNum>1){
   					$scope.currentNum-=1;
   					$scope.currentNumber=$scope.currentNum;
   				}
   				else{alert("第一页")};	
   			}
   			$scope.next=function (){
   				//max=$scope.stu.length;
   				max=Math.ceil($scope.stu.length/$scope.pageSize);
		   		if($scope.currentNum<max){
		   			$scope.currentNum+=1;
		   			$scope.currentNumber=$scope.currentNum;
	   			}
	   			else{alert("最后一页")};
   			}
   			$scope.goToPage=function(){
	   			var num=$scope.toPage;
	   			max=Math.ceil($scope.stu.length/$scope.pageSize);
	   			if(num>=1&&num<=max){
	   				$scope.currentNum=num;
	   				$scope.toPage="";
	   				$scope.currentNumber=$scope.currentNum;
	   			}
	   			else{
	   				alert("请重新输入");
	   			}
   		}
	}]);
	
	



json
[
{
"Name" : "Alfreds Futterkiste",
"City" : "Berlin",
"Country" : "Germany"
},
{
"Name" : "Berglunds snabbköp",
"City" : "Luleå",
"Country" : "Sweden"
},
{
"Name" : "Centro comercial Moctezuma",
"City" : "México D.F.",
"Country" : "Mexico"
},
{
"Name" : "Ernst Handel",
"City" : "Graz",
"Country" : "Austria"
}
]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值