angulajs增删改查以及排序验证

来源:互联网 发布:"笨办法"学python 编辑:程序博客网 时间:2024/06/03 17:38
<!DOCTYPE html>
<html>


<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" src="js/angular.min.js"></script>


<style>
.title {
margin-top: 20px;
}

.table {
margin-top: 30px;
}

.f {
margin-top: 30px;
width: 600px;
}
</style>
</head>


<body ng-app="myapp" ng-controller="mycr">
<center>
<p>管理信息</p>
<div class="title">
<input type="button" value="批量删除" ng-click="pshan()" />&nbsp;&nbsp;&nbsp;&nbsp; 用户名&nbsp;&nbsp;
<input type="text" ng-model="mname" ng-keydown="huiche($event)" />&nbsp;&nbsp;&nbsp;&nbsp;


<select ng-model="px" ng-init="px=xuan[0]" ng-change="paixu()">
<option ng-repeat="xx in xuan">{{xx}}</option>
</select>
&nbsp;&nbsp;&nbsp;&nbsp;


<input type="button" value="添加" ng-click="tian()" />
</div>


<table border="1px" width="600px" cellpadding="0" cellspacing="1" class="table">
<tr>
<th><input type="checkbox" ng-model="qx" ng-click="mqx()" /></th>
<th>姓名</th>
<th>年龄</th>
<th>城市</th>
<th>录入日期</th>
<th>操作</th>
</tr>
<!--根据name查找-->
<tr align="center" ng-repeat="x in user|filter:{name:sname}">
<td><input type="checkbox" ng-model="x.check" /></td>
<td>{{x.name}}</td>
<td>{{x.age}}</td>
<td>{{x.city}}</td>
<td>{{x.data}}</td>
<td>
<input type="button" value="修改" ng-click="update($index)"/>&nbsp;&nbsp;
<input type="button" value="删除" ng-click="shanchu($index)" />
</td>
</tr>
</table>


<fieldset class="f" ng-show="yinxian">
<legend>用户信息</legend>
<center>
姓名<input type="txet" ng-model="aname" /><br /><br /> 年龄
<input type="txet" ng-model="aage" /><br /><br /> 城市
<input type="txet" ng-model="acity" /><br /><br />
<input type="button" value="OK" ng-click="add()" />
</center>
</fieldset>


</center>
</body>
<script>
var app = angular.module("myapp", []);
app.controller("mycr", function($scope) {
$scope.xuan = ["以年龄倒序", "以年龄正序"];


$scope.user = [{
"check": false,
"name": "张三",
"age": "22",
"city": "北京",
"data": "2014-12-15"
},
{
"check": false,
"name": "文儿",
"age": "24",
"city": "河北",
"data": "2015-10-12"
},
{
"check": false,
"name": "夏可可",
"age": "20",
"city": "石家庄",
"data": "2017-08-22"
}
];


//根据用户名模糊查询
$scope.huiche = function(enevt) {
if(enevt.keyCode == 13) {
$scope.sname = $scope.mname;
}
}


//排序的代码
$scope.paixu = function() {
//alert($scope.px)
if($scope.px == "以年龄倒序") {
$scope.sname="";
$scope.user.sort(function(a,b){
return b.age-a.age;
})


} else {
$scope.user.sort(function(a,b){
$scope.sname="";
return a.age-b.age;
})

}


}




//删除的代码
$scope.shanchu = function($index) {
$scope.user.splice($index, 1);


}




//全选的代码
$scope.mqx = function() {


if($scope.qx) {


for(var i = 0; i < $scope.user.length; i++) {
$scope.user[i].check = true;
}
} else {
for(var i = 0; i < $scope.user.length; i++) {
$scope.user[i].check = false;
}
}
}

//批量删除的代码
$scope.pshan = function() {
for(var i = $scope.user.length - 1; i >= 0; i--) {
if($scope.user[i].check) {
$scope.user.splice(i, 1);
}
}
//把全选改为false
$scope.qx = false;
}


//点击添加按钮显示添加
$scope.tian = function() {
$scope.yinxian = true;
                
               //添加的代码
$scope.add = function() {

var flog = kong($scope.aname) && kong($scope.aage) && shu($scope.aage) && kong($scope.acity);

if(flog) {
var tian = {
"check": false,
"name":$scope.aname,
"age": $scope.aage,
"city": $scope.acity,
"data": new Date().toLocaleDateString(),
   };
$scope.user.push(tian);

//清空的方法
$scope.aname="";
                   $scope.aage="";
                   $scope.acity="";
                   //隐藏
                   $scope.yinxian = false;
}
}


}





//不为空的方法
function kong(k) {
if(k == undefined||k=="") {
 
alert("不能为空")
return false;
}
return true;
}
//年龄必须为数字的方法
function shu(s) {
var s1 = /^\d+$/;
if(!s1.test(s)) {
alert("年龄必须为数字")
return false;
}
return true;
}


//修改的代码
$scope.update=function($index){
$scope.yinxian = true;
$scope.aname=$scope.user[$index].name;
$scope.aage=$scope.user[$index].age;
$scope.acity=$scope.user[$index].city;

$scope.add = function() {

//判断不为空   年龄必须为数字
var flog = kong($scope.aname) && kong($scope.aage) && shu($scope.aage) && kong($scope.acity);
if(flog){
//两种方法都行
/*splice第一个参数为要删除的下标  第二个为删除1个  第三个为把当前值替换为ss对象*/
/*var ss={"name":$scope.aname,"age":$scope.aage,"city":$scope.acity,"data":new Date().toLocaleDateString()};
                     $scope.user.splice($index, 1,ss);*/
$scope.user[$index].name=$scope.aname;
$scope.user[$index].age=$scope.aage;
$scope.user[$index].city=$scope.acity;

}
//清空的方法
$scope.aname="";
                   $scope.aage="";
                   $scope.acity="";
                   //隐藏
                   $scope.yinxian = false;

}
}


})
</script>


</html>
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 魅族软件包损坏怎么办 安装包自动删除怎么办 apk文件损坏了怎么办 安装包发生错误怎么办 转发视频穿帮了怎么办 自拍神器死机了怎么办 电脑看视频死机怎么办 ios验证应用没用怎么办 宝宝不睡睡袋怎么办 酷我音乐收费怎么办 电脑版酷狗字体模糊怎么办 酷我切歌怎么办 快手暂不支持音乐格式怎么办 上传视频不清晰怎么办 MP4格式嫌大怎么办 课堂派怎么办改成考试 手机信息幕变黑怎么办 手机百度太耗电怎么办 电脑打不开主页面怎么办? 贴吧被永久封了怎么办 晒课视频太大怎么办 有道精品课过期怎么办 手机缓存变小了怎么办 优学院课程过期怎么办 上公开课紧张怎么办 光纤被老鼠咬断怎么办 石灰粉进入眼睛怎么办 幼儿误吃粉笔怎么办? 吃了粉笔应该怎么办 小孩吃了颜料怎么办 小宝宝吃了纸怎么办 孩子不认真听讲怎么办 监控手机软件离线状态怎么办 云课堂忘记密码怎么办 广州办培训机构怎么办 一师一优课账号忘了怎么办 云相册空间不足怎么办 三星云空间不足怎么办 三星储存空间不足怎么办 宁阳县教育局强制补课怎么办 沉迷网络该怎么办英语