-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgrid-widget.html
102 lines (89 loc) · 3.26 KB
/
grid-widget.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
<!DOCTYPE html>
<html>
<head>
<title>Kendo UI DataSource CRUD Example</title>
<meta charset="utf-8">
<link rel="stylesheet" href="http://cdn.kendostatic.com/2015.1.318/styles/kendo.common.min.css" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" />
<link rel="stylesheet" href="http://cdn.kendostatic.com/2015.1.318/styles/kendo.bootstrap.min.css" />
</head>
<body style="margin:100px">
<div class="panel panel-default">
<div class="panel-body">
<label for="name">Enter users name click add:</label>
<div class="input-group">
<input type="text" id="name" class="form-control" placeholder="name">
<span class="input-group-btn">
<button class="btn btn-primary" id="add" type="button">Add</button>
</span>
</div>
<br>
<div id="grid"></div>
</div>
</div>
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="http://cdn.kendostatic.com/2015.1.318/js/kendo.all.min.js"></script>
<script>
//create DataSource instance
var dataSource = new kendo.data.DataSource({
autoSync: true, //sync changes with restful API automatically
transport: {
read: {
url: 'http://localhost:3000/users',
dataType: 'json', //not needed jQuery figures it out, shown to be verbose
type: 'GET' //defined but, this is the default
},
create: {
url: 'http://localhost:3000/users',
type: 'POST'
},
update: {
url: function(data) {
return 'http://localhost:3000/users/' + data.id;
},
type: 'PUT'
},
destroy: {
url: function(data) {
return 'http://localhost:3000/users/' + data.id;
},
type: 'DELETE'
}
},
schema: {
model: {
id: 'id'
}
}
});
$('#grid').kendoGrid({
columns: [
{field: 'id'},
{field: 'name',template:'<input value="#:name#">'},
{field: ' ',template:'<button type="button" data-id="#:id#" id="update" class="btn btn-default btn-xs">update</button> <button type="button" data-id="#:id#" id="delete" class="btn btn-danger btn-xs">delete</button>'}
],
dataSource: dataSource
});
$('#add').on('click', function() {
dataSource.add({
name: $('#name').val()
});
//dataSource.sync(); //using autoSync: true so don't have to call
});
$('#grid').on('click', '#delete', function() {
var $this = $(this);
dataSource.remove(
dataSource.get($this.data('id'))
);
//dataSource.sync(); //using autoSync: true so don't have to call
});
$('#grid').on('click', '#update', function() {
var $this = $(this);
dataSource.get(
$this.data('id')
).set('name', $this.closest('tr').find('input').val());
//dataSource.sync(); //using autoSync: true so don't have to call
});
</script>
</body>
</html>