Angular实现表单功能

技术涉及 双向数据绑定 ng-repeat、 ng-show指令和数组排序等知识。实现了表单的增、删和排序的功能

代码如下:

<!DOCTYPE html>
<html ng-app="myapp">

	<head>
		<meta charset="UTF-8">
		<title>模拟表单</title>
		<script type="text/javascript" src="../js/angular.js"></script>
		<style>
			.tab-content{
				width: 600px;
				margin: 0 auto;
			}
			table,tr,td,th{
				border: 1px solid #333;
				/*边框重合*/
				border-collapse: collapse;
				text-align: center;
			}
			th,td{
				width: 20%;
				line-height: 150%;
			}
			th{
				cursor: pointer;
			}
			tr:nth-child(2n){
				background-color: #eee;
			}
			.form_box{
				width: 600px;
				margin: 0 auto;
				background-color: skyblue;
			}
		</style>
	</head>

	<body ng-controller="MainCtr as mainctrl">
		<div class="tab-content">
			<table width="600px">
				<tr>
					<th ng-click = "mainctrl.changeSort('code')">编号 <span ng-show = "mainctrl.sortBy == 'code'">{{mainctrl.asc ? '↑':'↓'}}</span></th>
					<th ng-click = "mainctrl.changeSort('title')">名称 <span ng-show = "mainctrl.sortBy == 'title'">{{mainctrl.asc ? '↑':'↓'}}</span></th>
					<th ng-click = "mainctrl.changeSort('unit')">单位 <span ng-show = "mainctrl.sortBy == 'unit'">{{mainctrl.asc ? '↑':'↓'}}</span></th>
					<th ng-click = "mainctrl.changeSort('price')">价格 <span ng-show = "mainctrl.sortBy == 'price'">{{mainctrl.asc ? '↑':'↓'}}</span></th>
					<th ng-click = "mainctrl.changeSort('count')">数量 <span ng-show = "mainctrl.sortBy == 'count'">{{mainctrl.asc ? '↑':'↓'}}</span></th>
					<th>删除</th>
				</tr>
				<tr ng-repeat = "item in mainctrl.data">
					<td>{{item.code}}</td>
					<td>{{item.title}}</td>
					<td>{{item.unit}}</td>
					<td>{{item.price|currency:'¥'}}</td>
					<td>{{item.count}}</td>
					<td><input type="button" value="删除" ng-click ="mainctrl.remove(item.code)"/></td>
				</tr>
			</table>
		</div>
		<div class="form_box">
			<p>结果:{{mainctrl.formobj}}</p>
			<p>编号: <input type="text"  ng-model = "mainctrl.formobj.code"/></p>
			<p>名称: <input type="text"  ng-model = "mainctrl.formobj.title"/></p>
			<p>单位: <input type="text"  ng-model = "mainctrl.formobj.unit"/></p>
			<p>价格: <input type="text"  ng-model = "mainctrl.formobj.price"/></p>
			<p>数量: <input type="text"  ng-model = "mainctrl.formobj.count"/></p>
			<p><input type="button" name="" id="" value="提交"  ng-click = "mainctrl.add()"/></p>
		</div>

	</body>

</html>
<script type="text/javascript">
	var app = angular.module("myapp", []);
	app.controller('MainCtr', function() {
		// 1.定义数据
		this.data = [
		{"code" : "0001" , "title" : "ATF-4R" , "unit" : "升" , "price" : "12.9" , "count" : 10},
		{"code" : "0002" , "title" : "ATF-5R" , "unit" : "升" , "price" : "13.9" , "count" : 6},
		{"code" : "0003" , "title" : "ATF-6R" , "unit" : "升" , "price" : "14.9" , "count" : 9},
		{"code" : "0004" , "title" : "ATF-7R" , "unit" : "升" , "price" : "15.9" , "count" : 8},
		]
		// 2.定义表单对象
		this.formobj = {};
		// 3.定义提交函数
		var that = this;
		this.add = function(){
			that.data.push(that.formobj);
			that.formobj = {};
		}
		// 4.删除函数
		this.remove = function(code){
			console.log(code);
			for(let i = 0 ;i < that.data.length;i++){
				if(that.data[i].code == code){
					// 第二个函数 不为0表示删除
					that.data.splice(i,1);
				}
			}
			
		}
		// 5.定义排序字段
		this.sortBy = "code";
		// 6.定义排序方式 true为升序 false 降序排列
		this.asc = "true";
		// 7.排序方法
		this.changeSort = function(value){
			if(value == that.sortBy){
				that.asc = !that.asc;
			}else{
				that.sortBy = value;
				that.asc = true;
			}
			// 开始排序
			that.data.sort(function(a,b){
				if(a[that.sortBy] > b[that.sortBy]){
					return that.asc ? 1:-1;
				}else{
					return that.asc ? -1:1;
				}
			})
		}
	});
</script>

效果如下:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值