forked from 6pac/SlickGrid
/
example3a-compound-editors.html
150 lines (120 loc) · 4.09 KB
/
example3a-compound-editors.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
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="shortcut icon" type="image/ico" href="favicon.ico" />
<title>SlickGrid example 3a: Advanced Editing</title>
<link rel="stylesheet" href="../slick.grid.css" type="text/css"/>
<link rel="stylesheet" href="../css/smoothness/jquery-ui.css" type="text/css"/>
<link rel="stylesheet" href="examples.css" type="text/css"/>
<style>
.cell-title {
font-weight: bold;
}
</style>
</head>
<body>
<div style="position:relative">
<div style="width:600px;">
<div id="myGrid" style="width:100%;height:500px;"></div>
</div>
<div class="options-panel">
<h2>Demonstrates:</h2>
<ul>
<li>compound cell editors driving multiple fields from one cell</li>
<li>providing validation from the editor</li>
<li>hooking into validation events</li>
</ul>
<h2>View Source:</h2>
<ul>
<li><A href="https://github.com/6pac/SlickGrid/blob/master/examples/example3a-compound-editors.html" target="_sourcewindow"> View the source for this example on Github</a></li>
</ul>
</div>
</div>
<script src="../lib/firebugx.js"></script>
<script src="../lib/jquery-1.12.4.min.js"></script>
<script src="../lib/jquery-ui.min.js"></script>
<script src="../lib/jquery.event.drag-2.3.0.js"></script>
<script src="../slick.core.js"></script>
<script src="../slick.editors.js"></script>
<script src="../slick.grid.js"></script>
<script>
var grid;
var data = [];
var columns = [
{id: "title", name: "Title", field: "title", width: 120, cssClass: "cell-title", editor: Slick.Editors.Text},
{id: "range", name: "Range", width: 120, formatter: NumericRangeFormatter, editor: NumericRangeEditor}
];
var options = {
editable: true,
enableAddRow: false,
enableCellNavigation: true,
asyncEditorLoading: false
};
function NumericRangeFormatter(row, cell, value, columnDef, dataContext) {
return dataContext.from + " - " + dataContext.to;
}
function NumericRangeEditor(args) {
var $from, $to;
var scope = this;
this.init = function () {
$from = $("<INPUT type=text style='width:40px' />")
.appendTo(args.container)
.on("keydown", scope.handleKeyDown);
$(args.container).append(" to ");
$to = $("<INPUT type=text style='width:40px' />")
.appendTo(args.container)
.on("keydown", scope.handleKeyDown);
scope.focus();
};
this.handleKeyDown = function (e) {
if (e.keyCode == $.ui.keyCode.LEFT || e.keyCode == $.ui.keyCode.RIGHT || e.keyCode == $.ui.keyCode.TAB) {
e.stopImmediatePropagation();
}
};
this.destroy = function () {
$(args.container).empty();
};
this.focus = function () {
$from.focus();
};
this.serializeValue = function () {
return {from: parseInt($from.val(), 10), to: parseInt($to.val(), 10)};
};
this.applyValue = function (item, state) {
item.from = state.from;
item.to = state.to;
};
this.loadValue = function (item) {
$from.val(item.from);
$to.val(item.to);
};
this.isValueChanged = function () {
return args.item.from != parseInt($from.val(), 10) || args.item.to != parseInt($to.val(), 10);
};
this.validate = function () {
if (isNaN(parseInt($from.val(), 10)) || isNaN(parseInt($to.val(), 10))) {
return {valid: false, msg: "Please type in valid numbers."};
}
if (parseInt($from.val(), 10) > parseInt($to.val(), 10)) {
return {valid: false, msg: "'from' cannot be greater than 'to'"};
}
return {valid: true, msg: null};
};
this.init();
}
$(function () {
for (var i = 0; i < 500; i++) {
var d = (data[i] = {});
d["title"] = "Task " + i;
d["from"] = Math.round(Math.random() * 100);
d["to"] = d["from"] + Math.round(Math.random() * 100);
}
grid = new Slick.Grid("#myGrid", data, columns, options);
grid.onValidationError.subscribe(function (e, args) {
alert(args.validationResults.msg);
});
})
</script>
</body>
</html>