-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrud.html
205 lines (191 loc) · 5.87 KB
/
crud.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<!DOCTYPE html>
<html><head>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<style>
.loading {
position: relative;
top: 0;
left: 0;
height: 128px;
width: 128px;
-webkit-mask-image: url(spinner.png);
background-color: #000;
-webkit-animation-name: spinnerRotate;
-webkit-animation-duration: 2s;
-webkit-animation-iteration-count: infinite;
-webkit-animation-timing-function: linear;
}
@-webkit-keyframes spinnerRotate {
from {
-webkit-transform:rotate(0deg);
}
to {
-webkit-transform:rotate(360deg);
}
}
th {
background: #e0e0e0;
width: 15em;
}
.editcol {
width:10em
}
tr:nth-child(odd) input {
background: #e0e0e0;
width: 100%;
}
tr:nth-child(even) input {
background: white;
width: 100%;
}
input {
border: none;
}
input.editing {
border: solid thin black;
}
div[ng-app] {
margin: 5em;
}
.statuscol {
color: red;
}
</style>
</head>
<body>
<div ng-app="crud" ng-controller="InplaceEditController as myCtrl" >
<table>
<tr>
<th ng-repeat="field in fields">{{field.name}}</th>
<th colspan="2">edit</th>
</tr>
<tr ng-repeat="person in people" ng-form="editUserForm">
<td ng-repeat="field in fields" >
<input dyn-name="{{field.name}}" ng-disabled="!person.editing" ng-class="{editing:person.editing}"
ng-model="person[field.name]" ng-required="{{field.required}}" type="{{field.type}}" min="{{field.min}}"
max="{{field.max}}" />
</td>
<td class="editcol">
<span ng-hide="person.editing">
<button type="button" ng-click="edit(person)"><span class="glyphicon glyphicon-pencil"></span></button>
<button type="button" ng-click="delete(person)"><span class="glyphicon glyphicon-remove"></span></button>
</span>
<span ng-show="person.editing">
<button type="button" ng-click="formsubmit(person)">save</button>
<button type="button" ng-click="reset(person)">cancel</button>
</span>
</td>
<td class="statuscol">
<div ng-repeat="validation in validations">
<div my-validate="{{validation.msg}}" ng-repeat="field in fields" ng-show="editUserForm[field['name']].$error.{{validation.type}}" ></div>
</div>
<div ng-bind="person.status"></div>
</td>
</tr>
</table>
<div ng-hide="addinguser || loading">
<button type="button" ng-click="addinguser=true;newperson={editing:true,new:true}; people.push(newperson); ">
<span class="glyphicon glyphicon-plus"></span>Add user
</button>
</div>
<div ng-class="{loading:loading}"></div>
</div>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.0-rc.0/angular.min.js"></script>
<script>
(function (){
angular.module('crud',[])
.controller('InplaceEditController',function($scope,$http,$filter,$compile){
/*****INIT VARIABLES*****/
$scope.master={};
$scope.fields=[{name:'name',type:'string',required:true},{name:'age',type:'number',required:true,min:1}];
$scope.validations=[
{type:'required',msg:'{{field.name}} is required'},
{type:'number',msg:'{{field.name}} takes a numeric value'},
{type:'min',msg:'{{field.name}} must be at least {{field.min}}'},
{type:'max',msg:'{{field.name}} must be less than {{field.max}}'}
];
/****RESET*****/
$scope.reset=function(person){
person.status="";
if(person.new==true) {
$scope.people.pop();
$scope.addinguser=false;
return;
};
//restore old values
for (field in $scope.fields) {
person[$scope.fields[field].name]=person.save[$scope.fields[field].name];
}
person.save=null;
person.editing=false;
};
/****EDIT*****/
$scope.edit = function(person){
person.save={};
//save old values
for (field in $scope.fields) {
person.save[$scope.fields[field].name]=person[$scope.fields[field].name];
}
person.editing=true;
};
/*****SUBMIT*****/
$scope.formsubmit = function(person) {
person.status="";
action = (person.new==true ? 'addperson' : 'updateperson' );
$http.post('/angular-crud/serverside.php?'+action,person).success(function(p){
console.log($filter('json')(p));
person.editing=person.new=false;
person.id=p.id;
$scope.addinguser=false;
})
.error(function(e){person.status=e.err;});
};
/*****DELETE*****/
$scope.delete=function(person){
person.status="";
$http.post('/angular-crud/serverside.php?deleteperson',person).success(function(p){
console.log($filter('json')($scope.people));
console.log("deleting index " + p);
for (i in $scope.people) {
if ($scope.people[i].id==p) {
$scope.people.splice(i,1);
break;
}
}
})
.error(function(e){person.status=e.err;});
}
/*****GET*****/
$scope.loading=true;
$http.get('/angular-crud/serverside.php').success(function(e){
console.log(e);
$scope.people=[];
$scope.people=e;
$scope.loading=false;
console.log($scope.people);
});
})
/*****ALLOW DYNAMIC FORMS*****/
//see http://stackoverflow.com/questions/14378401/dynamic-validation-and-name-in-a-form-with-angularjs
.directive('dynName',function() {
return {
require : ['ngModel', '^form'],
link : function(scope,element,attr,ctrls) {
ctrls[0].$name = attr.dynName;
ctrls[1].$addControl(ctrls[0]);
}
}
})
/*****GENERALIZE VALIDATION MESSAGE*****/
.directive('myValidate',function($compile) {
return {
link: function(scope,element,attr){
element.text(attr.myValidate);
$compile(element.contents())(scope);
}
};
});
;})();
</script>
</body>
</html>