js實現查詢 添加 排序功能

.
<html>


<head>
<meta charset="utf-8" />
<title></title>
<!--
        引入第三類庫和樣式
        -->
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css" />
<script src="js/jquery-3.2.1.min.js" type="text/javascript" charset="utf-8"></script>
<script src="js/angular.min.js" type="text/javascript" charset="utf-8"></script>


<style type="text/css">
.content {
margin-top: 20px;
display: flex;
flex-direction: row;
}

.left {
flex: 1;
}

.right {
flex: 1;
}

.left input {
width: 500px;
height: 30px;
}

.right select {
width: 500px;
height: 35px;
}

table {
margin-top: 20px;
width: 1220px;
}

#add {
margin-top: 20px;
background: #33CCFF;
border-radius: 5px;
width: 80px;
height: 30px;
}
</style>
</head>


<body ng-app="myApp" ng-controller="myCtrl">
<!--
        界面
        -->
姓名:<input type="text" id="name" />位置:<input type="text" id="weizhi" />球號:<input type="text" id="qiuhao" />票數:<input type="text" id="piaoshu" />
<br>
<div class="content">
<div class="left">
<span>
<input class="button-dark" type="button" id="add" value="查詢球員" ng-click="add()" />
</span><br>
<!--
        查詢輸入框
        -->
<input type="text" ng-keydown="cha($event)" id="chaName"/>
</div>
<div class="right">
<span>
排序
</span><br>
<select ng-model="xuanxiang" ng-options="x for x in names" ng-init="xuanxiang = names[0]" ng-change="change()">
</select>
</div>
</div>
<!--
        添加按鈕 改變樣式
        -->
<input class="button-dark" type="button" id="add" value="添加球員" ng-click="add()" />
<!--
        表格,顯示數據
        -->
<table border="1px" cellspacing="0px" cellpadding="0px" class="table-striped">
<tr style="background: #999999;">
<td>姓名</td>
<td>位置</td>
<td>球號</td>
<td>票數</td>
</tr>
<tr ng-repeat="p in persons">
<td>{{p.name|myFilter}}</td>
<td>{{p.weizhi}}</td>
<td>{{p.qiuhao}}</td>
<td>{{p.piaoshu}}</td>
</tr>
</table>
<!--
        寫完ng-app等指令
        創建模塊等操作
        -->
<script type="text/javascript">
var mo = angular.module("myApp", []);
//定義過濾器,過濾敏感詞   比如:馬--*
mo.filter("myFilter",function(){

return function(input){//input 就是要過濾的數據


var newInput = input.replace(/馬/g,"*");//參數1:正則,要替換的內容   參數2:新的內容

return newInput;
}

});//參數1:過濾器的名字,參數2.方法
//創建控制器
mo.controller("myCtrl", function($scope) {
//下拉顯示的數據
$scope.names =["升序","降序"];
//初始化數據
$scope.persons = [{
"name": "梅西",
"weizhi": "前鋒",
"qiuhao": 10,
"piaoshu": 100
}, {
"name": "內馬爾",
"weizhi": "前鋒",
"qiuhao": 11,
"piaoshu": 80
}, {
"name": "貝克漢姆",
"weizhi": "前鋒",
"qiuhao": 10,
"piaoshu": 90
}];
//添加
$scope.add = function() {
//取得數據
var name = $("#name").val();
var weizhi = $("#weizhi").val();
var qiuhao = $("#qiuhao").val();
var piaoshu = $("#piaoshu").val();
//驗證
if (name == "") {
alert("名字不能爲空");
return;
}
//驗證是否重複
for (var i = 0; i < $scope.persons.length; i++) {
var p0 = $scope.persons[i];
var pname = p0.name;
if (pname == name) {
alert("名字重複");
return;
}
}
//添加
var newPerson = {
"name": name,
"weizhi": weizhi,
"qiuhao": qiuhao,
"piaoshu": piaoshu
};
$scope.persons.push(newPerson);
}
//排序
$scope.change = function() {
var xz = $scope.xuanxiang;
//判斷
if(xz=="升序"){
$scope.persons.sort(function(a,b){
if(a.piaoshu<b.piaoshu){
return -1;
}else if(a.piaoshu>b.piaoshu){
return 1;
}
return 0;
});
}else{
$scope.persons.sort(function(a,b){
if(a.piaoshu<b.piaoshu){
return 1;
}else if(a.piaoshu>b.piaoshu){
return -1;
}
return 0;
});
}
}
//查詢事件
$scope.cha = function($event){

var key = $event.keyCode;
if(key==13){
//取得輸入的值
var name1 = $("#chaName").val();
//創建一個新的數組
var newPersons = [];
for (var i=0;i<$scope.persons.length;i++) {
if(name1==$scope.persons[i].name){
newPersons.push($scope.persons[i]);
}
}
//之後,將數據替換掉
$scope.persons = newPersons;
}

}

});
</script>
</body>


</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章