angular表单添加查询删除

<!DOCTYPE html>
<html>


<head>
<meta charset="utf-8" />
<title></title>
<script src="js/angular.min.js" type="text/javascript" charset="utf-8"></script>
<style type="text/css">
* {
margin: 0px auto;
padding: 0px;
}

.main {
margin-top: 50px;
width: 800px;
}

tr:nth-of-type(odd) {
background: #FFFFFF;
}

tr:nth-of-type(odd):hover {
background: #41C7DB;
}

tr:nth-of-type(even) {
background: #EEEEEE;
}

tr:nth-of-type(even):hover {
background: #41C7DB;
}

a {
text-decoration: none;
}

.cha {
width: 650px;
}
</style>
</head>


<body ng-app="myApp" ng-controller="myCtrl">
<div class="main">
<input type="text" ng-model="name" class="cha" />
<select ng-change="change()" ng-model="age" ng-init="age=ages[0]">
<option ng-repeat="x in ages">{{x}}</option>
</select>
<table border="1px" cellpadding="0px" cellspacing="0px" width="800px" style="margin-top: 30px;">


<tr style="background: gray;">
<th>姓名</th>
<th>年龄</th>
<th>拼音</th>
<th>职位</th>
<th>操作</th>
</tr>
<tr ng-repeat="person in persons">
<th>{{person.name}}</th>
<th>{{person.age}}</th>
<th>{{person.pin}}</th>
<th>{{person.zhiwei}}</th>
<th><a href="#" ng-click="dele($index)">删除</a></th>
</tr>
</table>
<input type="button" value="查询" ng-click="cha()" style="margin-top: 30px;width: 100px;" />
<input type="button" value="添加" ng-click="xianshi()" style="margin-top: 30px;width: 100px;" />
<hr />
<fieldset ng-show="xs">
<legend>添加用户信息</legend>
姓名:<input type="text" ng-model="name1" /><br> 年龄:
<input type="text" ng-model="age1" /><br> 拼音:
<input type="text" ng-model="pin1" /><br> 职位:
<input type="text" ng-model="zhiwei1" /><br>
<input type="button" value="保存" ng-click="add()" />
</fieldset>
</div>


<script type="text/javascript">
var mo = angular.module("myApp", []);
mo.controller("myCtrl", function($scope) {


$scope.ages = ["按年龄正序排序", "按年龄倒序排列"];
//定义数组
var persons_temp = [{
"name": "张三",
"age": 22,
"pin": "zhnagsan",
"zhiwei": "总经理"
}, {
"name": "李四",
"age": 20,
"pin": "lisi",
"zhiwei": "总经理"
}, {
"name": "杨过",
"age": 25,
"pin": "yangguo",
"zhiwei": "总经理"
}];
//赋值
$scope.persons = persons_temp;
//排序


$scope.change = function() {
var a1 = $scope.age;
//console.log("--" + a1);
if (a1 == "按年龄倒序排列") {
//console.log(a1 + "----")
persons_temp.sort(function(a, b) {
return b.age - a.age;
});
} else {
persons_temp.sort(function(a, b) {
return a.age - b.age;
});
}
//重新赋值
$scope.persons = persons_temp;
}
//查询
$scope.cha = function() {
//新数组
var newPersons = [];
var n = $scope.name;
if (n == "") {
alert("名字不能为空")
return;
}
//遍历
for (var i = 0; i < persons_temp.length; i++) {
var n1 = persons_temp[i].name;
//模糊查询
if (n1.indexOf(n)!=-1) {
newPersons.push(persons_temp[i]);
}


}
//判断
if (newPersons.length == 0) {
alert("没有匹配项");
return;
}
$scope.persons = newPersons;
}
//删除
$scope.dele = function($index) {
persons_temp.splice($index, 1);
$scope.persons = newPersons;
return false;
}
//控制显示/隐藏
$scope.xianshi = function() {
$scope.xs = true;
}
//保存
$scope.add = function() {
//创建对象
var obj = {
"name": $scope.name1,
"age": $scope.age1,
"pin": $scope.pin1,
"zhiwei": $scope.zhiwei1
};
persons_temp.push(obj);
$scope.persons = persons_temp;
//添加完毕,将输入框清空
$scope.name1="";
$scope.age1="";
$scope.pin1="";
$scope.zhiwei1="";
//隐藏掉
$scope.xs = false;
}
})
</script>
</body>


</html>

猜你喜欢

转载自blog.csdn.net/qq_40056429/article/details/78767342