table-data.js
4.73 KB
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
$(document).ready(function() {
//ajax mocks
$.mockjaxSettings.responseTime = 500;
$.mockjax({
url: '/post',
response: function(settings) {
log(settings, this);
}
});
$.mockjax({
url: '/error',
status: 400,
statusText: 'Bad Request',
response: function(settings) {
this.responseText = 'Please input correct value';
log(settings, this);
}
});
$.mockjax({
url: '/status',
status: 500,
response: function(settings) {
this.responseText = 'Internal Server Error';
log(settings, this);
}
});
$.mockjax({
url: '/groups',
response: function(settings) {
this.responseText = [
{value: 0, text: 'Guest'},
{value: 1, text: 'Service'},
{value: 2, text: 'Customer'},
{value: 3, text: 'Operator'},
{value: 4, text: 'Support'},
{value: 5, text: 'Admin'}
];
log(settings, this);
}
});
function log(settings, response) {
var s = [], str;
s.push(settings.type.toUpperCase() + ' url = "' + settings.url + '"');
for(var a in settings.data) {
if(settings.data[a] && typeof settings.data[a] === 'object') {
str = [];
for(var j in settings.data[a]) {str.push(j+': "'+settings.data[a][j]+'"');}
str = '{ '+str.join(', ')+' }';
} else {
str = '"'+settings.data[a]+'"';
}
s.push(a + ' = ' + str);
}
s.push('RESPONSE: status = ' + response.status);
if(response.responseText) {
if($.isArray(response.responseText)) {
s.push('[');
$.each(response.responseText, function(i, v){
s.push('{value: ' + v.value+', text: "'+v.text+'"}');
});
s.push(']');
} else {
s.push($.trim(response.responseText));
}
}
s.push('--------------------------------------\n');
$('#console').val(s.join('\n') + $('#console').val());
}
//turn to inline mode
$.fn.editable.defaults.mode = 'inline';
//editables
$('#example-editable td a').editable({
url: '/post',
type: 'text',
pk: 1,
name: 'username',
title: 'Enter username'
});
// Datatables
$('#example').DataTable();
$('#example-editable').DataTable();
var table = $('#example2').DataTable({
"columnDefs": [
{ "visible": false, "targets": 2 }
],
"order": [[ 2, 'asc' ]],
"displayLength": 25,
"drawCallback": function ( settings ) {
var api = this.api();
var rows = api.rows( {page:'current'} ).nodes();
var last=null;
api.column(2, {page:'current'} ).data().each( function ( group, i ) {
if ( last !== group ) {
$(rows).eq( i ).before(
'<tr class="group"><td colspan="5">'+group+'</td></tr>'
);
last = group;
}
} );
}
} );
// Order by the grouping
$('#example2 tbody').on( 'click', 'tr.group', function () {
var currentOrder = table.order()[0];
if ( currentOrder[0] === 2 && currentOrder[1] === 'asc' ) {
table.order( [ 2, 'desc' ] ).draw();
}
else {
table.order( [ 2, 'asc' ] ).draw();
}
} );
$.fn.isValid = function(){
return this[0].checkValidity()
}
var t = $('#example3').DataTable();
$('#add-row').on( 'click', function () {
if($("#add-row-form").isValid()) {
var name = $('#name-input').val(),
position = $('#position-input').val(),
age = $('#age-input').val(),
date = $('#date-input').val(),
salary = $('#salary-input').val();
t.row.add( [
name,
position,
age,
date,
'$' + salary
] ).draw();
$('.modal').modal('hide');
return false;
}
});
$('.date-picker').datepicker({
orientation: "top auto",
autoclose: true
});
});